Skip to content

Commit

Permalink
feat(command): slice and array with pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
donatorsky committed Sep 25, 2023
1 parent 597679a commit 765bf5b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
25 changes: 17 additions & 8 deletions examples/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,21 @@ type Struct struct {
Foo string
Bar int
}
FieldData foo.OtherStruct
FieldDataPtr *foo.OtherStruct
UniqueMultiFlag baz.OtherGenericStruct[any]
UniqueMultiFlagPtr *baz.OtherGenericStruct[any]
Uuid uuid.UUID
UuidPtr *uuid.UUID
Yaml yaml.Decoder
YamlPtr *yaml.Decoder
Func func()
//FuncWithArgs func(string, *string, *[]*baz.OtherGenericStruct[any], *[3]*baz.OtherGenericStruct[any]) //TODO
FuncPtr *func()
FieldData foo.OtherStruct
FieldDataPtr *foo.OtherStruct
FieldDataPtrSlicePtr *[]*foo.OtherStruct
UniqueMultiFlag baz.OtherGenericStruct[any]
UniqueMultiFlagPtr *baz.OtherGenericStruct[any]
UniqueMultiFlagPtrSlicePtr *[]*baz.OtherGenericStruct[any]
Uuid uuid.UUID
UuidPtr *uuid.UUID
UuidPtrSlicePtr *[]*uuid.UUID
UuidPtrArrayPtr *[3]*uuid.UUID
Yaml yaml.Decoder
YamlPtr *yaml.Decoder
YamlPtrSlicePtr *[]*yaml.Decoder
YamlPtrArrayPtr *[3]*yaml.Decoder
}
20 changes: 16 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,9 @@ func detectFieldType(fieldType types.Type, logger *log.Logger, importsAliases ma
fieldType = pointerType.Elem()
}

if namedType, ok := fieldType.(*types.Named); ok {
typeFQN := namedType.String()
switch actualType := fieldType.(type) {
case *types.Named:
typeFQN := actualType.String()
typeNameIndex := strings.LastIndexByte(typeFQN, '.')
if typeNameIndex == -1 {
logger.Fatalf("Type FQN %q is expected to contain .", typeFQN)
Expand Down Expand Up @@ -327,7 +328,18 @@ func detectFieldType(fieldType types.Type, logger *log.Logger, importsAliases ma
}

return pointer, fmt.Sprintf("%s.%s", typeNamespace, typeFQN[typeNameIndex+1:])
}

return pointer, fieldType.String()
case *types.Slice:
elemPointer, elemType := detectFieldType(actualType.Elem(), logger, importsAliases, imports)

return pointer, fmt.Sprintf("[]%s%s", elemPointer, elemType)

case *types.Array:
elemPointer, elemType := detectFieldType(actualType.Elem(), logger, importsAliases, imports)

return pointer, fmt.Sprintf("[%d]%s%s", actualType.Len(), elemPointer, elemType)

default:
return pointer, fieldType.String()
}
}

0 comments on commit 765bf5b

Please sign in to comment.