From 0efe4d506a1eb617946562361a6c11f071e1987a Mon Sep 17 00:00:00 2001 From: Eugeny <15173395+ebirukov@users.noreply.github.com> Date: Tue, 24 Oct 2023 20:21:01 +0300 Subject: [PATCH] new function to a metaspace repository (#26) * add new function to a metaspace repository for unmarshall the fixtures from json * add new function to a metaspace repository for unmarshall the fixtures from json --- internal/pkg/generator/tmpl/meta.tmpl | 4 +++ .../pkg/generator/tmpl/octopus/fixture.tmpl | 36 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/internal/pkg/generator/tmpl/meta.tmpl b/internal/pkg/generator/tmpl/meta.tmpl index 94e3c8d..44937bb 100644 --- a/internal/pkg/generator/tmpl/meta.tmpl +++ b/internal/pkg/generator/tmpl/meta.tmpl @@ -8,6 +8,7 @@ import ( type SpaceMeta struct { PackageName string Unpacker func(ctx context.Context, tuple octopus.TupleData) (any, error) + FixtureUnpacker func(ctx context.Context, source []byte) (any, error) Fields []FieldMeta PK IndexMeta Indexes map[string]IndexMeta @@ -44,6 +45,9 @@ var NamespacePackages = NSPackage { return {{ $ns.Namespace.PackageName }}.MarshalFixtures([]*{{ $ns.Namespace.PackageName }}.{{ $ns.Namespace.PublicName }}{obj}) }, + FixtureUnpacker: func(ctx context.Context, source []byte) (any, error) { + return {{ $ns.Namespace.PackageName }}.UnmarshalFixturesFromJSON(source) + }, Fields: []FieldMeta{ {{- range $_, $field := $ns.Fields }} {{ $sname := $field.Serializer.Name }} diff --git a/internal/pkg/generator/tmpl/octopus/fixture.tmpl b/internal/pkg/generator/tmpl/octopus/fixture.tmpl index 562552b..f42cbea 100644 --- a/internal/pkg/generator/tmpl/octopus/fixture.tmpl +++ b/internal/pkg/generator/tmpl/octopus/fixture.tmpl @@ -40,7 +40,7 @@ type {{ $PublicStructName }}FTPK struct { {{ $serializer := index $serializers $sname -}} {{ $rtype = $serializer.Type -}} {{ end }} - {{ $fstruct.Name }} {{ $rtype -}} `yaml:"{{ $fstruct.Name | snakeCase -}}"` + {{ $fstruct.Name }} {{ $rtype -}} `yaml:"{{ $fstruct.Name | snakeCase -}}" mapstructure:"{{ $fstruct.Name | snakeCase -}}" json:"{{ $fstruct.Name | snakeCase -}}"` {{- end }} } @@ -107,6 +107,22 @@ func UnmarshalFixtures(source []byte) []*{{$PublicStructName}} { return objs } + +func UnmarshalFixturesFromJSON(source []byte) ([]{{$PublicStructName}}FT, error) { + source = bytes.TrimLeft(source, " \t\r\n") + + if len(source) > 0 && source[0] == '{' { + source = []byte(fmt.Sprintf("[%s]", string(source))) + } + + var v []{{$PublicStructName}}FT + + if err := json.Unmarshal([]byte(source), &v); err != nil { + return nil, err + } + + return v, nil +} {{ end }} func (objs {{$PublicStructName}}List) String() string { @@ -139,10 +155,26 @@ type {{ $PublicStructName }}FT struct { {{ $serializer := index $serializers $sname -}} {{ $rtype = $serializer.Type -}} {{ end }} - {{ $fstruct.Name }} {{ $rtype -}} `yaml:"{{ $fstruct.Name | snakeCase -}}"` + {{ $fstruct.Name }} {{ $rtype -}} `yaml:"{{ $fstruct.Name | snakeCase -}}" mapstructure:"{{ $fstruct.Name | snakeCase -}}" json:"{{ $fstruct.Name | snakeCase -}}"` {{- end }} } +func UnmarshalFixturesFromJSON(source []byte) ([]{{$PublicStructName}}FT, error) { + source = bytes.TrimLeft(source, " \t\r\n") + + if len(source) > 0 && source[0] == '{' { + source = []byte(fmt.Sprintf("[%s]", string(source))) + } + + var v []{{$PublicStructName}}FT + + if err := json.Unmarshal([]byte(source), &v); err != nil { + return nil, err + } + + return v, nil +} + func MarshalFixtures(objs []*{{$PublicStructName}}) ([]byte, error) { fts := make([]{{$PublicStructName}}FT, 0, len(objs)) for _, obj := range objs {