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

Commit

Permalink
encoding/protobuf: support integer enums
Browse files Browse the repository at this point in the history
Also support mappings to different representations.

For string disjunctions: associate #enumValue with string

For int disjunctions: map definition references
to symbols.

Change-Id: I7d6dec1798c26a43d40f9c6e8db29b26e8499866
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9402
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Apr 15, 2021
1 parent 38ad7c3 commit dcfff00
Show file tree
Hide file tree
Showing 18 changed files with 723 additions and 151 deletions.
2 changes: 2 additions & 0 deletions cmd/cue/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
flagPath flagName = "path"
flagFiles flagName = "files"
flagProtoPath flagName = "proto_path"
flagProtoEnum flagName = "proto_enum"
flagWithContext flagName = "with-context"
flagOut flagName = "out"
flagOutFile flagName = "outfile"
Expand Down Expand Up @@ -79,6 +80,7 @@ func addOrphanFlags(f *pflag.FlagSet) {
f.Bool(string(flagList), false, "concatenate multiple objects into a list")
f.Bool(string(flagWithContext), false, "import as object with contextual data")
f.StringArrayP(string(flagProtoPath), "I", nil, "paths in which to search for imports")
f.String(string(flagProtoEnum), "int", "mode for rendering enums (int|json)")
f.StringP(string(flagGlob), "n", "", "glob filter for file names")
f.Bool(string(flagMerge), true, "merge non-CUE files")
}
Expand Down
57 changes: 43 additions & 14 deletions cmd/cue/cmd/get_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,12 @@ func (e *extractor) reportDecl(x *ast.GenDecl) (a []cueast.Decl) {
typ := e.pkg.TypesInfo.TypeOf(v.Name)
enums := e.consts[typ.String()]
name := v.Name.Name
mapNamed := false
underlying := e.pkg.TypesInfo.TypeOf(v.Type)
if b, ok := underlying.Underlying().(*types.Basic); ok && b.Kind() != types.String {
mapNamed = true
}

switch tn, ok := e.pkg.TypesInfo.Defs[v.Name].(*types.TypeName); {
case ok:
if altType := e.altType(tn.Type()); altType != nil {
Expand All @@ -695,8 +701,7 @@ func (e *extractor) reportDecl(x *ast.GenDecl) (a []cueast.Decl) {
a = append(a, e.def(x.Doc, name, s, true))
break
}
// TODO: only print original type if value is not marked as enum.
underlying := e.pkg.TypesInfo.TypeOf(v.Type)

f, _ := e.makeField(name, cuetoken.ISA, underlying, x.Doc, true)
a = append(a, f)
cueast.SetRelPos(f, cuetoken.NewSection)
Expand All @@ -708,20 +713,44 @@ func (e *extractor) reportDecl(x *ast.GenDecl) (a []cueast.Decl) {
cueast.AddComment(a[len(a)-1], internal.NewComment(false, enumName))

// Constants are mapped as definitions.
var x cueast.Expr = e.ident(enums[0], true)
cueast.SetRelPos(x, cuetoken.Newline)
for _, v := range enums[1:] {
y := e.ident(v, true)
cueast.SetRelPos(y, cuetoken.Newline)
x = cueast.NewBinExpr(cuetoken.OR, x, y)
var exprs []cueast.Expr
var named []cueast.Decl
for _, v := range enums {
label := cueast.NewString(v)
cueast.SetRelPos(label, cuetoken.Blank)

var x cueast.Expr = e.ident(v, true)
cueast.SetRelPos(x, cuetoken.Newline)
exprs = append(exprs, x)

if !mapNamed {
continue
}

named = append(named, &cueast.Field{
Label: label,
Value: e.ident(v, true),
})
}

addField := func(label string, exprs []cueast.Expr) {
f := &cueast.Field{
Label: cueast.NewIdent(label),
Value: cueast.NewBinExpr(cuetoken.OR, exprs...),
}
cueast.SetRelPos(f, cuetoken.NewSection)
a = append(a, f)
}
// a = append(a, e.def(nil, enumName, x, true))
f := &cueast.Field{
Label: cueast.NewIdent(enumName),
Value: x,

addField(enumName, exprs)
if len(named) > 0 {
f := &cueast.Field{
Label: cueast.NewIdent("#values_" + name),
Value: &cueast.StructLit{Elts: named},
}
cueast.SetRelPos(f, cuetoken.NewSection)
a = append(a, f)
}
a = append(a, f)
cueast.SetRelPos(f, cuetoken.NewSection)
}
}

Expand Down
9 changes: 5 additions & 4 deletions cmd/cue/cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,11 @@ func protoMode(b *buildPlan) error {
}

c := &protobuf.Config{
Root: root,
Module: module,
Paths: b.encConfig.ProtoPath,
PkgName: b.encConfig.PkgName,
Root: root,
Module: module,
Paths: b.encConfig.ProtoPath,
PkgName: b.encConfig.PkgName,
EnumMode: flagProtoEnum.String(b.cmd),
}
if module != "" {
// We only allow imports from packages within the module if an actual
Expand Down
28 changes: 28 additions & 0 deletions cmd/cue/cmd/testdata/script/get_go_types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ const (
High
)

type Level2 Level

const (
AnotherLevel1 Level2 = iota + 4
AnotherLevel2
)

type CustomJSON struct {
}

Expand Down Expand Up @@ -432,6 +439,13 @@ _#internalIdentifier: #Identifier & "internal"
#Medium |
#High

#values_Level: {
Unknown: #Unknown
Low: #Low
Medium: #Medium
High: #High
}

// Block comment.
// Indented.
//
Expand All @@ -443,6 +457,20 @@ _#internalIdentifier: #Identifier & "internal"
#Medium: #Level & 2
#High: #Level & 3

#Level2: #Level // #enumLevel2

#enumLevel2:
#AnotherLevel1 |
#AnotherLevel2

#values_Level2: {
AnotherLevel1: #AnotherLevel1
AnotherLevel2: #AnotherLevel2
}

#AnotherLevel1: #Level2 & 4
#AnotherLevel2: #Level2 & 5

#CustomJSON: _

#CustomYAML: {
Expand Down
17 changes: 17 additions & 0 deletions cmd/cue/cmd/testdata/script/import_proto.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ option go_package = "acme.com/api/mixer/v1";

// Attributes defines attributes.
message Attributes {
enum Type {
TYPE_A = 1;
TYPE_B = 2;
}

// A map of attribute name to its value.
map<string, AttributeValue> attributes = 1;

Expand Down Expand Up @@ -171,6 +176,18 @@ import (

// Attributes defines attributes.
#Attributes: {
#Type:
#TYPE_A |
#TYPE_B

#TYPE_A: 1
#TYPE_B: 2

#Type_value: {
TYPE_A: 1
TYPE_B: 2
}

// A map of attribute name to its value.
attributes?: {
[string]: #AttributeValue
Expand Down
Loading

0 comments on commit dcfff00

Please sign in to comment.