MCPcopy Create free account
hub / github.com/aws/smithy-go / encodeNumber

Method encodeNumber

document/cbor/encode.go:186–228  ·  view source on GitHub ↗
(rv reflect.Value)

Source from the content-addressed store, hash-verified

184}
185
186func (e *encoder) encodeNumber(rv reflect.Value) (cbor.Value, error) {
187 const tagbigpos = 2
188 const tagbigneg = 3
189 const tagbigfloat = 4
190
191 switch rv.Kind() {
192 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
193 iv := rv.Int()
194 if iv >= 0 {
195 return cbor.EncodeFixedUint(uint64(iv)), nil
196 }
197 return cbor.EncodeFixedNegInt(uint64(-iv)), nil
198 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
199 return cbor.EncodeFixedUint(rv.Uint()), nil
200 case reflect.Float32, reflect.Float64:
201 return cbor.Float64(rv.Float()), nil
202 default:
203 rvt := rv.Type()
204 switch {
205 case rvt.ConvertibleTo(serde.ReflectTypeOf.BigInt):
206 i := rv.Convert(serde.ReflectTypeOf.BigInt).Interface().(big.Int)
207 if i.Sign() > -1 {
208 return &cbor.Tag{
209 ID: tagbigpos,
210 Value: cbor.Slice(i.Bytes()),
211 }, nil
212 }
213
214 biased := new(big.Int).Add(&i, big.NewInt(1))
215 return &cbor.Tag{
216 ID: tagbigneg,
217 Value: cbor.Slice(biased.Bytes()),
218 }, nil
219 case rvt.ConvertibleTo(serde.ReflectTypeOf.BigFloat):
220 // FUTURE(rpc2cbor): when document support is enabled, complete this logic
221 return &cbor.Tag{}, nil
222 default:
223 return nil, &document.InvalidMarshalError{
224 Message: fmt.Sprintf("incompatible type: %s", rvt.String()),
225 }
226 }
227 }
228}

Callers 2

encodeStructMethod · 0.95
encodeScalarMethod · 0.95

Calls 8

EncodeFixedUintTypeAlias · 0.92
EncodeFixedNegIntTypeAlias · 0.92
Float64TypeAlias · 0.92
SliceTypeAlias · 0.92
BytesMethod · 0.65
AddMethod · 0.65
StringMethod · 0.65
FloatMethod · 0.45

Tested by

no test coverage detected