src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

serde.go (5563B)


      1 package smithy
      2 
      3 import (
      4 	"fmt"
      5 	"io"
      6 	"math/big"
      7 	"time"
      8 
      9 	"github.com/aws/smithy-go/document"
     10 )
     11 
     12 // ShapeSerializer implements the marshaling of an in-code representation of a
     13 // shape to an unspecified data format, which is determined by the
     14 // implementation.
     15 //
     16 // A ShapeSerializer is consumed by the **code-generated** Serialize() method
     17 // of a modeled structure. For example:
     18 //
     19 //	func (v *PutItemInput) Serialize(s smithy.ShapeSerializer) {
     20 //		s.WriteStruct(schemas.PutItemInput)
     21 //		v.SerializeMembers(s)
     22 //		s.CloseStruct()
     23 //	}
     24 //
     25 //	func (v *PutItemInput) SerializeMembers(s smithy.ShapeSerializer) {
     26 //		if v.TableName != nil {
     27 //			s.WriteString(schemas.PutItemInput_TableName, *v.TableName)
     28 //		}
     29 //		if v.Item != nil {
     30 //			serializeAttributeMap(s, schemas.PutItemInput_Item, v.Item)
     31 //		}
     32 //		// ...
     33 //	}
     34 type ShapeSerializer interface {
     35 	Bytes() []byte
     36 
     37 	WriteInt8(*Schema, int8)
     38 	WriteInt16(*Schema, int16)
     39 	WriteInt32(*Schema, int32)
     40 	WriteInt64(*Schema, int64)
     41 	WriteFloat32(*Schema, float32)
     42 	WriteFloat64(*Schema, float64)
     43 	WriteBool(*Schema, bool)
     44 	WriteString(*Schema, string)
     45 	WriteBigInt(*Schema, *big.Int)
     46 	WriteBigFloat(*Schema, *big.Float)
     47 	WriteBlob(*Schema, []byte)
     48 	WriteTime(*Schema, time.Time)
     49 
     50 	WriteUnion(schema, variant *Schema)
     51 	CloseUnion()
     52 	WriteDocument(*Schema, document.Value)
     53 	WriteNil(*Schema)
     54 
     55 	WriteStruct(*Schema)
     56 	CloseStruct()
     57 
     58 	WriteList(*Schema)
     59 	CloseList()
     60 
     61 	WriteMap(*Schema)
     62 	WriteKey(*Schema, string)
     63 	CloseMap()
     64 }
     65 
     66 // ShapeDeserializer implements the unmarshaling from some unspecified data
     67 // format to an in-code representation of a shape, which is determined by the
     68 // implementation.
     69 type ShapeDeserializer interface {
     70 	ReadInt8(*Schema, *int8) error
     71 	ReadInt16(*Schema, *int16) error
     72 	ReadInt32(*Schema, *int32) error
     73 	ReadInt64(*Schema, *int64) error
     74 	ReadFloat32(*Schema, *float32) error
     75 	ReadFloat64(*Schema, *float64) error
     76 	ReadBool(*Schema, *bool) error
     77 	ReadString(*Schema, *string) error
     78 	ReadBlob(*Schema, *[]byte) error
     79 	ReadTime(*Schema, *time.Time) error
     80 	ReadBigInt(*Schema, *big.Int) error
     81 	ReadBigFloat(*Schema, *big.Float) error
     82 	ReadNil(*Schema) (bool, error)
     83 
     84 	ReadStruct(*Schema) error
     85 	ReadStructMember() (*Schema, error)
     86 
     87 	ReadUnion(*Schema) (*Schema, error)
     88 	ReadDocument(*Schema, *document.Value) error
     89 
     90 	ReadList(*Schema) error
     91 	ReadListItem(*Schema) (hasMoreElements bool, err error)
     92 
     93 	ReadMap(*Schema) error
     94 	ReadMapKey(*Schema) (key string, hasMoreElements bool, err error)
     95 }
     96 
     97 // Serializable is an entity that can describe itself to a ShapeSerializer to
     98 // be encoded to some format.
     99 //
    100 // Unlike the standard library marshaler interfaces, which idiomatically encode
    101 // to []byte, the output format and data type here is not specified at all.
    102 // This is because Smithy shapes need to encode to a variety of formats or data
    103 // carriers. For example, HTTP-binding JSON protocols need to serialize some
    104 // members to bytes (the HTTP request body) and others directly to fields on
    105 // the HTTP request itself (e.g. headers).
    106 type Serializable interface {
    107 	Serialize(ShapeSerializer)
    108 }
    109 
    110 // StreamingInput is implemented by input types that have a streaming blob
    111 // payload (an io.Reader member with @httpPayload + @streaming).
    112 type StreamingInput interface {
    113 	GetPayloadStream() io.Reader
    114 }
    115 
    116 // StreamingOutput is implemented by output types that have a streaming blob
    117 // payload (an io.ReadCloser member with @httpPayload + @streaming).
    118 type StreamingOutput interface {
    119 	SetPayloadStream(io.ReadCloser)
    120 }
    121 
    122 // Deserializable is an entity that can unmarshal itself from a
    123 // ShapeDeserializer.
    124 type Deserializable interface {
    125 	Deserialize(ShapeDeserializer) error
    126 }
    127 
    128 // DeserializableError is implemented by modeled error types for a service.
    129 type DeserializableError interface {
    130 	Deserializable
    131 	error
    132 }
    133 
    134 // ReadUnion is a utility API for generated clients.
    135 func ReadUnion(d ShapeDeserializer, schema *Schema, memberFn func(*Schema) error) error {
    136 	ms, err := d.ReadUnion(schema)
    137 	if ms == nil || err != nil {
    138 		return err
    139 	}
    140 
    141 	if err := memberFn(ms); err != nil {
    142 		return err
    143 	}
    144 
    145 	for {
    146 		ms, err = d.ReadUnion(schema)
    147 		if err != nil {
    148 			return err
    149 		}
    150 		if ms == nil {
    151 			return nil
    152 		}
    153 		return fmt.Errorf("union has more than one non-nil member: %s", ms.MemberName())
    154 	}
    155 }
    156 
    157 // ReadStruct is a utility API for generated clients.
    158 func ReadStruct(d ShapeDeserializer, schema *Schema, memberFn func(*Schema) error) error {
    159 	if err := d.ReadStruct(schema); err != nil {
    160 		return err
    161 	}
    162 
    163 	for {
    164 		ms, err := d.ReadStructMember()
    165 		if err != nil {
    166 			return err
    167 		}
    168 
    169 		if ms == nil {
    170 			return nil
    171 		}
    172 
    173 		if err := memberFn(ms); err != nil {
    174 			return err
    175 		}
    176 	}
    177 }
    178 
    179 // ReadList is a utility API for generated clients.
    180 func ReadList(d ShapeDeserializer, schema *Schema, memberFn func() error) error {
    181 	if err := d.ReadList(schema); err != nil {
    182 		return err
    183 	}
    184 
    185 	var memberSchema *Schema
    186 	if schema != nil {
    187 		memberSchema = schema.ListMember()
    188 	}
    189 
    190 	for {
    191 		ok, err := d.ReadListItem(memberSchema)
    192 		if !ok {
    193 			return nil
    194 		}
    195 		if err != nil {
    196 			return err
    197 		}
    198 
    199 		if err := memberFn(); err != nil {
    200 			return err
    201 		}
    202 	}
    203 }
    204 
    205 // ReadMap is a utility API for generated clients.
    206 func ReadMap(d ShapeDeserializer, schema *Schema, memberFn func(string) error) error {
    207 	if err := d.ReadMap(schema); err != nil {
    208 		return err
    209 	}
    210 
    211 	var keySchema *Schema
    212 	if schema != nil {
    213 		keySchema = schema.MapKey()
    214 	}
    215 
    216 	for {
    217 		k, ok, err := d.ReadMapKey(keySchema)
    218 		if !ok {
    219 			return nil
    220 		}
    221 		if err != nil {
    222 			return err
    223 		}
    224 
    225 		if err := memberFn(k); err != nil {
    226 			return err
    227 		}
    228 	}
    229 }