jsonpb

package
v0.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package jsonpb rewrites a CUE expression based upon the Protobuf interpretation of JSON.

API Status: DRAFT: API may change without notice.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

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
           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.

func NewDecoder

func NewDecoder(schema cue.Value, options ...Option) *Decoder

NewDecoder creates a Decoder for the given schema.

func (*Decoder) RewriteExpr

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 (*Decoder) RewriteFile

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.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

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).

func NewEncoder

func NewEncoder(schema cue.Value, options ...Option) *Encoder

NewEncoder creates an Encoder for the given schema.

func (*Encoder) RewriteExpr

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 (*Encoder) RewriteFile

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.

type Option

type Option func()

Option is an option.

There are currently no options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL