Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
name: Build and Publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-go@v1
- uses: actions/setup-go@v5
with:
go-version: 1.21

Expand Down
59 changes: 59 additions & 0 deletions tools/gen_registry/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"fmt"
"go/format"
"os"
"path/filepath"
"regexp"
"strings"
)

func main() {
root, err := os.Getwd()
if err != nil {
panic(err)
}

registryPath := filepath.Join(root, "registry_types.go")
content, err := os.ReadFile(registryPath)
if err != nil {
panic(err)
}

re := regexp.MustCompile(`"([A-Za-z0-9_]+)"`)
matches := re.FindAllStringSubmatch(string(content), -1)
if len(matches) == 0 {
panic("no type names found in registry_types.go")
}

seen := map[string]bool{}
names := make([]string, 0, len(matches))
for _, match := range matches {
name := match[1]
if seen[name] {
continue
}
seen[name] = true
names = append(names, name)
}

var out strings.Builder
out.WriteString("// Code generated by tools/gen_registry; DO NOT EDIT.\n\n")
out.WriteString("package types\n\n")
out.WriteString("var baseCodecFactories = map[string]CodecFactory{\n")
for _, name := range names {
out.WriteString(fmt.Sprintf("\t%q: func() Decoder { return &%s{} },\n", strings.ToLower(name), name))
}
out.WriteString("}\n")

formatted, err := format.Source([]byte(out.String()))
if err != nil {
panic(err)
}

outPath := filepath.Join(root, "registry_gen.go")
if err := os.WriteFile(outPath, formatted, 0o644); err != nil {
panic(err)
}
}
8 changes: 6 additions & 2 deletions types/Bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ func (b *Bool) Process() {
b.Value = b.getNextBool()
}

func (b *Bool) Encode(value bool) string {
if value {
func (b *Bool) Encode(value interface{}) string {
v, ok := value.(bool)
if !ok {
panic("invalid bool input")
}
if v {
return "01"
}
return "00"
Expand Down
44 changes: 28 additions & 16 deletions types/Bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ func (b *Bytes) Process() {
}
}

func (b *Bytes) Encode(value string) string {
func (b *Bytes) Encode(value interface{}) string {
valueStr, ok := value.(string)
if !ok {
panic("invalid bytes input")
}
var bytes []byte
if strings.HasPrefix(value, "0x") {
value = utiles.TrimHex(value)
if len(value)%2 == 1 {
value += "0"
if strings.HasPrefix(valueStr, "0x") {
valueStr = utiles.TrimHex(valueStr)
if len(valueStr)%2 == 1 {
valueStr += "0"
}
} else {
value = utiles.BytesToHex([]byte(value))
valueStr = utiles.BytesToHex([]byte(valueStr))
}
bytes = utiles.HexToBytes(value)
return Encode("Compact<u32>", len(bytes)) + value
bytes = utiles.HexToBytes(valueStr)
return Encode("Compact<u32>", len(bytes)) + valueStr
}

func (b *Bytes) TypeStructString() string {
Expand All @@ -41,13 +45,17 @@ func (h *HexBytes) Process() {
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(h.ProcessAndUpdateData("Compact<u32>").(int))))
}

func (h *HexBytes) Encode(value string) string {
value = utiles.TrimHex(value)
if len(value)%2 == 1 {
value += "0"
func (h *HexBytes) Encode(value interface{}) string {
valueStr, ok := value.(string)
if !ok {
panic("invalid hexbytes input")
}
valueStr = utiles.TrimHex(valueStr)
if len(valueStr)%2 == 1 {
valueStr += "0"
}
bytes := utiles.HexToBytes(value)
return Encode("Compact<u32>", len(bytes)) + value
bytes := utiles.HexToBytes(valueStr)
return Encode("Compact<u32>", len(bytes)) + valueStr
}

func (h *HexBytes) TypeStructString() string {
Expand All @@ -56,8 +64,12 @@ func (h *HexBytes) TypeStructString() string {

type String struct{ Bytes }

func (s *String) Encode(value string) string {
bytes := []byte(value)
func (s *String) Encode(value interface{}) string {
valueStr, ok := value.(string)
if !ok {
panic("invalid string input")
}
bytes := []byte(valueStr)
return Encode("Compact<u32>", len(bytes)) + utiles.BytesToHex(bytes)
}

Expand Down
37 changes: 17 additions & 20 deletions types/FixedArray.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package types

import (
"fmt"
"reflect"
"strings"

"github.com/itering/scale.go/types/scaleBytes"
Expand Down Expand Up @@ -49,31 +48,29 @@ func (f *FixedArray) TypeStructString() string {

func (f *FixedArray) Encode(value interface{}) string {
var raw string
if reflect.TypeOf(value).Kind() == reflect.String && value.(string) == "" {
return ""
}
switch reflect.TypeOf(value).Kind() {
case reflect.Slice:
s := reflect.ValueOf(value)
if s.Len() != f.FixedLength {
panic("fixed length not match")
}
subType := f.SubType
for i := 0; i < s.Len(); i++ {
raw += EncodeWithOpt(subType, s.Index(i).Interface(), &ScaleDecoderOption{Spec: f.Spec, Metadata: f.Metadata})
if valueStr, ok := value.(string); ok {
if valueStr == "" {
return ""
}
return raw
case reflect.String:
valueStr := value.(string)
if strings.HasPrefix(valueStr, "0x") {
return utiles.TrimHex(valueStr)
} else {
return utiles.BytesToHex([]byte(valueStr))
}
default:
if f.FixedLength == 1 {
return EncodeWithOpt(f.SubType, value, &ScaleDecoderOption{Spec: f.Spec, Metadata: f.Metadata})
}
values, ok := asInterfaceSlice(value)
if ok {
if len(values) != f.FixedLength {
panic("fixed length not match")
}
subType := f.SubType
for _, item := range values {
raw += EncodeWithOpt(subType, item, &ScaleDecoderOption{Spec: f.Spec, Metadata: f.Metadata})
}
panic(fmt.Errorf("invalid vec input"))
return raw
}
if f.FixedLength == 1 {
return EncodeWithOpt(f.SubType, value, &ScaleDecoderOption{Spec: f.Spec, Metadata: f.Metadata})
}
panic(fmt.Errorf("invalid vec input"))
}
20 changes: 19 additions & 1 deletion types/Option.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,27 @@ func (o *Option) Encode(value interface{}) string {
if v, ok := value.(string); ok && v == "" {
return "00"
}
if utiles.IsNil(value) {
if value == nil {
return "00"
}
switch v := value.(type) {
case []byte:
if v == nil {
return "00"
}
case []interface{}:
if v == nil {
return "00"
}
case map[string]interface{}:
if v == nil {
return "00"
}
case error:
if v == nil {
return "00"
}
}
if o.SubType == "bool" {
if value.(bool) {
return "01"
Expand Down
10 changes: 7 additions & 3 deletions types/Results.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ func (b *Result) Process() {
}
}

func (b *Result) Encode(value map[string]interface{}) string {
func (b *Result) Encode(value interface{}) string {
typed, ok := value.(map[string]interface{})
if !ok {
panic("invalid Result input")
}
subType := strings.Split(b.SubType, ",")
if len(subType) != 2 {
panic("Result subType not illegal")
}
if data, ok := value["Ok"]; ok {
if data, ok := typed["Ok"]; ok {
return "00" + EncodeWithOpt(subType[0], data, &ScaleDecoderOption{Spec: b.Spec, Metadata: b.Metadata})
}
if data, ok := value["Error"]; ok {
if data, ok := typed["Error"]; ok {
return "01" + EncodeWithOpt(subType[1], data, &ScaleDecoderOption{Spec: b.Spec, Metadata: b.Metadata})
}
panic("illegal Result data")
Expand Down
8 changes: 6 additions & 2 deletions types/Struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ func (s *Struct) Process() {
s.Value = result
}

func (s *Struct) Encode(value map[string]interface{}) string {
func (s *Struct) Encode(value interface{}) string {
var raw string
typed, ok := value.(map[string]interface{})
if !ok {
panic("invalid struct input")
}
if s.TypeMapping != nil {
for k, v := range s.TypeMapping.Names {
raw += EncodeWithOpt(s.TypeMapping.Types[k], value[v], &ScaleDecoderOption{Spec: s.Spec, Metadata: s.Metadata})
raw += EncodeWithOpt(s.TypeMapping.Types[k], typed[v], &ScaleDecoderOption{Spec: s.Spec, Metadata: s.Metadata})
}
}
return raw
Expand Down
Loading