Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gen-accessors: skip Client struct and unexported fields #794

Merged
merged 4 commits into from
Dec 2, 2017
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
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