Involved Source Files Package structpb contains generated types for google/protobuf/struct.proto.
The messages (i.e., Value, Struct, and ListValue) defined in struct.proto are
used to represent arbitrary JSON. The Value message represents a JSON value,
the Struct message represents a JSON object, and the ListValue message
represents a JSON array. See https://json.org for more information.
The Value, Struct, and ListValue types have generated MarshalJSON and
UnmarshalJSON methods such that they serialize JSON equivalent to what the
messages themselves represent. Use of these types with the
"google.golang.org/protobuf/encoding/protojson" package
ensures that they will be serialized as their JSON equivalent.
# Conversion to and from a Go interface
The standard Go "encoding/json" package has functionality to serialize
arbitrary types to a large degree. The Value.AsInterface, Struct.AsMap, and
ListValue.AsSlice methods can convert the protobuf message representation into
a form represented by any, map[string]any, and []any.
This form can be used with other packages that operate on such data structures
and also directly with the standard json package.
In order to convert the any, map[string]any, and []any
forms back as Value, Struct, and ListValue messages, use the NewStruct,
NewList, and NewValue constructor functions.
# Example usage
Consider the following example JSON object:
{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}
To construct a Value message representing the above JSON object:
m, err := structpb.NewValue(map[string]any{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": map[string]any{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100",
},
"phoneNumbers": []any{
map[string]any{
"type": "home",
"number": "212 555-1234",
},
map[string]any{
"type": "office",
"number": "646 555-4567",
},
},
"children": []any{},
"spouse": nil,
})
if err != nil {
... // handle error
}
... // make use of m as a *structpb.Value
Package-Level Type Names (total 10)
/* sort by: | */
`ListValue` is a wrapper around a repeated field of values.
The JSON representation for `ListValue` is JSON array. Repeated field of dynamically typed values. AsSlice converts x to a general-purpose Go slice.
The slice elements are converted by calling Value.AsInterface. Deprecated: Use ListValue.ProtoReflect.Descriptor instead.(*ListValue) GetValues() []*Value(*ListValue) MarshalJSON() ([]byte, error)(*ListValue) ProtoMessage()(*ListValue) ProtoReflect() protoreflect.Message(*ListValue) Reset()(*ListValue) String() string(*ListValue) UnmarshalJSON(b []byte) error
*ListValue : google.golang.org/protobuf/reflect/protoreflect.ProtoMessage
*ListValue : google.golang.org/protobuf/runtime/protoiface.MessageV1
*ListValue : encoding/json.Marshaler
*ListValue : encoding/json.Unmarshaler
*ListValue : expvar.Var
*ListValue : fmt.Stringer
*ListValue : github.com/goccy/go-json.Marshaler
*ListValue : github.com/goccy/go-json.Unmarshaler
*ListValue : github.com/gogo/protobuf/proto.Message
func NewList(v []any) (*ListValue, error)
func (*Value).GetListValue() *ListValue
func NewListValue(v *ListValue) *Value
`Struct` represents a structured data value, consisting of fields
which map to dynamically typed values. In some languages, `Struct`
might be supported by a native representation. For example, in
scripting languages like JS a struct is represented as an
object. The details of that representation are described together
with the proto support for the language.
The JSON representation for `Struct` is JSON object. Unordered map of dynamically typed values. AsMap converts x to a general-purpose Go map.
The map values are converted by calling Value.AsInterface. Deprecated: Use Struct.ProtoReflect.Descriptor instead.(*Struct) GetFields() map[string]*Value(*Struct) MarshalJSON() ([]byte, error)(*Struct) ProtoMessage()(*Struct) ProtoReflect() protoreflect.Message(*Struct) Reset()(*Struct) String() string(*Struct) UnmarshalJSON(b []byte) error
*Struct : google.golang.org/protobuf/reflect/protoreflect.ProtoMessage
*Struct : google.golang.org/protobuf/runtime/protoiface.MessageV1
*Struct : encoding/json.Marshaler
*Struct : encoding/json.Unmarshaler
*Struct : expvar.Var
*Struct : fmt.Stringer
*Struct : github.com/goccy/go-json.Marshaler
*Struct : github.com/goccy/go-json.Unmarshaler
*Struct : github.com/gogo/protobuf/proto.Message
func NewStruct(v map[string]any) (*Struct, error)
func (*Value).GetStructValue() *Struct
func NewStructValue(v *Struct) *Value
`Value` represents a dynamically typed value which can be either
null, a number, a string, a boolean, a recursive struct value, or a
list of values. A producer of value is expected to set one of these
variants. Absence of any variant indicates an error.
The JSON representation for `Value` is JSON value. The kind of value.
Types that are valid to be assigned to Kind:
*Value_NullValue
*Value_NumberValue
*Value_StringValue
*Value_BoolValue
*Value_StructValue
*Value_ListValue AsInterface converts x to a general-purpose Go interface.
Calling Value.MarshalJSON and "encoding/json".Marshal on this output produce
semantically equivalent JSON (assuming no errors occur).
Floating-point values (i.e., "NaN", "Infinity", and "-Infinity") are
converted as strings to remain compatible with MarshalJSON. Deprecated: Use Value.ProtoReflect.Descriptor instead.(*Value) GetBoolValue() bool(*Value) GetKind() isValue_Kind(*Value) GetListValue() *ListValue(*Value) GetNullValue() NullValue(*Value) GetNumberValue() float64(*Value) GetStringValue() string(*Value) GetStructValue() *Struct(*Value) MarshalJSON() ([]byte, error)(*Value) ProtoMessage()(*Value) ProtoReflect() protoreflect.Message(*Value) Reset()(*Value) String() string(*Value) UnmarshalJSON(b []byte) error
*Value : google.golang.org/protobuf/reflect/protoreflect.ProtoMessage
*Value : google.golang.org/protobuf/runtime/protoiface.MessageV1
*Value : encoding/json.Marshaler
*Value : encoding/json.Unmarshaler
*Value : expvar.Var
*Value : fmt.Stringer
*Value : github.com/goccy/go-json.Marshaler
*Value : github.com/goccy/go-json.Unmarshaler
*Value : github.com/gogo/protobuf/proto.Message
func NewBoolValue(v bool) *Value
func NewListValue(v *ListValue) *Value
func NewNullValue() *Value
func NewNumberValue(v float64) *Value
func NewStringValue(v string) *Value
func NewStructValue(v *Struct) *Value
func NewValue(v any) (*Value, error)
func (*ListValue).GetValues() []*Value
func (*Struct).GetFields() map[string]*Value
Represents a boolean value.
Represents a repeated `Value`.
Represents a null value.
Represents a double value.
Represents a string value.
Represents a structured value.
Package-Level Functions (total 9)
NewBoolValue constructs a new boolean Value.
NewList constructs a ListValue from a general-purpose Go slice.
The slice elements are converted using NewValue.
NewListValue constructs a new list Value.
NewNullValue constructs a new null Value.
NewNumberValue constructs a new number Value.
NewStringValue constructs a new string Value.
NewStruct constructs a Struct from a general-purpose Go map.
The map keys must be valid UTF-8.
The map values are converted using NewValue.
NewStructValue constructs a new struct Value.
NewValue constructs a Value from a general-purpose Go interface.
╔═══════════════════════════════════════╤════════════════════════════════════════════╗
║ Go type │ Conversion ║
╠═══════════════════════════════════════╪════════════════════════════════════════════╣
║ nil │ stored as NullValue ║
║ bool │ stored as BoolValue ║
║ int, int8, int16, int32, int64 │ stored as NumberValue ║
║ uint, uint8, uint16, uint32, uint64 │ stored as NumberValue ║
║ float32, float64 │ stored as NumberValue ║
║ json.Number │ stored as NumberValue ║
║ string │ stored as StringValue; must be valid UTF-8 ║
║ []byte │ stored as StringValue; base64-encoded ║
║ map[string]any │ stored as StructValue ║
║ []any │ stored as ListValue ║
╚═══════════════════════════════════════╧════════════════════════════════════════════╝
When converting an int64 or uint64 to a NumberValue, numeric precision loss
is possible since they are stored as a float64.
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.