Skip to content
Merged
Show file tree
Hide file tree
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
42 changes: 37 additions & 5 deletions github/gen-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ func (t *templateData) processAST(f *ast.File) error {
continue
}
for _, field := range st.Fields.List {
se, ok := field.Type.(*ast.StarExpr)
if len(field.Names) == 0 || !ok {
if len(field.Names) == 0 {
continue
}

Expand All @@ -132,13 +131,25 @@ func (t *templateData) processAST(f *ast.File) error {
continue
}

se, ok := field.Type.(*ast.StarExpr)
if !ok {
switch x := field.Type.(type) {
case *ast.MapType:
t.addMapType(x, ts.Name.String(), fieldName.String(), false)
continue
}

logf("Skipping field type %T, fieldName=%v", field.Type, fieldName)
continue
}

switch x := se.X.(type) {
case *ast.ArrayType:
t.addArrayType(x, ts.Name.String(), fieldName.String())
case *ast.Ident:
t.addIdent(x, ts.Name.String(), fieldName.String())
case *ast.MapType:
t.addMapType(x, ts.Name.String(), fieldName.String())
t.addMapType(x, ts.Name.String(), fieldName.String(), true)
case *ast.SelectorExpr:
t.addSelectorExpr(x, ts.Name.String(), fieldName.String())
default:
Expand Down Expand Up @@ -232,7 +243,7 @@ func (t *templateData) addIdent(x *ast.Ident, receiverType, fieldName string) {
t.Getters = append(t.Getters, newGetter(receiverType, fieldName, x.String(), zeroValue, namedStruct))
}

func (t *templateData) addMapType(x *ast.MapType, receiverType, fieldName string) {
func (t *templateData) addMapType(x *ast.MapType, receiverType, fieldName string, isAPointer bool) {
var keyType string
switch key := x.Key.(type) {
case *ast.Ident:
Expand All @@ -253,7 +264,9 @@ func (t *templateData) addMapType(x *ast.MapType, receiverType, fieldName string

fieldType := fmt.Sprintf("map[%v]%v", keyType, valueType)
zeroValue := fmt.Sprintf("map[%v]%v{}", keyType, valueType)
t.Getters = append(t.Getters, newGetter(receiverType, fieldName, fieldType, zeroValue, false))
ng := newGetter(receiverType, fieldName, fieldType, zeroValue, false)
ng.MapType = !isAPointer
t.Getters = append(t.Getters, ng)
}

func (t *templateData) addSelectorExpr(x *ast.SelectorExpr, receiverType, fieldName string) {
Expand Down Expand Up @@ -300,6 +313,7 @@ type getter struct {
FieldType string
ZeroValue string
NamedStruct bool // Getter for named struct.
MapType bool
}

type byName []*getter
Expand Down Expand Up @@ -332,6 +346,14 @@ 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.
func ({{.ReceiverVar}} *{{.ReceiverType}}) Get{{.FieldName}}() {{.FieldType}} {
if {{.ReceiverVar}} == nil || {{.ReceiverVar}}.{{.FieldName}} == nil {
return {{.ZeroValue}}
}
return {{.ReceiverVar}}.{{.FieldName}}
}
{{else}}
// Get{{.FieldName}} returns the {{.FieldName}} field if it's non-nil, zero value otherwise.
func ({{.ReceiverVar}} *{{.ReceiverType}}) Get{{.FieldName}}() {{.FieldType}} {
Expand Down Expand Up @@ -368,6 +390,16 @@ func Test{{.ReceiverType}}_Get{{.FieldName}}(tt *testing.T) {
{{.ReceiverVar}} = nil
{{.ReceiverVar}}.Get{{.FieldName}}()
}
{{else if .MapType}}
func Test{{.ReceiverType}}_Get{{.FieldName}}(tt *testing.T) {
zeroValue := {{.FieldType}}{}
{{.ReceiverVar}} := &{{.ReceiverType}}{ {{.FieldName}}: zeroValue }
{{.ReceiverVar}}.Get{{.FieldName}}()
{{.ReceiverVar}} = &{{.ReceiverType}}{}
{{.ReceiverVar}}.Get{{.FieldName}}()
{{.ReceiverVar}} = nil
{{.ReceiverVar}}.Get{{.FieldName}}()
}
{{else}}
func Test{{.ReceiverType}}_Get{{.FieldName}}(tt *testing.T) {
var zeroValue {{.FieldType}}
Expand Down
5 changes: 0 additions & 5 deletions github/gen-stringify-test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,6 @@ func (t *templateData) processAST(f *ast.File) error {
continue
}

if _, ok := field.Type.(*ast.MapType); ok {
t.addMapType(ts.Name.String(), fieldName.String())
continue
}

se, ok := field.Type.(*ast.StarExpr)
if !ok {
logf("Ignoring type %T for Name=%q, FieldName=%q", field.Type, ts.Name.String(), fieldName.String())
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.

23 changes: 10 additions & 13 deletions github/github-stringify_test.go

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

3 changes: 3 additions & 0 deletions github/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func stringifyValue(w io.Writer, val reflect.Value) {
if fv.Kind() == reflect.Slice && fv.IsNil() {
continue
}
if fv.Kind() == reflect.Map && fv.IsNil() {
continue
}

if sep {
w.Write([]byte(", "))
Expand Down
4 changes: 2 additions & 2 deletions github/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ func TestString(t *testing.T) {
{Event{ID: String("1")}, `github.Event{ID:"1"}`},
{GistComment{ID: Int64(1)}, `github.GistComment{ID:1}`},
{GistFile{Size: Int(1)}, `github.GistFile{Size:1}`},
{Gist{ID: String("1")}, `github.Gist{ID:"1", Files:map[]}`},
{Gist{ID: String("1")}, `github.Gist{ID:"1"}`},
{GitObject{SHA: String("s")}, `github.GitObject{SHA:"s"}`},
{Gitignore{Name: String("n")}, `github.Gitignore{Name:"n"}`},
{Hook{ID: Int64(1)}, `github.Hook{ID:1, LastResponse:map[], Config:map[]}`},
{Hook{ID: Int64(1)}, `github.Hook{ID:1}`},
{IssueComment{ID: Int64(1)}, `github.IssueComment{ID:1}`},
{Issue{Number: Int(1)}, `github.Issue{Number:1}`},
{Key{ID: Int64(1)}, `github.Key{ID:1}`},
Expand Down