Skip to content

Commit

Permalink
add usage example for Result
Browse files Browse the repository at this point in the history
  • Loading branch information
timwu20 committed Jun 18, 2021
1 parent 22d69ed commit 7be9479
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/scale/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,55 @@ result := scale.NewResult(int32(0), int32(0)
result.Set(scale.Ok, 10)
```

```
import (
"fmt"
"github.com/ChainSafe/gossamer/pkg/scale"
)
func resultExample() {
// pass in zero or non-zero values of the types for Ok and Err cases
res := scale.NewResult(bool(false), string(""))
// set the OK case with a value of true, any values for OK that are not bool will return an error
err := res.Set(scale.OK, true)
if err != nil {
panic(err)
}
bytes, err := scale.Marshal(res)
if err != nil {
panic(err)
}
// [0x00, 0x01]
fmt.Printf("%v\n", bytes)
res1 := scale.NewResult(bool(false), string(""))
err = scale.Unmarshal(bytes, &res1)
if err != nil {
panic(err)
}
// res1 should be Set with OK mode and value of true
ok, err := res1.Unwrap()
if err != nil {
panic(err)
}
switch ok := ok.(type) {
case bool:
if !ok {
panic(fmt.Errorf("unexpected ok value: %v", ok))
}
default:
panic(fmt.Errorf("unexpected type: %T", ok))
}
}
```

### Varying Data Type

A `VaryingDataType` is analogous to a Rust enum. A `VaryingDataType` needs to be registered using the `RegisterVaryingDataType` function with its associated `VaryingDataTypeValue` types. `VaryingDataTypeValue` is an
Expand Down

0 comments on commit 7be9479

Please sign in to comment.