Skip to content

Commit

Permalink
support array type in go generate
Browse files Browse the repository at this point in the history
support array type in go generate with whitelist
  • Loading branch information
olibaa committed May 1, 2023
1 parent a938017 commit ff7fd8c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
34 changes: 28 additions & 6 deletions github/gen-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ var (
skipStructs = map[string]bool{
"Client": true,
}

// whitelistSliceGetters lists "struct.field" to add getter method
whitelistSliceGetters = map[string]bool{
"PushEvent.Commits": true,
}
)

func logf(fmt string, args ...interface{}) {
Expand Down Expand Up @@ -137,6 +142,13 @@ func (t *templateData) processAST(f *ast.File) error {
case *ast.MapType:
t.addMapType(x, ts.Name.String(), fieldName.String(), false)
continue
case *ast.ArrayType:
if key := fmt.Sprintf("%v.%v", ts.Name, fieldName); whitelistSliceGetters[key] {
logf("Method %v is whitelist; adding getter method.", key)
t.addArrayType(x, ts.Name.String(), fieldName.String(), false)
continue
}
continue
}

logf("Skipping field type %T, fieldName=%v", field.Type, fieldName)
Expand All @@ -145,7 +157,7 @@ func (t *templateData) processAST(f *ast.File) error {

switch x := se.X.(type) {
case *ast.ArrayType:
t.addArrayType(x, ts.Name.String(), fieldName.String())
t.addArrayType(x, ts.Name.String(), fieldName.String(), true)
case *ast.Ident:
t.addIdent(x, ts.Name.String(), fieldName.String())
case *ast.MapType:
Expand Down Expand Up @@ -218,17 +230,26 @@ func newGetter(receiverType, fieldName, fieldType, zeroValue string, namedStruct
}
}

func (t *templateData) addArrayType(x *ast.ArrayType, receiverType, fieldName string) {
func (t *templateData) addArrayType(x *ast.ArrayType, receiverType, fieldName string, isAPointer bool) {
var eltType string
var ng *getter
switch elt := x.Elt.(type) {
case *ast.Ident:
eltType = elt.String()
ng = newGetter(receiverType, fieldName, "[]"+eltType, "nil", false)
case *ast.StarExpr:
ident, ok := elt.X.(*ast.Ident)
if !ok {
return
}
ng = newGetter(receiverType, fieldName, "[]*"+ident.String(), "nil", false)
default:
logf("addArrayType: type %q, field %q: unknown elt type: %T %+v; skipping.", receiverType, fieldName, elt, elt)
return
}

t.Getters = append(t.Getters, newGetter(receiverType, fieldName, "[]"+eltType, "nil", false))
ng.ArrayType = !isAPointer
t.Getters = append(t.Getters, ng)
}

func (t *templateData) addIdent(x *ast.Ident, receiverType, fieldName string) {
Expand Down Expand Up @@ -322,6 +343,7 @@ type getter struct {
ZeroValue string
NamedStruct bool // Getter for named struct.
MapType bool
ArrayType bool
}

type byName []*getter
Expand Down Expand Up @@ -356,8 +378,8 @@ func ({{.ReceiverVar}} *{{.ReceiverType}}) Get{{.FieldName}}() *{{.FieldType}} {
}
return {{.ReceiverVar}}.{{.FieldName}}
}
{{else if .MapType}}
// Get{{.FieldName}} returns the {{.FieldName}} map if it's non-nil, an empty map otherwise.
{{else if or .MapType .ArrayType }}
// Get{{.FieldName}} returns the {{.FieldName}} {{if .MapType}}map{{else if .ArrayType }}slice{{end}} if it's non-nil, {{if .MapType}}an empty map{{else if .ArrayType }}nil{{end}} otherwise.
func ({{.ReceiverVar}} *{{.ReceiverType}}) Get{{.FieldName}}() {{.FieldType}} {
if {{.ReceiverVar}} == nil || {{.ReceiverVar}}.{{.FieldName}} == nil {
return {{.ZeroValue}}
Expand Down Expand Up @@ -402,7 +424,7 @@ func Test{{.ReceiverType}}_Get{{.FieldName}}(tt *testing.T) {
{{.ReceiverVar}} = nil
{{.ReceiverVar}}.Get{{.FieldName}}()
}
{{else if .MapType}}
{{else if or .MapType .ArrayType}}
func Test{{.ReceiverType}}_Get{{.FieldName}}(tt *testing.T) {
zeroValue := {{.FieldType}}{}
{{.ReceiverVar}} := &{{.ReceiverType}}{ {{.FieldName}}: zeroValue }
Expand Down
8 changes: 8 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ff7fd8c

Please sign in to comment.