Skip to content

Commit

Permalink
fix(#14): return usable type name (in Name attr) (#16)
Browse files Browse the repository at this point in the history
* fix(#14): return short type name (in Name attr)

* feat: add type Name and InternalName
  • Loading branch information
leorolland authored Nov 16, 2023
1 parent 8b3796a commit c99fca8
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 26 deletions.
6 changes: 3 additions & 3 deletions examples/1_validator/main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"unicode"
)

func (v {{ .Type }}) Validate() error {
func (v {{ .Type.InternalName }}) Validate() error {
{{ range .Attributes }} {{$attribute := .}}
{{ if eq .Type "string" }}
{{ if eq .Type.InternalName "string" }}
{{ range .Comments }}
{{ if eq . "+optional" }}
if v.{{ $attribute.Name }} == "" {
Expand All @@ -22,7 +22,7 @@ func (v {{ .Type }}) Validate() error {
{{ end }}
{{ end }}
{{ end }}
{{ if eq .Type "int" "uint" "uint8" "uint32" "float32" "float64" }}
{{ if eq .Type.InternalName "int" "uint" "uint8" "uint32" "float32" "float64" }}
// WORK IN PROGRESS
{{ range .Comments }}
{{ $comparators := split "," . }}
Expand Down
9 changes: 9 additions & 0 deletions examples/2_getters/getters.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package test

{{ range .Attributes }}
{{ if has "+getter" .Comments }}{{ $receiverName := substr 0 1 $.Type.InternalName | lower}}
func ({{ $receiverName }} *{{ $.Type.InternalName }}) Get{{ camelcase .Name }}() {{ .Type.InternalName }} {
return {{ $receiverName }}.{{.Name}}
}
{{ end }}
{{ end }}
15 changes: 15 additions & 0 deletions examples/2_getters/test/car.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package test

//go:generate genz -type Car -template ../getters.tmpl -output car.gen.go
type Car struct {

//+getter
model string

//+getter
wheels []Wheel

motorReference string
}

type Wheel struct{}
32 changes: 28 additions & 4 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ import (
)

func Parse(pkg *packages.Package, structName string) (Struct, error) {
parsedStruct := Struct{Type: Type(structName)}
parsedStruct := Struct{
Type: Type{
Name: fmt.Sprintf("%s.%s", pkg.Name, structName),
InternalName: structName,
},
}

found := false
for ident := range pkg.TypesInfo.Defs {
if ident.Name == structName {
Expand Down Expand Up @@ -84,7 +90,7 @@ func structAttributes(typesInfo *types.Info, structType *ast.StructType) []Attri

attributes[i] = Attribute{
Name: field.Names[0].Name,
Type: Type(typesInfo.TypeOf(field.Type).String()),
Type: parseType(typesInfo.TypeOf(field.Type)),
Comments: comments,
}
}
Expand All @@ -106,15 +112,15 @@ func structMethods(namedType *types.Named) ([]Method, error) {
if signature.Params() != nil {
params = make([]Type, signature.Params().Len())
for j := 0; j < signature.Params().Len(); j++ {
params[j] = Type(signature.Params().At(j).Type().String())
params[j] = parseType(signature.Params().At(j).Type())
}
}

returns := []Type{}
if signature.Results() != nil {
returns = make([]Type, signature.Results().Len())
for j := 0; j < signature.Results().Len(); j++ {
returns[j] = Type(signature.Results().At(j).Type().String())
returns[j] = parseType(signature.Results().At(j).Type())
}
}

Expand All @@ -132,3 +138,21 @@ func structMethods(namedType *types.Named) ([]Method, error) {

return methods, nil
}

func parseType(t types.Type) Type {
// Remove every qualifier before the type name
// transforming "github.com/google/uuid.UUID" into "UUID"
noPackageQualifier := func(_ *types.Package) string { return "" }

// Adds the package name qualifier before the type name
// transforming "github.com/google/uuid.UUID" into "uuid.UUID"
packageNameQualifier := func(pkg *types.Package) string {
return pkg.Name()
}

return Type{
Name: types.TypeString(t, packageNameQualifier), // (e.g. "uuid.UUID")
InternalName: types.TypeString(t, noPackageQualifier), // (e.g. "UUID")
}

}
Loading

0 comments on commit c99fca8

Please sign in to comment.