godence

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2022 License: MIT Imports: 6 Imported by: 0

README

Godence

codecov Unit Test Go Reference GitHub top language

Get Started

install by go get.

go get github.com/LemonNekoGH/godence
Convert Cadence value to Go value

If you have the following Cadence struct.

pub struct Person {
    pub var age: UInt8
    pub var Name: String
}
import "github.com/LemonNekoGH/godence"

type Person struct {
    Age uint8 `godence:"age"` // you can specify field name in cadence by tag.
    Name string
}

func main() {
    dist := &Person{}
    // Omit: You got a Cadence value from a script or transaction. And return value named 'ret'.
    godence.ToGo(ret, dist)
}
Convert Go value to Cadence value

Convert to Cadecne Struct, Event, Resource is currently not support becase type name of Struct, Event, Resource is unpredictable.
Can i convert to AnyStruct and AnyResource?

Testing

Requirements
  • Flow CLI: Use to emulate flow network.
Steps
  1. Start the flow emulator with command: flow emulator
  2. Open another terminal and run go clean -testcache && go test .

TODO-List: Go to Cadence

  • Documents for Go to Cadence
Integers
  • Go int to Cadence Int
  • Go int8 to Cadence Int8
  • Go int16 to Cadence Int16
  • Go int32 to Cadence Int32
  • Go int64 to Cadence Int64
  • Go *big.Int to Cadence Int128
  • Go *big.Int to Cadence Int256
  • Go uint to Cadence UInt
  • Go uint8 to Cadence UInt8
  • Go uint16 to Cadence UInt16
  • Go uint32 to Cadence UInt32
  • Go uint64 to Cadence UInt64
  • Go *big.Int to Cadence UInt128
  • Go *big.Int to Cadence UInt256
Fixed-Point Numbers
  • Go int64 to Cadence Fix64
  • Go uint64 to Cadence UFix64
Other
  • Go string to Cadence String
  • Go string to Cadence Path
  • Go string to Cadence Address
  • Go bool to Cadence Bool
  • Go slice or array to Cadence Array
  • Go ? to Cadence Struct
  • Go string to Cadence Character
  • Go ? to Cadence Resource
  • Go ? to Cadence Dictionary
  • Go ? to Cadence Event

TODO-List: Cadence to go

  • Documents for Cadence base type to Go.
  • Documents for Cadence complex type to Go.
Integers
  • Cadence Int to Go *big.Int
  • Cadence Int8 to Go int8
  • Cadence Int16 to Go int16
  • Cadence Int32 to Go int32
  • Cadence Int64 to Go int64
  • Cadence Int128 to Go *big.Int
  • Cadence Int256 to Go *big.Int
  • Cadence UInt to Go *big.Int
  • Cadence UInt8 to Go uint8
  • Cadence UInt16 to Go uint16
  • Cadence UInt32 to Go uint32
  • Cadence UInt64 to Go uint64
  • Cadence UInt128 to Go *big.Int
  • Cadence UInt256 to Go *big.Int
Fixed-Point Numbers
  • Cadence Fix64 to Go int64
  • Cadence UFix64 to Go uint64
Other
  • Cadence String to Go string
  • Cadence Path to Go string
  • Cadence Address to Go string or cadence.Address or [8]uint8
  • Cadence Bool to Go bool
  • Cadence Array to Go slice
  • Cadence Struct to Go struct
  • Cadence Character to Go string
  • Cadence Resource to Go struct
  • Cadence Dictionary to Go map
  • Cadence Event to Go struct

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToCadence

func ToCadence(value any) (cadence.Value, error)

ToCadence Convert any go value to cadence value. Type uint64 will convert to UInt64, if you want to convert to UFix64, you should use our UFix64 type.

Example (BaseType)
cadenceValue, err := ToCadence(int(15))
fmt.Printf("type id: %s, err: %v", cadenceValue.Type().ID(), err)
Output:

type id: Int, err: <nil>
Example (BigInt)
cadenceValue, err := ToCadence(big.NewInt(10))
fmt.Printf("type id: %s, err: %v", cadenceValue.Type().ID(), err)
Output:

type id: Int128, err: <nil>
Example (UFix64)
cadenceValue, err := ToCadence(UFix64(15))
fmt.Printf("type id: %s, err: %v", cadenceValue.Type().ID(), err)
Output:

type id: UFix64, err: <nil>

func ToCadenceOptional

func ToCadenceOptional(value any) (cadence.Value, error)

ToCadence Convert any go value to cadence optional value.

func ToGo

func ToGo(value cadence.Value, dist any) (err error)

ToGo. Convert cadence types to go. Param 1: cadence value to convert. Param 2: go pointer. Address convert to string, will have 0x prefix.

Types

type Address

type Address string

helper for Address

type Character

type Character string

helper for Character

type Fix64

type Fix64 int64

helper for fix64

type Path

type Path string

helper for Address

type UFix64

type UFix64 uint64

helper for ufix64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL