Skip to content

Commit

Permalink
Added helpers in ABI type to EncodeTable and Encode (any valid st…
Browse files Browse the repository at this point in the history
…ruct)
  • Loading branch information
Matthieu Vachon committed Apr 1, 2020
1 parent 4a80cd6 commit 21f9a14
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions abiencoder.go
Expand Up @@ -24,7 +24,6 @@ type ABIEncoder struct {
}

func (a *ABI) EncodeAction(actionName ActionName, json []byte) ([]byte, error) {

action := a.ActionForName(actionName)
if action == nil {
return nil, fmt.Errorf("encode action: action %s not found in abi", actionName)
Expand All @@ -40,23 +39,50 @@ func (a *ABI) EncodeAction(actionName ActionName, json []byte) ([]byte, error) {
return buffer.Bytes(), nil
}

func (a *ABI) encode(binaryEncoder *Encoder, structureName string, json []byte) error {
func (a *ABI) EncodeTable(tableName TableName, json []byte) ([]byte, error) {
table := a.TableForName(tableName)
if table == nil {
return nil, fmt.Errorf("encode table: table %s not found in abi", tableName)
}

var buffer bytes.Buffer
encoder := NewEncoder(&buffer)

err := a.encode(encoder, table.Type, json)
if err != nil {
return nil, fmt.Errorf("encode table: %s", err)
}
return buffer.Bytes(), nil
}

func (a *ABI) EncodeStruct(structName string, json []byte) ([]byte, error) {
var buffer bytes.Buffer
encoder := NewEncoder(&buffer)

err := a.encode(encoder, structName, json)
if err != nil {
return nil, fmt.Errorf("encode: %s", err)
}
return buffer.Bytes(), nil
}

func (a *ABI) encode(binaryEncoder *Encoder, structName string, json []byte) error {
if loggingEnabled {
abiEncoderLog.Debug("abi encode struct", zap.String("name", structureName))
abiEncoderLog.Debug("abi encode struct", zap.String("name", structName))
}

structure := a.StructForName(structureName)
structure := a.StructForName(structName)
if structure == nil {
return fmt.Errorf("encode struct [%s] not found in abi", structureName)
return fmt.Errorf("encode struct [%s] not found in abi", structName)
}

if structure.Base != "" {
if loggingEnabled {
abiEncoderLog.Debug("struct has base struct", zap.String("struct", structureName), zap.String("base", structure.Base))
abiEncoderLog.Debug("struct has base struct", zap.String("struct", structName), zap.String("base", structure.Base))
}
err := a.encode(binaryEncoder, structure.Base, json)
if err != nil {
return fmt.Errorf("encode base [%s]: %s", structureName, err)
return fmt.Errorf("encode base [%s]: %s", structName, err)
}
}
err := a.encodeFields(binaryEncoder, structure.Fields, json)
Expand Down

0 comments on commit 21f9a14

Please sign in to comment.