Joe Tsai 27c2a76c85 internal/encoding/text: initial commit of proto text format parser/serializer
Package text provides a parser and serializer for the proto text format.
This focuses on the grammar of the format and is agnostic towards specific
semantics of protobuf types.

High-level API:
	func Marshal(v Value, indent string, delims [2]byte, outputASCII bool) ([]byte, error)
	func Unmarshal(b []byte) (Value, error)
	type Type uint8
		const Bool Type ...
	type Value struct{ ... }
		func ValueOf(v interface{}) Value
		func (v Value) Type() Type
		func (v Value) Bool() (x bool, ok bool)
		func (v Value) Int(b64 bool) (x int64, ok bool)
		func (v Value) Uint(b64 bool) (x uint64, ok bool)
		func (v Value) Float(b64 bool) (x float64, ok bool)
		func (v Value) Name() (protoreflect.Name, bool)
		func (v Value) String() string
		func (v Value) List() []Value
		func (v Value) Message() [][2]Value
		func (v Value) Raw() []byte

Change-Id: I4a78ec4474c160d0de4d32120651edd931ea2c1e
Reviewed-on: https://go-review.googlesource.com/127455
Reviewed-by: Herbie Ong <herbie@google.com>
2018-08-07 22:44:06 +00:00

175 lines
4.3 KiB
Go

// 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 (
"bytes"
"strings"
"google.golang.org/proto/internal/errors"
)
// Marshal serializes v as the proto text format, where v must be a Message.
// In the proto text format, the top-level value is always a message where the
// delimiters are elided.
//
// If indent is a non-empty string, it causes every entry in a List or Message
// to be preceded by the indent and trailed by a newline.
//
// If delims is not the zero value, it controls the delimiter characters used
// for messages (e.g., "{}" vs "<>").
//
// If outputASCII is true, strings will be serialized in such a way that
// multi-byte UTF-8 sequences are escaped. This property ensures that the
// overall output is ASCII (as opposed to UTF-8).
func Marshal(v Value, indent string, delims [2]byte, outputASCII bool) ([]byte, error) {
p := encoder{}
if len(indent) > 0 {
if strings.Trim(indent, " \t") != "" {
return nil, errors.New("indent may only be composed of space and tab characters")
}
p.indent = indent
p.newline = "\n"
}
switch delims {
case [2]byte{0, 0}:
p.delims = [2]byte{'{', '}'}
case [2]byte{'{', '}'}, [2]byte{'<', '>'}:
p.delims = delims
default:
return nil, errors.New("delimiters may only be \"{}\" or \"<>\"")
}
p.outputASCII = outputASCII
err := p.marshalMessage(v, false)
if !p.nerr.Merge(err) {
return nil, err
}
if len(indent) > 0 {
return append(bytes.TrimRight(p.out, "\n"), '\n'), p.nerr.E
}
return p.out, p.nerr.E
}
type encoder struct {
nerr errors.NonFatal
out []byte
indent string
indents []byte
newline string // set to "\n" if len(indent) > 0
delims [2]byte
outputASCII bool
}
func (p *encoder) marshalList(v Value) error {
if v.Type() != List {
return errors.New("invalid type %v, expected list", v.Type())
}
elems := v.List()
p.out = append(p.out, '[')
p.indents = append(p.indents, p.indent...)
if len(elems) > 0 {
p.out = append(p.out, p.newline...)
}
for i, elem := range elems {
p.out = append(p.out, p.indents...)
if err := p.marshalValue(elem); !p.nerr.Merge(err) {
return err
}
if i < len(elems)-1 {
p.out = append(p.out, ',')
}
p.out = append(p.out, p.newline...)
}
p.indents = p.indents[:len(p.indents)-len(p.indent)]
if len(elems) > 0 {
p.out = append(p.out, p.indents...)
}
p.out = append(p.out, ']')
return nil
}
func (p *encoder) marshalMessage(v Value, emitDelims bool) error {
if v.Type() != Message {
return errors.New("invalid type %v, expected message", v.Type())
}
items := v.Message()
if emitDelims {
p.out = append(p.out, p.delims[0])
p.indents = append(p.indents, p.indent...)
if len(items) > 0 {
p.out = append(p.out, p.newline...)
}
}
for i, item := range items {
p.out = append(p.out, p.indents...)
if err := p.marshalKey(item[0]); !p.nerr.Merge(err) {
return err
}
p.out = append(p.out, ':')
if len(p.indent) > 0 {
p.out = append(p.out, ' ')
}
if err := p.marshalValue(item[1]); !p.nerr.Merge(err) {
return err
}
if i < len(items)-1 && len(p.indent) == 0 {
p.out = append(p.out, ' ')
}
p.out = append(p.out, p.newline...)
}
if emitDelims {
p.indents = p.indents[:len(p.indents)-len(p.indent)]
if len(items) > 0 {
p.out = append(p.out, p.indents...)
}
p.out = append(p.out, p.delims[1])
}
return nil
}
func (p *encoder) marshalKey(v Value) error {
switch v.Type() {
case String:
var err error
p.out = append(p.out, '[')
if len(urlRegexp.FindString(v.str)) == len(v.str) {
p.out = append(p.out, v.str...)
} else {
err = p.marshalString(v)
}
p.out = append(p.out, ']')
return err
case Uint:
return p.marshalNumber(v)
case Name:
s, _ := v.Name()
p.out = append(p.out, s...)
return nil
default:
return errors.New("invalid type %v to encode key", v.Type())
}
}
func (p *encoder) marshalValue(v Value) error {
switch v.Type() {
case Bool, Int, Uint, Float:
return p.marshalNumber(v)
case String:
return p.marshalString(v)
case List:
return p.marshalList(v)
case Message:
return p.marshalMessage(v, true)
case Name:
s, _ := v.Name()
p.out = append(p.out, s...)
return nil
default:
return errors.New("invalid type %v to encode value", v.Type())
}
}