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>
This commit is contained in:
Herbie Ong 2020-02-06 18:43:12 -08:00
parent 6fb29949b8
commit 952a08d7c4
6 changed files with 27 additions and 24 deletions

View File

@ -6,7 +6,6 @@ package prototext
import (
"fmt"
"io"
"strings"
"unicode/utf8"
@ -131,7 +130,7 @@ func (d decoder) unmarshalMessage(m pref.Message, checkDelims bool) error {
// Continue below.
case text.EOF:
if checkDelims {
return io.ErrUnexpectedEOF
return text.ErrUnexpectedEOF
}
return nil
default:

View File

@ -45,6 +45,9 @@ func NewDecoder(b []byte) *Decoder {
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 @@ func (d *Decoder) Read() (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
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 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) {
}
// 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 @@ func (d *Decoder) parseTypeName() (Token, error) {
// 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 @@ func (d *Decoder) parseTypeName() (Token, error) {
}
if !closed {
return Token{}, io.ErrUnexpectedEOF
return Token{}, ErrUnexpectedEOF
}
// First character cannot be '.'. Last character cannot be '.' or '/'.

View File

@ -6,7 +6,6 @@ package text
import (
"bytes"
"io"
"strconv"
"strings"
"unicode"
@ -51,7 +50,7 @@ func (d *Decoder) parseStringValue() (Token, error) {
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 @@ func (d *Decoder) parseString() (string, error) {
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 @@ func (d *Decoder) parseString() (string, error) {
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 @@ func (d *Decoder) parseString() (string, error) {
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 @@ func (d *Decoder) parseString() (string, error) {
in, out = in[n+i:], append(out, in[:n+i]...)
}
}
return "", io.ErrUnexpectedEOF
return "", ErrUnexpectedEOF
}
// indexNeedEscapeInString returns the index of the character that needs

View File

@ -6,7 +6,6 @@ package text_test
import (
"fmt"
"io"
"math"
"strings"
"testing"
@ -17,7 +16,7 @@ import (
"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.

View File

@ -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 @@ import (
// Kind represents a token kind expressible in the textproto format.
type Kind uint8
// Kind values.
const (
Invalid Kind = iota
EOF
@ -65,6 +67,7 @@ func (t Kind) String() string {
// NameKind represents different types of field names.
type NameKind uint8
// NameKind values.
const (
IdentName NameKind = iota + 1
TypeName

View File

@ -93,7 +93,7 @@ func (e *Encoder) EndMessage() {
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...)