Skip to content

Commit

Permalink
debuger for tlb decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tron committed Jan 26, 2024
1 parent f4aab03 commit 0fd6d63
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion tlb/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import (

// Decoder unmarshals a cell into a golang type.
type Decoder struct {
hasher *boc.Hasher
hasher *boc.Hasher
withDebug bool
debugPath []string
}

func (d *Decoder) WithDebug() *Decoder {
d.withDebug = true
return d
}

// NewDecoder returns a new Decoder.
Expand Down Expand Up @@ -43,6 +50,12 @@ var bocCellType = reflect.TypeOf(boc.Cell{})
var bitStringType = reflect.TypeOf(boc.BitString{})

func decode(c *boc.Cell, tag string, val reflect.Value, decoder *Decoder) error {
if decoder.withDebug {
decoder.debugPath = append(decoder.debugPath, fmt.Sprintf("%v#%v", val.Type().Name(), tag))
defer func() {
decoder.debugPath = decoder.debugPath[:len(decoder.debugPath)-1]
}()
}
t, err := parseTag(tag)
if err != nil {
return err
Expand Down Expand Up @@ -198,6 +211,9 @@ func decodeStruct(c *boc.Cell, val reflect.Value, decoder *Decoder) error {

func decodeBasicStruct(c *boc.Cell, val reflect.Value, decoder *Decoder) error {
for i := 0; i < val.NumField(); i++ {
if decoder.withDebug {
decoder.debugPath = append(decoder.debugPath, val.Type().Field(i).Name)
}
if !val.Field(i).CanSet() {
return fmt.Errorf("can't set field %v", i)
}
Expand All @@ -206,6 +222,9 @@ func decodeBasicStruct(c *boc.Cell, val reflect.Value, decoder *Decoder) error {
if err != nil {
return err
}
if decoder.withDebug {
decoder.debugPath = decoder.debugPath[:len(decoder.debugPath)-1]
}
}
return nil
}
Expand All @@ -224,6 +243,12 @@ func decodeSumType(c *boc.Cell, val reflect.Value, decoder *Decoder) error {
return err
}
if ok {
if decoder.withDebug {
decoder.debugPath = append(decoder.debugPath, val.Type().Field(i).Name)
defer func() {
decoder.debugPath = decoder.debugPath[:len(decoder.debugPath)-1]
}()
}
val.FieldByName("SumType").SetString(val.Type().Field(i).Name)
err := decode(c, "", val.Field(i), decoder)
if err != nil {
Expand Down

0 comments on commit 0fd6d63

Please sign in to comment.