48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package document
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"githouse.ru/macrox/gotygen/pkg/utils"
|
|
)
|
|
|
|
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:
|
|
exists.Type = mergeStructs(exists.Type.(Struct), field.Type.(Struct))
|
|
}
|
|
continue
|
|
}
|
|
exists.Type = NewAny()
|
|
continue
|
|
}
|
|
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
|
|
}
|
|
|
|
func mergeDeclaredTypes(a, b DeclaredType) DeclaredType {
|
|
if utils.AllIsType[Struct](a.Type(), b.Type()) {
|
|
mergeStructs(a.Type().(Struct), b.Type().(Struct))
|
|
}
|
|
return a
|
|
}
|