A Decoder interprets CUE expressions as JSON protobuf encodings based on an underlying schema.
It bases the mapping on the underlying CUE type, without consulting Protobuf attributes.
Mappings per CUE type:
for any CUE type: null is omitted if null is not specifically allowed. bytes: if the expression is a string, it is reinterpreted using a base64 encoding. Either standard or URL-safe base64 encoding with/without paddings are accepted. int: string values are interpreted as integers float: string values are interpreted as numbers, and the values "NaN", "Infinity", and "-Infinity" are allowed and converted to corresponding error values. enums: if a field is of type int and does not have a standard integer type for its @protobuf attribute, this is assumed to represent a protobuf enum value. Enum names are converted to integers by interpreting the definitions of the disjunction constants as the symbol names. If CUE uses the string representation for enums, then an #enumValue integer associated with the string value is used for the conversion. {}: 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. time.Time / time.Duration: left as is _: left as is.
type Decoder struct {
// contains filtered or unexported fields
}
func NewDecoder(schema cue.Value, options ...Option) *Decoder
NewDecoder creates a Decoder for the given schema.
func (d *Decoder) RewriteExpr(expr ast.Expr) (ast.Expr, error)
RewriteExpr modifies expr, interpreting it in terms of the given schema according to the protocol buffer to JSON mapping defined in the protocol buffer spec.
RewriteExpr is idempotent, calling it multiples times on an expression gives the same result.
func (d *Decoder) RewriteFile(file *ast.File) error
RewriteFile modifies file, interpreting it in terms of the given schema according to the protocol buffer to JSON mapping defined in the protocol buffer spec.
RewriteFile is idempotent, calling it multiples times on an expression gives the same result.
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 {
// contains filtered or unexported fields
}
func NewEncoder(schema cue.Value, options ...Option) *Encoder
NewEncoder creates an Encoder for the given schema.
func (e *Encoder) RewriteExpr(expr ast.Expr) (ast.Expr, error)
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) RewriteFile(file *ast.File) error
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.
Option is an option.
There are currently no options.
type Option func()