Documentation
¶
Index ¶
- func BigEndian() encodings.Builder
- func LittleEndian() encodings.Builder
- func NewBigInt(numBytes uint, signed bool, intEncoder bigIntEncoder) (encodings.TypeCodec, error)
- func NewString(maxLength uint, encoder encodings.Builder) (encodings.TypeCodec, error)
- type Bool
- type Float32
- type Float64
- type Int16
- type Int32
- type Int64
- type Int8
- type OracleID
- type Uint16
- type Uint32
- type Uint64
- type Uint8
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BigEndian ¶
Example ¶
ExampleBigEndian provides a minimal example of constructing and using a codec.
package main
import (
"context"
"fmt"
"math/big"
"github.com/smartcontractkit/chainlink-common/pkg/codec/encodings"
"github.com/smartcontractkit/chainlink-common/pkg/codec/encodings/binary"
)
func main() {
ctx := context.Background()
typeCodec, _ := binary.BigEndian().BigInt(32, true)
// start with empty encoded bytes
encodedBytes := []byte{}
originalValue := big.NewInt(42)
encodedBytes, _ = typeCodec.Encode(originalValue, encodedBytes) // new encoded bytes are appended to existing
value, _, _ := typeCodec.Decode(encodedBytes)
// originalValue is the same as value
fmt.Printf("%+v == %+v\n", originalValue, value)
// TopLevelCodec is a TypeCodec that has a total size of all encoded elements
tlCodec, _ := encodings.NewStructCodec([]encodings.NamedTypeCodec{{Name: "Value", Codec: typeCodec}})
codec := encodings.CodecFromTypeCodec{"SomeStruct": tlCodec}
type SomeStruct struct {
Value *big.Int
}
originalStruct := SomeStruct{Value: big.NewInt(42)}
encodedStructBytes, _ := codec.Encode(ctx, originalStruct, "SomeStruct")
var someStruct SomeStruct
_ = codec.Decode(ctx, encodedStructBytes, &someStruct, "SomeStruct")
decodedStruct, _ := codec.CreateType("SomeStruct", false)
_ = codec.Decode(ctx, encodedStructBytes, &decodedStruct, "SomeStruct")
// encoded struct is equal to decoded struct using defined type and/or CreateType
fmt.Printf("%+v == %+v == %+v\n", originalStruct, someStruct, decodedStruct)
}
func LittleEndian ¶
Types ¶
type Float32 ¶
type Float32 Uint32
Float32 follows IEEE-754 convention for float32, the specification says what the "high" and "low" bits represent Leaving endianness to specify the byte ordering.
type Float64 ¶
type Float64 Int64
Float64 follows IEEE-754 convention for float64, the specification says what the "high" and "low" bits represent Leaving endianness to specify the byte ordering.
Source Files
¶
Click to show internal directories.
Click to hide internal directories.