]> Cypherpunks repositories - gostls13.git/commitdiff
html,bzip2,sql: rename Error methods that return error to Err
authorGustavo Niemeyer <gustavo@niemeyer.net>
Fri, 4 Nov 2011 13:50:20 +0000 (09:50 -0400)
committerGustavo Niemeyer <gustavo@niemeyer.net>
Fri, 4 Nov 2011 13:50:20 +0000 (09:50 -0400)
There are three classes of methods/functions called Error:

a) The Error method in the just introduced error interface
b) Error methods that create or report errors (http.Error, etc)
c) Error methods that return errors previously associated with
   the receiver (Tokenizer.Error, rows.Error, etc).

This CL introduces the convention that methods in case (c)
should be named Err.

The reasoning for the change is:

- The change differentiates the two kinds of APIs based on
  names rather than just on signature, unloading Error a bit
- Err is closer to the err variable name that is so commonly
  used with the intent of verifying an error
- Err is shorter and thus more convenient to be used often
  on error verifications, such as in iterators following the
  convention of the sql package.

R=bradfitz, rsc
CC=golang-dev
https://golang.org/cl/5327064

src/cmd/gofix/Makefile
src/cmd/gofix/fix.go
src/cmd/gofix/htmlerr.go [new file with mode: 0644]
src/cmd/gofix/htmlerr_test.go [new file with mode: 0644]
src/pkg/compress/bzip2/bit_reader.go
src/pkg/compress/bzip2/bzip2.go
src/pkg/exp/sql/sql.go
src/pkg/html/parse.go
src/pkg/html/token.go
src/pkg/html/token_test.go

index fea50cccc5c48ae33e26518c1fa6b46f18b02c32..85bef2815f162d4aa504c103ba62e80a156dc9e8 100644 (file)
@@ -9,6 +9,7 @@ GOFILES=\
        error.go\
        filepath.go\
        fix.go\
+       htmlerr.go\
        httpfinalurl.go\
        httpfs.go\
        httpheaders.go\
index 394685a15abd6f3ae06fcc9f71e31976ccee32ec..f153da970178f2324b797465790857e4922bd49d 100644 (file)
@@ -481,7 +481,7 @@ func newPkgDot(pos token.Pos, pkg, name string) ast.Expr {
        }
 }
 
-// renameTop renames all references to the top-level name top.
+// renameTop renames all references to the top-level name old.
 // It returns true if it makes any changes.
 func renameTop(f *ast.File, old, new string) bool {
        var fixed bool
diff --git a/src/cmd/gofix/htmlerr.go b/src/cmd/gofix/htmlerr.go
new file mode 100644 (file)
index 0000000..b5105c8
--- /dev/null
@@ -0,0 +1,47 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+import (
+       "go/ast"
+)
+
+func init() {
+       register(htmlerrFix)
+}
+
+var htmlerrFix = fix{
+       "htmlerr",
+       "2011-11-04",
+       htmlerr,
+       `Rename html's Tokenizer.Error method to Err.
+
+http://codereview.appspot.com/5327064/
+`,
+}
+
+var htmlerrTypeConfig = &TypeConfig{
+       Func: map[string]string{
+               "html.NewTokenizer": "html.Tokenizer",
+       },
+}
+
+func htmlerr(f *ast.File) bool {
+       if !imports(f, "html") {
+               return false
+       }
+
+       typeof, _ := typecheck(htmlerrTypeConfig, f)
+
+       fixed := false
+       walk(f, func(n interface{}) {
+               s, ok := n.(*ast.SelectorExpr)
+               if ok && typeof[s.X] == "html.Tokenizer" && s.Sel.Name == "Error" {
+                       s.Sel.Name = "Err"
+                       fixed = true
+               }
+       })
+       return fixed
+}
diff --git a/src/cmd/gofix/htmlerr_test.go b/src/cmd/gofix/htmlerr_test.go
new file mode 100644 (file)
index 0000000..043abc4
--- /dev/null
@@ -0,0 +1,39 @@
+// Copyright 2011 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+func init() {
+       addTestCases(htmlerrTests, htmlerr)
+}
+
+var htmlerrTests = []testCase{
+       {
+               Name: "htmlerr.0",
+               In: `package main
+
+import (
+       "html"
+)
+
+func f() {
+       e := errors.New("")
+       t := html.NewTokenizer(r)
+       _, _ = e.Error(), t.Error()
+}
+`,
+               Out: `package main
+
+import (
+       "html"
+)
+
+func f() {
+       e := errors.New("")
+       t := html.NewTokenizer(r)
+       _, _ = e.Error(), t.Err()
+}
+`,
+       },
+}
index d058c14833ba362631b0ca6db3020092bf5de0d3..b2c13e50ca4684bfd5d1f5ecf54296b515e76113 100644 (file)
@@ -37,7 +37,7 @@ func newBitReader(r io.Reader) bitReader {
 
 // ReadBits64 reads the given number of bits and returns them in the
 // least-significant part of a uint64. In the event of an error, it returns 0
-// and the error can be obtained by calling Error().
+// and the error can be obtained by calling Err().
 func (br *bitReader) ReadBits64(bits uint) (n uint64) {
        for bits > br.bits {
                b, err := br.r.ReadByte()
@@ -82,6 +82,6 @@ func (br *bitReader) ReadBit() bool {
        return n != 0
 }
 
-func (br *bitReader) Error() error {
+func (br *bitReader) Err() error {
        return br.err
 }
index 343cca03e349ef51e49d87185ac0b2272e0afb8a..3dc8c62061530fde60f536a83aaf31b26e1c3ebe 100644 (file)
@@ -80,7 +80,7 @@ func (bz2 *reader) Read(buf []byte) (n int, err error) {
 
        if !bz2.setupDone {
                err = bz2.setup()
-               brErr := bz2.br.Error()
+               brErr := bz2.br.Err()
                if brErr != nil {
                        err = brErr
                }
@@ -91,7 +91,7 @@ func (bz2 *reader) Read(buf []byte) (n int, err error) {
        }
 
        n, err = bz2.read(buf)
-       brErr := bz2.br.Error()
+       brErr := bz2.br.Err()
        if brErr != nil {
                err = brErr
        }
index 1af8e063cf3ed20bdb3fa793db1e9afa464d9f87..291af7f67dcb0878f8c1fce8322daa9f0ea2619d 100644 (file)
@@ -620,7 +620,7 @@ func (s *Stmt) Close() error {
 //         err = rows.Scan(&id, &name)
 //         ...
 //     }
-//     err = rows.Error() // get any Error encountered during iteration
+//     err = rows.Err() // get any error encountered during iteration
 //     ...
 type Rows struct {
        db          *DB
@@ -651,8 +651,8 @@ func (rs *Rows) Next() bool {
        return rs.lasterr == nil
 }
 
-// Error returns the error, if any, that was encountered during iteration.
-func (rs *Rows) Error() error {
+// Err returns the error, if any, that was encountered during iteration.
+func (rs *Rows) Err() error {
        if rs.lasterr == io.EOF {
                return nil
        }
index 811e2654731d60cee8980ea1ea787744e5de8013..fae0975d376cab67a70d6a4f604574f83634cb86 100644 (file)
@@ -250,7 +250,7 @@ func (p *parser) read() error {
        p.tok = p.tokenizer.Token()
        switch p.tok.Type {
        case ErrorToken:
-               return p.tokenizer.Error()
+               return p.tokenizer.Err()
        case SelfClosingTagToken:
                p.hasSelfClosingToken = true
                p.tok.Type = StartTagToken
index 9213844728b82059623d13a30b61dcca95733de1..2c138227b1049ffc738712a677a452e1bd0a19db 100644 (file)
@@ -149,9 +149,9 @@ type Tokenizer struct {
        textIsRaw bool
 }
 
-// Error returns the error associated with the most recent ErrorToken token.
+// Err returns the error associated with the most recent ErrorToken token.
 // This is typically io.EOF, meaning the end of tokenization.
-func (z *Tokenizer) Error() error {
+func (z *Tokenizer) Err() error {
        if z.tt != ErrorToken {
                return nil
        }
index 76cc9f835da36fc3f7917c2a4f188e16156c5020..61d4e67c06d7f178400672a65d539fda6e5d1b30 100644 (file)
@@ -427,7 +427,7 @@ loop:
                if tt.golden != "" {
                        for i, s := range strings.Split(tt.golden, "$") {
                                if z.Next() == ErrorToken {
-                                       t.Errorf("%s token %d: want %q got error %v", tt.desc, i, s, z.Error())
+                                       t.Errorf("%s token %d: want %q got error %v", tt.desc, i, s, z.Err())
                                        continue loop
                                }
                                actual := z.Token().String()
@@ -438,8 +438,8 @@ loop:
                        }
                }
                z.Next()
-               if z.Error() != io.EOF {
-                       t.Errorf("%s: want EOF got %q", tt.desc, z.Error())
+               if z.Err() != io.EOF {
+                       t.Errorf("%s: want EOF got %q", tt.desc, z.Err())
                }
        }
 }
@@ -543,8 +543,8 @@ loop:
                tt := z.Next()
                switch tt {
                case ErrorToken:
-                       if z.Error() != io.EOF {
-                               t.Error(z.Error())
+                       if z.Err() != io.EOF {
+                               t.Error(z.Err())
                        }
                        break loop
                case TextToken: