github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/db_args.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/rpc/shared"
    24  )
    25  
    26  type DbArgs struct {
    27  	Database string
    28  	Key      string
    29  	Value    []byte
    30  }
    31  
    32  func (args *DbArgs) UnmarshalJSON(b []byte) (err error) {
    33  	var obj []interface{}
    34  	if err := json.Unmarshal(b, &obj); err != nil {
    35  		return shared.NewDecodeParamError(err.Error())
    36  	}
    37  
    38  	if len(obj) < 2 {
    39  		return shared.NewInsufficientParamsError(len(obj), 2)
    40  	}
    41  
    42  	var objstr string
    43  	var ok bool
    44  
    45  	if objstr, ok = obj[0].(string); !ok {
    46  		return shared.NewInvalidTypeError("database", "not a string")
    47  	}
    48  	args.Database = objstr
    49  
    50  	if objstr, ok = obj[1].(string); !ok {
    51  		return shared.NewInvalidTypeError("key", "not a string")
    52  	}
    53  	args.Key = objstr
    54  
    55  	if len(obj) > 2 {
    56  		objstr, ok = obj[2].(string)
    57  		if !ok {
    58  			return shared.NewInvalidTypeError("value", "not a string")
    59  		}
    60  
    61  		args.Value = []byte(objstr)
    62  	}
    63  
    64  	return nil
    65  }
    66  
    67  func (a *DbArgs) requirements() error {
    68  	if len(a.Database) == 0 {
    69  		return shared.NewValidationError("Database", "cannot be blank")
    70  	}
    71  	if len(a.Key) == 0 {
    72  		return shared.NewValidationError("Key", "cannot be blank")
    73  	}
    74  	return nil
    75  }
    76  
    77  type DbHexArgs struct {
    78  	Database string
    79  	Key      string
    80  	Value    []byte
    81  }
    82  
    83  func (args *DbHexArgs) UnmarshalJSON(b []byte) (err error) {
    84  	var obj []interface{}
    85  	if err := json.Unmarshal(b, &obj); err != nil {
    86  		return shared.NewDecodeParamError(err.Error())
    87  	}
    88  
    89  	if len(obj) < 2 {
    90  		return shared.NewInsufficientParamsError(len(obj), 2)
    91  	}
    92  
    93  	var objstr string
    94  	var ok bool
    95  
    96  	if objstr, ok = obj[0].(string); !ok {
    97  		return shared.NewInvalidTypeError("database", "not a string")
    98  	}
    99  	args.Database = objstr
   100  
   101  	if objstr, ok = obj[1].(string); !ok {
   102  		return shared.NewInvalidTypeError("key", "not a string")
   103  	}
   104  	args.Key = objstr
   105  
   106  	if len(obj) > 2 {
   107  		objstr, ok = obj[2].(string)
   108  		if !ok {
   109  			return shared.NewInvalidTypeError("value", "not a string")
   110  		}
   111  
   112  		args.Value = common.FromHex(objstr)
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  func (a *DbHexArgs) requirements() error {
   119  	if len(a.Database) == 0 {
   120  		return shared.NewValidationError("Database", "cannot be blank")
   121  	}
   122  	if len(a.Key) == 0 {
   123  		return shared.NewValidationError("Key", "cannot be blank")
   124  	}
   125  	return nil
   126  }