Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 47 additions & 40 deletions make.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,50 +211,57 @@ func (c *MakeConfig) genAnyMap(typ reflect.Type) *Generator[any] {
}

func (c *MakeConfig) genAnyStruct(typ reflect.Type) *Generator[any] {
customFields := map[string]*Generator[any]{}
if c.Fields != nil {
if custom, ok := c.Fields[typ]; ok {
customFields = custom
}
}

numFields := typ.NumField()
fieldGens := make([]*Generator[any], numFields)
for i := 0; i < numFields; i++ {
field := typ.Field(i)
if !field.IsExported() {
continue
}

if gen, ok := customFields[field.Name]; ok {
fieldGens[i] = gen
} else {
fieldGens[i] = c.newMakeGen(field.Type)
switch typ {
case reflect.TypeOf(time.Time{}):
return Custom[any](func(t *T) any {
return time.Unix(Int64().value(t), 0).UTC()
})
default:
customFields := map[string]*Generator[any]{}
if c.Fields != nil {
if custom, ok := c.Fields[typ]; ok {
customFields = custom
}
}
}

return Custom(func(t *T) any {
s := reflect.Indirect(reflect.New(typ))

fieldsSet := 0

numFields := typ.NumField()
fieldGens := make([]*Generator[any], numFields)
for i := 0; i < numFields; i++ {
if fieldGens[i] == nil {
field := typ.Field(i)
if !field.IsExported() {
continue
}

value := fieldGens[i].value(t)
if value == nil {
continue

if gen, ok := customFields[field.Name]; ok {
fieldGens[i] = gen
} else {
fieldGens[i] = c.newMakeGen(field.Type)
}

s.Field(i).Set(reflect.ValueOf(value))
fieldsSet++
}

if fieldsSet == 0 {
t.s.drawBits(0)
}

return s.Interface()
})

return Custom(func(t *T) any {
s := reflect.Indirect(reflect.New(typ))

fieldsSet := 0
for i := 0; i < numFields; i++ {
if fieldGens[i] == nil {
continue
}

value := fieldGens[i].value(t)
if value == nil {
continue
}

s.Field(i).Set(reflect.ValueOf(value))
fieldsSet++
}

if fieldsSet == 0 {
t.s.drawBits(0)
}

return s.Interface()
})
}
}
Loading