1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// 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 jsonpb
import (
"strconv"
"cuelang.org/go/cue"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/token"
"cuelang.org/go/encoding/protobuf/pbinternal"
)
// TODO: Options:
// - Convert integer strings.
// - URL encoder
// - URL decoder
// An Encoder rewrites CUE values according to the Protobuf to JSON mappings,
// based on a given CUE schema.
//
// It bases the mapping on the underlying CUE type, without consulting Protobuf
// attributes.
//
// Mappings per CUE type:
//
// for any CUE type:
// int: if the expression value is an integer and the schema value is
// an int64, it is converted to a string.
// {}: JSON objects representing any values will be left as is.
// If the CUE type corresponding to the URL can be determined within
// the module context it will be unified.
// _: Adds a `@type` URL (TODO).
type Encoder struct {
schema cue.Value
}
// NewEncoder creates an Encoder for the given schema.
func NewEncoder(schema cue.Value, options ...Option) *Encoder {
return &Encoder{schema: schema}
}
// RewriteFile modifies file, modifying it to conform to the Protocol buffer
// to JSON mapping it in terms of the given schema.
//
// RewriteFile is idempotent, calling it multiples times on an expression gives
// the same result.
func (e *Encoder) RewriteFile(file *ast.File) error {
var enc encoder
enc.rewriteDecls(e.schema, file.Decls)
return enc.errs
}
// RewriteExpr modifies file, modifying it to conform to the Protocol buffer
// to JSON mapping it in terms of the given schema.
//
// RewriteExpr is idempotent, calling it multiples times on an expression gives
// the same result.
func (e *Encoder) RewriteExpr(expr ast.Expr) (ast.Expr, error) {
var enc encoder
x := enc.rewrite(e.schema, expr)
return x, enc.errs
}
type encoder struct {
errs errors.Error
}
func (e *encoder) rewriteDecls(schema cue.Value, decls []ast.Decl) {
for _, f := range decls {
field, ok := f.(*ast.Field)
if !ok {
continue
}
sel := cue.Label(field.Label)
if !sel.IsString() {
continue
}
v := schema.LookupPath(cue.MakePath(sel.Optional()))
if !v.Exists() {
continue
}
field.Value = e.rewrite(v, field.Value)
}
}
func (e *encoder) rewrite(schema cue.Value, expr ast.Expr) (x ast.Expr) {
switch x := expr.(type) {
case *ast.ListLit:
for i, elem := range x.Elts {
v := schema.LookupPath(cue.MakePath(cue.Index(i).Optional()))
if !v.Exists() {
break
}
x.Elts[i] = e.rewrite(v, elem)
}
return expr
case *ast.StructLit:
e.rewriteDecls(schema, x.Elts)
return expr
case *ast.BasicLit:
if x.Kind != token.INT {
break
}
info, err := pbinternal.FromValue("", schema)
if err != nil {
break
}
switch info.Type {
case "int64", "fixed64", "sfixed64", "uint64":
b, ok := expr.(*ast.BasicLit)
if schema.IncompleteKind() == cue.IntKind && ok && b.Kind == token.INT {
b.Kind = token.STRING
b.Value = literal.String.Quote(b.Value)
}
case "int32", "fixed32", "sfixed32", "uint32", "float", "double":
case "varint":
default:
if !info.IsEnum {
break
}
i, err := strconv.ParseInt(x.Value, 10, 32)
if err != nil {
break
}
if s := pbinternal.MatchByInt(schema, i); s != "" {
x.Kind = token.STRING
x.Value = literal.String.Quote(s)
}
}
}
return expr
}
|