Skip to content

Commit

Permalink
Remove generated accessors for Client struct and unexported fields. (#…
Browse files Browse the repository at this point in the history
…794)

This change makes gen-accessors.go skip the following structs and fields
from consideration when generating accessor methods:

1.	Unexported structs and fields are skipped. They're unexported,
	and shouldn't have accessors.
2.	Client struct is explicitly blacklistd and skipped. It was not meant
	to have accessors, and it doesn't need them.

The generated accessors that are removed were added relatively recently,
and it's not likely they were used since that time.

Resolves #778.
  • Loading branch information
sahildua2305 authored and dmitshur committed Dec 2, 2017
1 parent df88dd9 commit fbfee05
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 353 deletions.
38 changes: 29 additions & 9 deletions github/gen-accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@ var (

sourceTmpl = template.Must(template.New("source").Parse(source))

// blacklist lists which "struct.method" combos to not generate.
blacklist = map[string]bool{
// blacklistStructMethod lists "struct.method" combos to skip.
blacklistStructMethod = map[string]bool{
"RepositoryContent.GetContent": true,
"Client.GetBaseURL": true,
"Client.GetUploadURL": true,
"ErrorResponse.GetResponse": true,
"RateLimitError.GetResponse": true,
"AbuseRateLimitError.GetResponse": true,
}
// blacklistStruct lists structs to skip.
blacklistStruct = map[string]bool{
"Client": true,
}
)

func logf(fmt string, args ...interface{}) {
Expand Down Expand Up @@ -95,6 +99,16 @@ func (t *templateData) processAST(f *ast.File) error {
if !ok {
continue
}
// Skip unexported identifiers.
if !ts.Name.IsExported() {
logf("Struct %v is unexported; skipping.", ts.Name)
continue
}
// Check if the struct is blacklisted.
if blacklistStruct[ts.Name.Name] {
logf("Struct %v is blacklisted; skipping.", ts.Name)
continue
}
st, ok := ts.Type.(*ast.StructType)
if !ok {
continue
Expand All @@ -106,8 +120,14 @@ func (t *templateData) processAST(f *ast.File) error {
}

fieldName := field.Names[0]
if key := fmt.Sprintf("%v.Get%v", ts.Name, fieldName); blacklist[key] {
logf("Method %v blacklisted; skipping.", key)
// Skip unexported identifiers.
if !fieldName.IsExported() {
logf("Field %v is unexported; skipping.", fieldName)
continue
}
// Check if "struct.method" is blacklisted.
if key := fmt.Sprintf("%v.Get%v", ts.Name, fieldName); blacklistStructMethod[key] {
logf("Method %v is blacklisted; skipping.", key)
continue
}

Expand Down Expand Up @@ -139,7 +159,7 @@ func (t *templateData) dump() error {
return nil
}

// Sort getters by ReceiverType.FieldName
// Sort getters by ReceiverType.FieldName.
sort.Sort(byName(t.Getters))

var buf bytes.Buffer
Expand Down Expand Up @@ -225,7 +245,7 @@ func (t *templateData) addMapType(x *ast.MapType, receiverType, fieldName string
}

func (t *templateData) addSelectorExpr(x *ast.SelectorExpr, receiverType, fieldName string) {
if strings.ToLower(fieldName[:1]) == fieldName[:1] { // non-exported field
if strings.ToLower(fieldName[:1]) == fieldName[:1] { // Non-exported field.
return
}

Expand Down Expand Up @@ -261,13 +281,13 @@ type templateData struct {
}

type getter struct {
sortVal string // lower-case version of "ReceiverType.FieldName"
ReceiverVar string // the one-letter variable name to match the ReceiverType
sortVal string // Lower-case version of "ReceiverType.FieldName".
ReceiverVar string // The one-letter variable name to match the ReceiverType.
ReceiverType string
FieldName string
FieldType string
ZeroValue string
NamedStruct bool // getter for named struct
NamedStruct bool // Getter for named struct.
}

type byName []*getter
Expand Down
Loading

0 comments on commit fbfee05

Please sign in to comment.