Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
encoding/protobuf/textproto: add encoder
Browse files Browse the repository at this point in the history
Doesn't really copy positioning and all comments, but
the textproto API is sort of hit or miss anyway whether
this information is supported at all.
So good enough for now.

Issue #5

Change-Id: Ia0d09a0c4b92756f68c2a09a114311b363fef33a
  • Loading branch information
mpvl committed Apr 15, 2021
1 parent c028026 commit 841567d
Show file tree
Hide file tree
Showing 9 changed files with 496 additions and 1 deletion.
8 changes: 7 additions & 1 deletion encoding/protobuf/pbinternal/attribute.go
Expand Up @@ -16,6 +16,8 @@ package pbinternal

import (
"strings"
"unicode"
"unicode/utf8"

"cuelang.org/go/cue"
)
Expand Down Expand Up @@ -50,6 +52,8 @@ type Info struct {
ValueType ValueType
Type string

IsEnum bool

// For maps only
KeyType ValueType // only for maps
KeyTypeString string
Expand Down Expand Up @@ -97,7 +101,7 @@ func FromValue(name string, v cue.Value) (info Info, err error) {

case cue.StructKind:
if strings.HasPrefix(info.Type, "map[") {
a := strings.SplitN(info.Type[len("map["):], ",", 2)
a := strings.SplitN(info.Type[len("map["):], "]", 2)
info.KeyTypeString = strings.TrimSpace(a[0])
switch info.KeyTypeString {
case "string":
Expand Down Expand Up @@ -133,6 +137,8 @@ func FromValue(name string, v cue.Value) (info Info, err error) {

case cue.IntKind:
info.ValueType = Int
r, _ := utf8.DecodeRuneInString(info.Type)
info.IsEnum = unicode.In(r, unicode.Upper)

case cue.FloatKind, cue.NumberKind:
info.ValueType = Float
Expand Down
44 changes: 44 additions & 0 deletions encoding/protobuf/pbinternal/symbol.go
Expand Up @@ -64,3 +64,47 @@ func matchBySymbol(v cue.Value, name string, x *ast.BasicLit) bool {

return false
}

// MatchByInt finds a symbol for a given enum value and sets it in x.
func MatchByInt(v cue.Value, val int64) string {
if op, a := v.Expr(); op == cue.AndOp {
for _, v := range a {
if s := MatchByInt(v, val); s != "" {
return s
}
}
}
v = cue.Dereference(v)
return matchByInt(v, val)
}

func matchByInt(v cue.Value, val int64) string {
switch op, a := v.Expr(); op {
case cue.OrOp, cue.AndOp:
for _, v := range a {
if s := matchByInt(v, val); s != "" {
return s
}
}

default:
if i, err := v.Int64(); err != nil || i != val {
break
}

_, path := v.ReferencePath()
a := path.Selectors()
if len(a) == 0 {
break
}

sel := a[len(a)-1]
if !sel.IsDefinition() {
break
}

return sel.String()[1:]
}

return ""
}
203 changes: 203 additions & 0 deletions encoding/protobuf/textproto/encoder.go
@@ -0,0 +1,203 @@
// Copyright 2021 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package textproto

import (
"fmt"
"strings"

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/encoding/protobuf/pbinternal"

"github.com/protocolbuffers/txtpbfmt/ast"
pbast "github.com/protocolbuffers/txtpbfmt/ast"
"github.com/protocolbuffers/txtpbfmt/parser"
)

// Encoder marshals CUE into text proto.
//
type Encoder struct {
// Schema
}

// NewEncoder returns a new encoder, where the given options are default
// options.
func NewEncoder(options ...Option) *Encoder {
return &Encoder{}
}

// Encode converts a CUE value to a text proto file.
//
// Fields do not need to have a @protobuf attribute except for in the following
// cases:
//
// - it is explicitly required that only fields with an attribute are exported
// - a struct represents a Protobuf map
// - custom naming
//
func (e *Encoder) Encode(v cue.Value, options ...Option) ([]byte, error) {
n := &pbast.Node{}
enc := &encoder{}

enc.encodeMsg(n, v)

if enc.errs != nil {
return nil, enc.errs
}

// Pretty printing does not do errors, and returns a string (why o why?).
s := parser.Pretty(n.Children, 0)
return []byte(s), nil
}

type encoder struct {
errs errors.Error
}

func (e *encoder) addErr(err error) {
e.errs = errors.Append(e.errs, errors.Promote(err, "textproto"))
}

func (e *encoder) encodeMsg(parent *pbast.Node, v cue.Value) {
i, err := v.Fields()
if err != nil {
e.addErr(err)
return
}
for i.Next() {
v := i.Value()
if !v.IsConcrete() {
continue
}

info, err := pbinternal.FromIter(i)
if err != nil {
e.addErr(err)
}

switch info.CompositeType {
case pbinternal.List:
elems, err := v.List()
if err != nil {
e.addErr(err)
return
}
for first := true; elems.Next(); first = false {
n := &pbast.Node{Name: info.Name}
if first {
copyMeta(n, v)
}
elem := elems.Value()
copyMeta(n, elem)
parent.Children = append(parent.Children, n)
e.encodeValue(n, elem)
}

case pbinternal.Map:
i, err := v.Fields()
if err != nil {
e.addErr(err)
return
}
for first := true; i.Next(); first = false {
n := &pbast.Node{Name: info.Name}
if first {
copyMeta(n, v)
}
parent.Children = append(parent.Children, n)
var key *pbast.Node
switch info.KeyType {
case pbinternal.String, pbinternal.Bytes:
key = pbast.StringNode("key", i.Label())
default:
key = &pbast.Node{
Name: "key",
Values: []*ast.Value{{Value: i.Label()}},
}
}
n.Children = append(n.Children, key)

value := &pbast.Node{Name: "value"}
e.encodeValue(value, i.Value())
n.Children = append(n.Children, value)
}

default:
n := &pbast.Node{Name: info.Name}
copyMeta(n, v)
e.encodeValue(n, v)
// Don't add if there are no values or children.
parent.Children = append(parent.Children, n)
}
}
}

// copyMeta copies metadata from nodes to values.
//
// TODO: also copy positions. The textproto API is rather messy and complex,
// though, and so far it seems to be quite buggy too. Not sure if it is worth
// the effort.
func copyMeta(x *pbast.Node, v cue.Value) {
for _, doc := range v.Doc() {
s := strings.TrimRight(doc.Text(), "\n")
for _, c := range strings.Split(s, "\n") {
x.PreComments = append(x.PreComments, "# "+c)
}
}
}

func (e *encoder) encodeValue(n *pbast.Node, v cue.Value) {
var value string
switch v.Kind() {
case cue.StructKind:
e.encodeMsg(n, v)

case cue.StringKind:
s, err := v.String()
if err != nil {
e.addErr(err)
}
sn := pbast.StringNode("foo", s)
n.Values = append(n.Values, sn.Values...)

case cue.BytesKind:
b, err := v.Bytes()
if err != nil {
e.addErr(err)
}
sn := pbast.StringNode("foo", string(b))
n.Values = append(n.Values, sn.Values...)

case cue.BoolKind:
value = fmt.Sprint(v)
n.Values = append(n.Values, &pbast.Value{Value: value})

case cue.IntKind, cue.FloatKind, cue.NumberKind:
d, _ := v.Decimal()
value := d.String()

if info, _ := pbinternal.FromValue("", v); !info.IsEnum {
} else if i, err := v.Int64(); err != nil {
} else if s := pbinternal.MatchByInt(v, i); s != "" {
value = s
}

n.Values = append(n.Values, &pbast.Value{Value: value})

default:
e.addErr(errors.Newf(v.Pos(), "textproto: unknown type %v", v.Kind()))
}
}
73 changes: 73 additions & 0 deletions encoding/protobuf/textproto/encoder_test.go
@@ -0,0 +1,73 @@
// Copyright 2021 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package textproto_test

import (
"strings"
"testing"

"cuelang.org/go/cue"
"cuelang.org/go/cue/errors"
"cuelang.org/go/encoding/protobuf/textproto"
"cuelang.org/go/internal/cuetest"
"cuelang.org/go/internal/cuetxtar"
)

func TestEncode(t *testing.T) {
test := cuetxtar.TxTarTest{
Root: "./testdata/encoder",
Name: "encode",
Update: cuetest.UpdateGoldenFiles,
}

r := cue.Runtime{}

test.Run(t, func(t *cuetxtar.Test) {
// TODO: use high-level API.

var schema, value cue.Value

for _, f := range t.Archive.Files {
switch {
case strings.HasSuffix(f.Name, ".cue"):
inst, err := r.Compile(f.Name, f.Data)
if err != nil {
t.WriteErrors(errors.Promote(err, "test"))
return
}
switch f.Name {
case "schema.cue":
schema = inst.Value()
case "value.cue":
value = inst.Value()
}
}
}

v := schema.Unify(value)
if err := v.Err(); err != nil {
t.WriteErrors(errors.Promote(err, "test"))
return
}

b, err := textproto.NewEncoder().Encode(v)
if err != nil {
t.WriteErrors(errors.Promote(err, "test"))
return
}
_, _ = t.Write(b)

})
}

0 comments on commit 841567d

Please sign in to comment.