goprotobuf: Use encoding.TextMarshaler and encoding.TextUnmarshaler in place of
the identical custom interfaces.
encoding.Text{Marshaler,Unmarshaler} were added in Go 1.2 (April 2013);
it's safe to rely on them now.
LGTM=r
R=r
CC=golang-codereviews
https://codereview.appspot.com/181770043
diff --git a/proto/text.go b/proto/text.go
index 5063009..a6fd69c 100644
--- a/proto/text.go
+++ b/proto/text.go
@@ -36,6 +36,7 @@
import (
"bufio"
"bytes"
+ "encoding"
"fmt"
"io"
"log"
@@ -74,13 +75,6 @@
w writer
}
-// textMarshaler is implemented by Messages that can marshal themsleves.
-// It is identical to encoding.TextMarshaler, introduced in go 1.2,
-// which will eventually replace it.
-type textMarshaler interface {
- MarshalText() (text []byte, err error)
-}
-
func (w *textWriter) WriteString(s string) (n int, err error) {
if !strings.Contains(s, "\n") {
if !w.compact && w.complete {
@@ -358,7 +352,7 @@
}
}
w.indent()
- if tm, ok := v.Interface().(textMarshaler); ok {
+ if tm, ok := v.Interface().(encoding.TextMarshaler); ok {
text, err := tm.MarshalText()
if err != nil {
return err
@@ -653,7 +647,7 @@
compact: compact,
}
- if tm, ok := pb.(textMarshaler); ok {
+ if tm, ok := pb.(encoding.TextMarshaler); ok {
text, err := tm.MarshalText()
if err != nil {
return err
diff --git a/proto/text_parser.go b/proto/text_parser.go
index 1799e1b..dc477c9 100644
--- a/proto/text_parser.go
+++ b/proto/text_parser.go
@@ -35,6 +35,7 @@
// TODO: message sets.
import (
+ "encoding"
"errors"
"fmt"
"reflect"
@@ -43,13 +44,6 @@
"unicode/utf8"
)
-// textUnmarshaler is implemented by Messages that can unmarshal themsleves.
-// It is identical to encoding.TextUnmarshaler, introduced in go 1.2,
-// which will eventually replace it.
-type textUnmarshaler interface {
- UnmarshalText(text []byte) error
-}
-
type ParseError struct {
Message string
Line int // 1-based line number
@@ -659,7 +653,7 @@
default:
return p.errorf("expected '{' or '<', found %q", tok.value)
}
- // TODO: Handle nested messages which implement textUnmarshaler.
+ // TODO: Handle nested messages which implement encoding.TextUnmarshaler.
return p.readStruct(fv, terminator)
case reflect.Uint32:
if x, err := strconv.ParseUint(tok.value, 0, 32); err == nil {
@@ -680,7 +674,7 @@
// If a required field is not set and no other error occurs,
// UnmarshalText returns *RequiredNotSetError.
func UnmarshalText(s string, pb Message) error {
- if um, ok := pb.(textUnmarshaler); ok {
+ if um, ok := pb.(encoding.TextUnmarshaler); ok {
err := um.UnmarshalText([]byte(s))
return err
}