Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrFailedToMarshal is returned when the function fails to marshal a protobuf message to a DynamoDB attribute value. ErrFailedToMarshal = errors.New("dynabuf: failed to marshal protobuf to DynamoDB attribute value") // ErrFailedToMarshalIntermediary is returned when the function fails to marshal the intermediary map to JSON. ErrFailedToMarshalIntermediary = errors.New("dynabuf: failed to marshal intermediary map to JSON") // ErrFailedToUnmarshalIntermediary is returned when the function fails to unmarshal the DynamoDB attribute value to an intermediary map. ErrFailedToUnmarshalIntermediary = errors.New("dynabuf: failed to unmarshal DynamoDB attribute value to intermediary map") // ErrFailedToUnmarshal is returned when the function fails to unmarshal a DynamoDB attribute value to a protobuf message. ErrFailedToUnmarshal = errors.New("dynabuf: failed to unmarshal DynamoDB attribute value to protobuf") // ErrInvalidInput is returned when the input is not a protobuf message or slice of messages. ErrInvalidInput = errors.New("dynabuf: invalid input, must be a protobuf message or slice of messages") // ErrInvalidOutput is returned when the output is not a pointer to a protobuf message or slice of messages. ErrInvalidOutput = errors.New("dynabuf: invalid output, must be a pointer to a protobuf message or slice of messages") )
Set of error messages that can be returned by the Marshal and Unmarshal functions.
Functions ¶
func Marshal ¶
Marshal returns the DynamoDB attribute value encoding of the given protobuf message or slice of messages. If there are any issues with marshaling, an error is returned.
Protocol Buffer to DynamoDB Attribute Value Marshaling ¶
We use a three-step process to marshal a protobuf message to a DynamoDB attribute value, using JSON as the logical intermediary.
The function first marshals the protobuf message to JSON using the google.golang.org/protobuf/encoding/protojson package.
Then, it unmarshals the JSON to a map using the standard library's encoding/json package.
Finally, it uses the github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue package to marshal the map to a DynamoDB attribute value.
The process is similar for a slice of protobuf messages, but the function iterates over each message in the slice and marshals them individually.
Example ¶
import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"google.golang.org/protobuf/types/known/structpb"
"github.com/picatz/dynabuf"
)
input := &structpb.Struct{
Fields: map[string]*structpb.Value{
"bar": {
Kind: &structpb.Value_StringValue{
StringValue: "hello world",
},
},
},
}
outputAny, _ := dynabuf.Marshal(input)
output := outputAny.(map[string]types.AttributeValue)
Example ¶
package main
import (
"fmt"
dbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/picatz/dynabuf"
"google.golang.org/protobuf/types/known/structpb"
)
func main() {
userOrder := &structpb.Struct{
Fields: map[string]*structpb.Value{
"user": structpb.NewStringValue("user#123"),
"order": structpb.NewStringValue("order#123"),
"status": structpb.NewStringValue("pending"),
"tags": structpb.NewListValue(&structpb.ListValue{
Values: []*structpb.Value{
structpb.NewStringValue("tag-1"),
},
}),
},
}
item, err := dynabuf.Marshal(userOrder)
if err != nil {
panic(err)
}
itemMap := item.(map[string]dbtypes.AttributeValue)
fmt.Println(itemMap["user"].(*dbtypes.AttributeValueMemberS).Value)
fmt.Println(itemMap["order"].(*dbtypes.AttributeValueMemberS).Value)
fmt.Println(itemMap["status"].(*dbtypes.AttributeValueMemberS).Value)
fmt.Println(itemMap["tags"].(*dbtypes.AttributeValueMemberL).Value[0])
}
Output: user#123 order#123 pending &{tag-1 {}}
func Unmarshal ¶
Unmarshal parses the DynamoDB attribute values in av and stores the result in v. v must be a pointer to a single protobuf message or a slice of protobuf messages. If there are any issues with unmarshaling, an error is returned.
DynamoDB Attribute Value to Protocol Buffer Unmarshaling ¶
Similar to marshalling, we use a three-step process to unmarshal a DynamoDB attribute value to a protobuf message, using JSON as the logical intermediary.
The function first unmarshals the DynamoDB attribute value to a map using the github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue package.
Then, it marshals the map to JSON using the standard library's encoding/json package.
Finally, it unmarshals the JSON to a protobuf message using the google.golang.org/protobuf/encoding/protojson package.
The process is similar for a slice of protobuf messages, but the function iterates over each item in the slice and unmarshals them individually.
Example ¶
import (
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"google.golang.org/protobuf/types/known/structpb"
"github.com/picatz/dynabuf"
)
av := map[string]types.AttributeValue{
"bar": &types.AttributeValueMemberS{
Value: "hello world",
},
}
var output structpb.Struct
_ = dynabuf.Unmarshal(av, &output)
Example ¶
package main
import (
"fmt"
dbtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/picatz/dynabuf"
"google.golang.org/protobuf/types/known/structpb"
)
func main() {
userOrderAV := map[string]dbtypes.AttributeValue{
"user": &dbtypes.AttributeValueMemberS{
Value: "user#123",
},
"order": &dbtypes.AttributeValueMemberS{
Value: "order#123",
},
"status": &dbtypes.AttributeValueMemberS{
Value: "pending",
},
"tags": &dbtypes.AttributeValueMemberL{
Value: []dbtypes.AttributeValue{
&dbtypes.AttributeValueMemberS{
Value: "tag-1",
},
},
},
}
userOrderPB := &structpb.Struct{}
err := dynabuf.Unmarshal(userOrderAV, userOrderPB)
if err != nil {
panic(err)
}
fmt.Println(userOrderPB.Fields["user"].GetStringValue())
fmt.Println(userOrderPB.Fields["order"].GetStringValue())
fmt.Println(userOrderPB.Fields["status"].GetStringValue())
fmt.Println(userOrderPB.Fields["tags"].GetListValue().Values[0].GetStringValue())
}
Output: user#123 order#123 pending tag-1
Types ¶
This section is empty.