Skip to content

Commit

Permalink
new function to a metaspace repository (#26)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ebirukov authored Oct 24, 2023
1 parent d4c9014 commit 0efe4d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions internal/pkg/generator/tmpl/meta.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand Down
36 changes: 34 additions & 2 deletions internal/pkg/generator/tmpl/octopus/fixture.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0efe4d5

Please sign in to comment.