github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/accounts/abi/tables.go (about)

     1  // Copyright 2019 The go-vnt Authors
     2  // This file is part of the go-vnt library.
     3  //
     4  // The go-vnt library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-vnt library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-vnt library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  )
    23  
    24  type Table struct {
    25  	Name        string
    26  	Type        Type
    27  	StorageType uint64
    28  	Tables      Tables
    29  }
    30  
    31  type Tables []Table
    32  
    33  type Extarg struct {
    34  	Name        string
    35  	Type        string
    36  	StorageType uint64
    37  	Tables      []Extarg
    38  }
    39  
    40  // UnmarshalJSON implements json.Unmarshaler interface
    41  func (table *Table) UnmarshalJSON(data []byte) error {
    42  	var ext Extarg
    43  	err := json.Unmarshal(data, &ext)
    44  	if err != nil {
    45  		return fmt.Errorf("argument json err: %v", err)
    46  	}
    47  
    48  	err = ext.recursive(table)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func (ext *Extarg) recursive(table *Table) error {
    57  	if ext.Type == "" {
    58  		table.Type = Type{}
    59  	} else {
    60  		var err error
    61  		table.Type, err = NewType(ext.Type)
    62  		if err != nil {
    63  			return err
    64  		}
    65  	}
    66  	table.Name = ext.Name
    67  	table.StorageType = ext.StorageType
    68  
    69  	// table.Tables = ext.Name
    70  	if len(ext.Tables) != 0 {
    71  		for _, v := range ext.Tables {
    72  			t := &Table{}
    73  			err := v.recursive(t)
    74  			if err != nil {
    75  				return err
    76  			}
    77  			table.Tables = append(table.Tables, *t)
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  func (tbl Table) Traversal(sym string, key *Key) {
    84  	if len(tbl.Tables) == 0 {
    85  		key.Types[sym] = tbl.Type
    86  		key.Keys[sym] = tbl
    87  	} else {
    88  		for _, v := range tbl.Tables {
    89  			s := fmt.Sprintf("%s.%s", sym, v.Name)
    90  			v.Traversal(s, key)
    91  		}
    92  	}
    93  }