encoding/prototext: make unexpected EOF error into proto.Error
Also fixed/added comments on exported vars/funcs.
Change-Id: I6c42b2afb90058e026a5310598bb3ebfcd01b989
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/218357
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/encoding/prototext/decode.go b/encoding/prototext/decode.go
index 5e9975d..9dea257 100644
--- a/encoding/prototext/decode.go
+++ b/encoding/prototext/decode.go
@@ -6,7 +6,6 @@
import (
"fmt"
- "io"
"strings"
"unicode/utf8"
@@ -131,7 +130,7 @@
// Continue below.
case text.EOF:
if checkDelims {
- return io.ErrUnexpectedEOF
+ return text.ErrUnexpectedEOF
}
return nil
default:
diff --git a/internal/encoding/text/decode.go b/internal/encoding/text/decode.go
index 074e849..eb10ea1 100644
--- a/internal/encoding/text/decode.go
+++ b/internal/encoding/text/decode.go
@@ -45,6 +45,9 @@
return &Decoder{orig: b, in: b}
}
+// ErrUnexpectedEOF means that EOF was encountered in the middle of the input.
+var ErrUnexpectedEOF = errors.New("%v", io.ErrUnexpectedEOF)
+
// call specifies which Decoder method was invoked.
type call uint8
@@ -70,7 +73,7 @@
return d.lastToken, d.lastErr
}
- tok, err := d.parseNext(d.lastToken.Kind())
+ tok, err := d.parseNext(d.lastToken.kind)
if err != nil {
return Token{}, err
}
@@ -114,7 +117,7 @@
case Name:
// Next token can be MessageOpen, ListOpen or Scalar.
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
switch ch := d.in[0]; ch {
case '{', '<':
@@ -148,7 +151,7 @@
case MessageOpen:
// Next token can be MessageClose, comma, semicolon or Name.
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
switch ch := d.in[0]; ch {
case closeCh:
@@ -167,7 +170,7 @@
case ListOpen:
// Next token can be ListClose or comma.
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
switch ch := d.in[0]; ch {
case ']':
@@ -183,7 +186,7 @@
case MessageOpen:
// Next token can be MessageClose or Name.
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
_, closeCh := d.currentOpenKind()
switch ch := d.in[0]; ch {
@@ -217,7 +220,7 @@
case MessageOpen:
// Next token can be MessageClose, comma, semicolon or Name.
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
switch ch := d.in[0]; ch {
case closeCh:
@@ -236,7 +239,7 @@
case ListOpen:
// Next token can be ListClose or comma
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
switch ch := d.in[0]; ch {
case closeCh:
@@ -252,7 +255,7 @@
case ListOpen:
// Next token can be ListClose, MessageStart or Scalar.
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
switch ch := d.in[0]; ch {
case ']':
@@ -286,7 +289,7 @@
case MessageOpen:
// Next token can be MessageClose, comma, semicolon or Name.
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
switch ch := d.in[0]; ch {
case closeCh:
@@ -319,7 +322,7 @@
case MessageOpen:
// Next token can be MessageClose or Name.
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
switch ch := d.in[0]; ch {
case closeCh:
@@ -340,7 +343,7 @@
}
// Next token can be MessageOpen or Scalar.
if isEOF {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
switch ch := d.in[0]; ch {
case '{', '<':
@@ -432,7 +435,7 @@
// Caller already checks for [ as first character.
s := consume(d.in[1:], 0)
if len(s) == 0 {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
var name []byte
@@ -470,7 +473,7 @@
}
if !closed {
- return Token{}, io.ErrUnexpectedEOF
+ return Token{}, ErrUnexpectedEOF
}
// First character cannot be '.'. Last character cannot be '.' or '/'.
diff --git a/internal/encoding/text/decode_string.go b/internal/encoding/text/decode_string.go
index 020c8c7..d4d3490 100644
--- a/internal/encoding/text/decode_string.go
+++ b/internal/encoding/text/decode_string.go
@@ -6,7 +6,6 @@
import (
"bytes"
- "io"
"strconv"
"strings"
"unicode"
@@ -51,7 +50,7 @@
func (d *Decoder) parseString() (string, error) {
in := d.in
if len(in) == 0 {
- return "", io.ErrUnexpectedEOF
+ return "", ErrUnexpectedEOF
}
quote := in[0]
in = in[1:]
@@ -69,7 +68,7 @@
return string(out), nil
case r == '\\':
if len(in) < 2 {
- return "", io.ErrUnexpectedEOF
+ return "", ErrUnexpectedEOF
}
switch r := in[1]; r {
case '"', '\'', '\\', '?':
@@ -117,7 +116,7 @@
n = 10
}
if len(in) < n {
- return "", io.ErrUnexpectedEOF
+ return "", ErrUnexpectedEOF
}
v, err := strconv.ParseUint(string(in[2:n]), 16, 32)
if utf8.MaxRune < v || err != nil {
@@ -128,7 +127,7 @@
r := rune(v)
if utf16.IsSurrogate(r) {
if len(in) < 6 {
- return "", io.ErrUnexpectedEOF
+ return "", ErrUnexpectedEOF
}
v, err := strconv.ParseUint(string(in[2:6]), 16, 16)
r = utf16.DecodeRune(r, rune(v))
@@ -146,7 +145,7 @@
in, out = in[n+i:], append(out, in[:n+i]...)
}
}
- return "", io.ErrUnexpectedEOF
+ return "", ErrUnexpectedEOF
}
// indexNeedEscapeInString returns the index of the character that needs
diff --git a/internal/encoding/text/decode_test.go b/internal/encoding/text/decode_test.go
index 6ad31ce..4749f8c 100644
--- a/internal/encoding/text/decode_test.go
+++ b/internal/encoding/text/decode_test.go
@@ -6,7 +6,6 @@
import (
"fmt"
- "io"
"math"
"strings"
"testing"
@@ -17,7 +16,7 @@
"google.golang.org/protobuf/internal/flags"
)
-var eofErr = io.ErrUnexpectedEOF.Error()
+var eofErr = text.ErrUnexpectedEOF.Error()
type R struct {
// K is expected Kind of the returned Token object from calling Decoder.Read.
diff --git a/internal/encoding/text/decode_token.go b/internal/encoding/text/decode_token.go
index b7b4e2f..83d2b0d 100644
--- a/internal/encoding/text/decode_token.go
+++ b/internal/encoding/text/decode_token.go
@@ -1,6 +1,7 @@
// Copyright 2018 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 text
import (
@@ -16,6 +17,7 @@
// Kind represents a token kind expressible in the textproto format.
type Kind uint8
+// Kind values.
const (
Invalid Kind = iota
EOF
@@ -65,6 +67,7 @@
// NameKind represents different types of field names.
type NameKind uint8
+// NameKind values.
const (
IdentName NameKind = iota + 1
TypeName
diff --git a/internal/encoding/text/encode.go b/internal/encoding/text/encode.go
index a593978..c4ba1c5 100644
--- a/internal/encoding/text/encode.go
+++ b/internal/encoding/text/encode.go
@@ -93,7 +93,7 @@
e.out = append(e.out, e.delims[1])
}
-// Writname writes out the field name and the separator ':'.
+// WriteName writes out the field name and the separator ':'.
func (e *Encoder) WriteName(s string) {
e.prepareNext(name)
e.out = append(e.out, s...)