-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cmd): remove duplicated code for making spec (#4)
- Loading branch information
1 parent
538e05b
commit 4b66c51
Showing
5 changed files
with
145 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package resource | ||
|
||
import ( | ||
"io" | ||
"io/fs" | ||
|
||
"github.com/siyul-park/uniflow/pkg/scheme" | ||
) | ||
|
||
type ( | ||
Builder struct { | ||
scheme *scheme.Scheme | ||
namespace string | ||
fsys fs.FS | ||
filename string | ||
} | ||
) | ||
|
||
func NewBuilder() *Builder { | ||
return &Builder{} | ||
} | ||
|
||
func (b *Builder) Scheme(scheme *scheme.Scheme) *Builder { | ||
b.scheme = scheme | ||
return b | ||
} | ||
|
||
func (b *Builder) Namespace(namespace string) *Builder { | ||
b.namespace = namespace | ||
return b | ||
} | ||
|
||
func (b *Builder) FS(fsys fs.FS) *Builder { | ||
b.fsys = fsys | ||
return b | ||
} | ||
|
||
func (b *Builder) Filename(filename string) *Builder { | ||
b.filename = filename | ||
return b | ||
} | ||
|
||
func (b *Builder) Build() ([]scheme.Spec, error) { | ||
if b.fsys == nil || b.filename == "" { | ||
return nil, nil | ||
} | ||
file, err := b.fsys.Open(b.filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
data, err := io.ReadAll(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var raws []map[string]any | ||
if err := UnmarshalYAMLOrJSON(data, &raws); err != nil { | ||
var e map[string]any | ||
if err := UnmarshalYAMLOrJSON(data, &e); err != nil { | ||
return nil, err | ||
} else { | ||
raws = []map[string]any{e} | ||
} | ||
} | ||
|
||
codec := NewSpecCodec(SpecCodecOptions{ | ||
Scheme: b.scheme, | ||
Namespace: b.namespace, | ||
}) | ||
|
||
var specs []scheme.Spec | ||
for _, raw := range raws { | ||
if spec, err := codec.Decode(raw); err != nil { | ||
return nil, err | ||
} else { | ||
specs = append(specs, spec) | ||
} | ||
} | ||
|
||
return specs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package resource | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"testing/fstest" | ||
|
||
"github.com/go-faker/faker/v4" | ||
"github.com/oklog/ulid/v2" | ||
"github.com/siyul-park/uniflow/pkg/node" | ||
"github.com/siyul-park/uniflow/pkg/scheme" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestBuilder_Build(t *testing.T) { | ||
s := scheme.New() | ||
fsys := make(fstest.MapFS) | ||
|
||
filename := "spec.json" | ||
kind := faker.Word() | ||
|
||
spec := &scheme.SpecMeta{ | ||
ID: ulid.Make(), | ||
Kind: kind, | ||
Namespace: scheme.NamespaceDefault, | ||
} | ||
|
||
codec := scheme.CodecFunc(func(spec scheme.Spec) (node.Node, error) { | ||
return node.NewOneToOneNode(node.OneToOneNodeConfig{ID: spec.GetID()}), nil | ||
}) | ||
|
||
s.AddKnownType(kind, &scheme.SpecMeta{}) | ||
s.AddCodec(kind, codec) | ||
|
||
data, _ := json.Marshal(spec) | ||
|
||
fsys[filename] = &fstest.MapFile{ | ||
Data: data, | ||
} | ||
|
||
builder := NewBuilder(). | ||
Scheme(s). | ||
Namespace(scheme.NamespaceDefault). | ||
FS(fsys). | ||
Filename(filename) | ||
|
||
specs, err := builder.Build() | ||
assert.NoError(t, err) | ||
assert.Len(t, specs, 1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters