- Fix omitempty

- Up version
This commit is contained in:
Alexander Garin 2023-02-15 15:52:57 +03:00
parent a3d53ce277
commit 1fbda33ac3
4 changed files with 14 additions and 4 deletions

View File

@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)
const version = "0.0.1"
const version = "0.0.2"
var versionCmd = &cobra.Command{
Use: "version",

2
go.mod
View File

@ -1,6 +1,6 @@
module githouse.ru/macrox/gotygen
go 1.20
go 1.19
require (
github.com/spf13/cobra v1.6.1

View File

@ -7,12 +7,14 @@ import (
)
func mergeStructs(a, b Struct) Struct {
ch := make(map[string]bool, a.Len())
for _, field := range b.Fields() {
field.OmitEmpty = true
if utils.IsType[Struct](field.Type) || utils.IsType[Ref](field.Type) {
field.Type.Nullable()
}
if exists, ok := a.Get(field.Key); ok {
ch[exists.Key] = true
if reflect.TypeOf(exists.Type) == reflect.TypeOf(field.Type) {
switch field.Type.(type) {
case Struct:
@ -25,6 +27,15 @@ func mergeStructs(a, b Struct) Struct {
}
a.Set(field)
}
// Check not exists and set nullable and omitempty
for _, field := range a.Fields() {
if _, ok := ch[field.Key]; !ok {
field.OmitEmpty = true
if utils.IsType[Struct](field.Type) || utils.IsType[Ref](field.Type) {
field.Type.Nullable()
}
}
}
return a
}

View File

@ -171,7 +171,7 @@ func (f *jsonFiller) scanObjectType(refOut *document.Document, rootName string)
}
s := document.NewStruct()
for i, key := range keys {
name, omitEmpty := f.ToCamelCase(key), false
name, omitEmpty := f.ToCamelCase(key), utils.IsType[document.Map](types[i]) || utils.IsType[document.Array](types[i])
if refOut != nil {
if arr, ok := types[i].(document.Array); ok {
element := arr.Element()
@ -180,7 +180,6 @@ func (f *jsonFiller) scanObjectType(refOut *document.Document, rootName string)
refOut.PutDeclaredType(document.NewDeclaredType(id, element), true)
arr.Element(document.NewRef(id))
}
omitEmpty = true
} else if _, ok = types[i].(document.Struct); ok {
id := f.normalizeId(rootName, name)
refOut.PutDeclaredType(document.NewDeclaredType(id, types[i]), true)