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 interface{}, map[string]interface{}, and []interface{}.
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 interface{}, map[string]interface{}, and []interface{}
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]interface{}{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 27,
"address": map[string]interface{}{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100",
},
"phoneNumbers": []interface{}{
map[string]interface{}{
"type": "home",
"number": "212 555-1234",
},
map[string]interface{}{
"type": "office",
"number": "646 555-4567",
},
},
"children": []interface{}{},
"spouse": nil,
})
if err != nil {
... // handle error
}
... // make use of m as a *structpb.Value
Package-Level Type Names (total 11, in which 10 are exported)
Package-Level Functions (total 12, in which 9 are exported)
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, int32, int64 │ stored as NumberValue ║
║ uint, uint32, uint64 │ stored as NumberValue ║
║ float32, float64 │ stored as NumberValue ║
║ string │ stored as StringValue; must be valid UTF-8 ║
║ []byte │ stored as StringValue; base64-encoded ║
║ map[string]interface{} │ stored as StructValue ║
║ []interface{} │ stored as ListValue ║
╚════════════════════════╧════════════════════════════════════════════╝
When converting an int64 or uint64 to a NumberValue, numeric precision loss
is possible since they are stored as a float64.
Package-Level Constants (only one, which is exported)
Null value.
The pages are generated with Goldsv0.3.2-preview. (GOOS=darwin 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 @Go100and1 (reachable from the left QR code) to get the latest news of Golds.