Skip to content

Commit

Permalink
remove idToField
Browse files Browse the repository at this point in the history
  • Loading branch information
billettc committed Jul 24, 2024
1 parent 98dfc24 commit b3eefa7
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 104 deletions.
3 changes: 1 addition & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ type Generator struct {
IdToField map[int64]protobuf.Field
}

func NewGenerator(templatePath string, messages []*protobuf.Message, metadata *types.Metadata, IdToField map[int64]protobuf.Field) *Generator {
func NewGenerator(templatePath string, messages []*protobuf.Message, metadata *types.Metadata) *Generator {
return &Generator{
templatePath: templatePath,
Messages: messages,
Metadata: metadata,
seenFields: make(map[string]bool),
IdToField: IdToField,
}
}

Expand Down
2 changes: 1 addition & 1 deletion generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Test_Generator(t *testing.T) {
return messages[i].FullTypeName() < messages[j].FullTypeName()
})

gen := NewGenerator("../templates/gen_types.go.gotmpl", messages, metadata, mc.TypeConverter.IdToField)
gen := NewGenerator("../templates/gen_types.go.gotmpl", messages, metadata)
err = gen.Generate()
require.NoError(t, err)
}
5 changes: 3 additions & 2 deletions pb/sf/gear/metadata/type/v1/output.pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
package pbgear

import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"

protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
)

const (
Expand Down
38 changes: 19 additions & 19 deletions proto/sf/gear/metadata/type/v1/output.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,30 @@ syntax = "proto3";
package sf.gear.metadata.type.v1;
option go_package = "github.com/streamingfast/firehose-gear/pb/sf/gear/metadata/type/v1;pbgear";

message Balances_Raw {
repeated uint32 value_0 = 1;
}
message Balances_Address20 {
repeated uint32 value_0 = 1;
}
message Balances_CompactString {
string value = 1;
}
message CompactTupleNull {
TupleNull value = 1;
}
message SpCoreCryptoAccountId32 {
repeated uint32 value_0 = 1;
}
message Balances_Id {
SpCoreCryptoAccountId32 value_0 = 1;
}
message TupleNull {
}
message Balances_Index {
CompactTupleNull value_0 = 1;
}
message Balances_Raw {
repeated uint32 value_0 = 1;
}
message Balances_Address20 {
message Balances_Address32 {
repeated uint32 value_0 = 1;
}
message Balances_Dest {
Expand All @@ -22,22 +37,7 @@ message Balances_Dest {
Balances_Address20 address20 = 5;
}
}
message Balances_CompactString {
string value = 1;
}
message SpCoreCryptoAccountId32 {
repeated uint32 value_0 = 1;
}
message Balances_Address32 {
repeated uint32 value_0 = 1;
}
message Balances_TransferKeepAliveCall {
Balances_Dest dest = 1;
Balances_CompactString value = 2;
}
message Balances_Id {
SpCoreCryptoAccountId32 value_0 = 1;
}
message CompactTupleNull {
TupleNull value = 1;
}
3 changes: 0 additions & 3 deletions rpc/convert_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func (mc *MetadataConverter) Convert(blockHash string) (map[string]types.IType,
case 14:
mc.TypeConverter = &TypeConverter{
messages: make(map[string]*protobuf.Message),
IdToField: map[int64]protobuf.Field{},
allMetadataTypes: []substrateTypes.PortableTypeV14{},
}
return mc.TypeConverter.convertTypesFromv14(metadata.AsMetadataV14)
Expand Down Expand Up @@ -89,7 +88,6 @@ func (mc *MetadataConverter) fetchStateMetadata(blockHash string) (*substrateTyp
type TypeConverter struct {
messages map[string]*protobuf.Message
allMetadataTypes []substrateTypes.PortableTypeV14
IdToField map[int64]protobuf.Field
}

func (c *TypeConverter) convertTypesFromv14(metadata substrateTypes.MetadataV14) (map[string]types.IType, error) {
Expand Down Expand Up @@ -496,7 +494,6 @@ func (c *TypeConverter) MessageForVariantTypes(name string, variant substrateTyp

field := c.FieldForType(fieldType, palletNameFromPath(fieldType.Type.Path), callName, fn)
msg.Fields = append(msg.Fields, field)
c.IdToField[idx] = field
}

return msg
Expand Down
162 changes: 107 additions & 55 deletions templates/gen_types.go
Original file line number Diff line number Diff line change
@@ -1,81 +1,133 @@
package gen_types

import (
"github.com/centrifuge/go-substrate-rpc-client/v4/registry"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
pbgear "github.com/streamingfast/firehose-gear/pb/sf/gear/metadata/type/v1"
"github.com/centrifuge/go-substrate-rpc-client/v4/registry"
pbgear "github.com/streamingfast/firehose-gear/pb/sf/gear/metadata/type/v1"
)

// type regEntry struct{
// fn func(fields []*registry.DecodedField) any
// returnType reflect.Type
// }
//
// var reg map[int64]*regEntry
//
// func init() {
// reg = make(map[int64]*regEntry)
// reg[0] = &regEntry{
// fn: to_SpCoreCryptoAccountId32,
// }
// }
func To_Balances_TransferKeepAliveCall(fields []*registry.DecodedField) any {
out := &pbgear.Balances_TransferKeepAliveCall{}
f := fields[0].Value.(registry.DecodedFields)

out.Dest = to_Balances_Dest([]*registry.DecodedField(f))
//out.Value = to_Balances_CompactString(fields[1].Value.([]*registry.DecodedField))
return out
}

func to_Balances_Dest(fields []*registry.DecodedField) *pbgear.Balances_Dest {
out := &pbgear.Balances_Dest{}

//===========
switch {
func to_Balances_Address20(fields []*registry.DecodedField) any {
out := &pbgear.Balances_Address20{}
out.Value_0 = to_repeated_uint32(fields[0].Value.([]*registry.DecodedField))
return out
}

case matchFields(fields, []int64{0}):
out.Value = &pbgear.Balances_Dest_Id{
Id: to_Balances_Id(fields),
} //Passthrough fields...
func to_repeated_uint32(fields []*registry.DecodedField) []uint32 {
out := make([]uint32, len(fields))
for _, f := range fields {
out = append(out, f.Value.(uint32))
}
//============

return out
}
}


func to_Balances_Id(fields []*registry.DecodedField) *pbgear.Balances_Id {
out := &pbgear.Balances_Id{}
out.Value_0 = to_SpCoreCryptoAccountId32(fields)
return out
func to_Balances_Address32(fields []*registry.DecodedField) any {
out := &pbgear.Balances_Address32{}
out.Value_0 = to_repeated_uint32(fields[0].Value.([]*registry.DecodedField))
return out
}


func to_Balances_CompactString(fields []*registry.DecodedField) any {
out := &pbgear.Balances_CompactString{}
out.Value = fields[0].Value.(string)
return out
}


func to_Balances_Dest(fields []*registry.DecodedField) any {
out := &pbgear.Balances_Dest{}


//===========
switch {

case matchFields(fields, []int64{0}):
out.Value = to_Balances_Id(fields) //Passthrough fields...

case matchFields(fields, []int64{95}):
out.Value = to_Balances_Index(fields) //Passthrough fields...

case matchFields(fields, []int64{13}):
out.Value = to_Balances_Raw(fields) //Passthrough fields...

case matchFields(fields, []int64{1}):
out.Value = to_Balances_Address32(fields) //Passthrough fields...

case matchFields(fields, []int64{96}):
out.Value = to_Balances_Address20(fields) //Passthrough fields...
}
//============
return out
}

func to_SpCoreCryptoAccountId32(fields []*registry.DecodedField) *pbgear.SpCoreCryptoAccountId32 {
out := &pbgear.SpCoreCryptoAccountId32{}
f := fields[0].Value.(registry.DecodedFields)
out.Value_0 = to_repeated_uint32(f)
func to_oneof_Balances_Value(fields []*registry.DecodedField) any {IdIndexRawAddress32Address20
var out *&pbgear.Balances_Value
out.Value = to_oneof_Balances_Value(fields) //Passthrough fields...
return out
}
}


func to_Balances_Id(fields []*registry.DecodedField) any {
out := &pbgear.Balances_Id{}
out.Value_0 = to_SpCoreCryptoAccountId32(fields[0].Value.([]*registry.DecodedField))
return out
}


func to_Balances_Index(fields []*registry.DecodedField) any {
out := &pbgear.Balances_Index{}
out.Value_0 = to_CompactTupleNull(fields[0].Value.([]*registry.DecodedField))
return out
}


func to_Balances_Raw(fields []*registry.DecodedField) any {
out := &pbgear.Balances_Raw{}
out.Value_0 = to_repeated_uint32(fields[0].Value.([]*registry.DecodedField))
return out
}


func to_Balances_TransferKeepAliveCall(fields []*registry.DecodedField) any {
out := &pbgear.Balances_TransferKeepAliveCall{}
out.Dest = to_Balances_Dest(fields[0].Value.([]*registry.DecodedField))
out.Value = to_Balances_CompactString(fields[1].Value.([]*registry.DecodedField))
return out
}



func to_CompactTupleNull(fields []*registry.DecodedField) any {
out := &pbgear.CompactTupleNull{}
out.Value = to_TupleNull(fields[0].Value.([]*registry.DecodedField))
return out
}


func to_SpCoreCryptoAccountId32(fields []*registry.DecodedField) any {
out := &pbgear.SpCoreCryptoAccountId32{}
out.Value_0 = to_repeated_uint32(fields[0].Value.([]*registry.DecodedField))
return out
}


func to_TupleNull(fields []*registry.DecodedField) any {
out := &pbgear.TupleNull{}
return out
}

func to_repeated_uint32(fields []*registry.DecodedField) []uint32 {
out := make([]uint32, len(fields))

data := fields[0].Value.([]interface{})
for _, f := range data {
u := uint8(f.(types.U8))
out = append(out, uint32(u))
}
return out
}

func matchFields(fields []*registry.DecodedField, ids []int64) bool {
if len(fields) != len(ids) {
return false
}
for i, f := range fields {
if ids[i] != f.LookupIndex {
if ids[i] != f.LookupIndex{
return false
}
}
return true
}
}
22 changes: 0 additions & 22 deletions templates/gen_types.go.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,11 @@ package gen_types
import (
"github.com/centrifuge/go-substrate-rpc-client/v4/registry"
pbgear "github.com/streamingfast/firehose-gear/pb/sf/gear/metadata/type/v1"
"reflect"
)

{{- $generator := . }}
{{- $metadata := $generator.Metadata }}
{{- $messages := $generator.Messages }}
{{- $idToField := $generator.IdToField }}

type regEntry struct{
fn func(fields []*registry.DecodedField) any
returnType reflect.Type
}

var reg map[int64]*regEntry

func init() {
reg = make(map[int64]*regEntry)
{{- range $k, $f:= $idToField}}
reg[{{$k}}] = &regEntry{
fn: {{$f.ToFuncName $metadata}},
{{/* returnType: "{{$f.ToGoTypeName $metadata}}", */}}
}
{{- end}}
}


{{ range $msg := $messages }}

Expand Down Expand Up @@ -126,11 +106,9 @@ func {{ $field.ToFuncName $meta}}(fields []*registry.DecodedField) {{ $field.Ret
{{- $goType := $field.ToGoTypeName $meta }}

func {{ $field.ToFuncName $meta}}(fields []*registry.DecodedField) any {
===========
{{- range $t := $field.Types }}
{{- $t.Name}}
{{- end }}
==========
var out *{{ $field.OutputType $meta }}
out.Value = {{ $field.ToFuncName $meta}}(fields) //Passthrough fields...
return out
Expand Down

0 comments on commit b3eefa7

Please sign in to comment.