Skip to content

Commit

Permalink
style changes.
Browse files Browse the repository at this point in the history
- method names can't start with "Get", enforced by the style tool.
- document our preference for return-by-reference, given the new design.
  • Loading branch information
nictuku committed May 22, 2011
1 parent 1993783 commit d80041b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions DESIGN.md
Expand Up @@ -74,6 +74,11 @@ improvement in the following regards.
* Identifier naming generally makes use of CamelCasing. Leading
upper/lowercase letter dictates private/public to package, as defined by
the Go programming language.
* Getter method names should *not* start with "Get". See
http://golang.org/doc/effective_go.html#Getters
* Functions and methods should preferably return objects by reference
(pointer) and callers can change them freely without fear of
synchronization issues, thanks to our clear channels-based design.

To ease the above, there are three make targets:

Expand Down
16 changes: 14 additions & 2 deletions src/util/style/style.go
Expand Up @@ -21,15 +21,17 @@ func main() {

v := NewNodeChecker(fset)
v.InterfaceName = regexp.MustCompile("I[A-Z][A-Za-z]+")
v.InvalidFuncName = regexp.MustCompile("^Get") // can't do negative match in Go's regexp?

for _, pkg := range pkgMap {
ast.Walk(v, pkg)
}
}

type NodeChecker struct {
fset *token.FileSet
InterfaceName *regexp.Regexp
fset *token.FileSet
InterfaceName *regexp.Regexp
InvalidFuncName *regexp.Regexp
}

func NewNodeChecker(fset *token.FileSet) *NodeChecker {
Expand All @@ -42,6 +44,10 @@ func (v *NodeChecker) Visit(node ast.Node) (w ast.Visitor) {
switch n := node.(type) {
case *ast.TypeSpec:
v.checkTypeName(n)
case *ast.FuncDecl:
if n.Recv != nil { // is a method.
v.checkFunc(n)
}
}
return v
}
Expand All @@ -62,6 +68,12 @@ func (v *NodeChecker) report(pos token.Pos, format string, args ...interface{})
allArgs...)
}

func (v *NodeChecker) checkFunc(f *ast.FuncDecl) {
if v.InvalidFuncName.MatchString(f.Name.String()) {
v.report(f.Name.NamePos, "Bad name for method %q\n", f.Name)
}
}

func (v *NodeChecker) checkTypeName(typeSpec *ast.TypeSpec) {
name := typeSpec.Name.Name
switch t := typeSpec.Type.(type) {
Expand Down

0 comments on commit d80041b

Please sign in to comment.