Skip to content

Commit

Permalink
combine bindings into single struct
Browse files Browse the repository at this point in the history
  • Loading branch information
EasterTheBunny committed Mar 13, 2024
1 parent 1659783 commit eafa22b
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions pkg/solana/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package solana
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"

Expand Down Expand Up @@ -64,8 +65,39 @@ func (b namespaceBindings) CreateType(namespace, methodName string, forEncoding
return bindings[0].CreateType(forEncoding)
}

// default to map when multiple bindings exist
return &map[string]any{}, nil
// build a merged struct from all bindings
fields := make([]reflect.StructField, 0)
var fieldIdx int

for _, binding := range bindings {
bindingType, err := binding.CreateType(forEncoding)
if err != nil {
return nil, err
}

tBinding := reflect.TypeOf(bindingType)

// all bindings must be structs to allow multiple bindings
if tBinding.Kind() != reflect.Struct {
return nil, fmt.Errorf("%w: support for multiple bindings only applies to all bindings having the type struct", types.ErrInvalidType)
}

for idx := 0; idx < tBinding.NumField(); idx++ {
value := tBinding.FieldByIndex([]int{idx})

field := reflect.StructField{
Name: value.Name,
Type: value.Type,
Index: []int{fieldIdx},
}

fields = append(fields, field)

fieldIdx++
}
}

return reflect.New(reflect.StructOf(fields)).Interface(), nil
}

func (b namespaceBindings) Bind(boundContracts []types.BoundContract) error {
Expand Down

0 comments on commit eafa22b

Please sign in to comment.