30d706c35e
* p2p/discover, p2p/discv5: add marshaling methods to Node * p2p/netutil: make Netlist decodable from TOML * common/math: encode nil HexOrDecimal256 as 0x0 * cmd/geth: add --config file flag * cmd/geth: add missing license header * eth: prettify Config again, fix tests * eth: use gasprice.Config instead of duplicating its fields * eth/gasprice: hide nil default from dumpconfig output * cmd/geth: hide genesis block in dumpconfig output * node: make tests compile * console: fix tests * cmd/geth: make TOML keys look exactly like Go struct fields * p2p: use discovery by default This makes the zero Config slightly more useful. It also fixes package node tests because Node detects reuse of the datadir through the NodeDatabase. * cmd/geth: make ethstats URL settable through config file * cmd/faucet: fix configuration * cmd/geth: dedup attach tests * eth: add comment for DefaultConfig * eth: pass downloader.SyncMode in Config This removes the FastSync, LightSync flags in favour of a more general SyncMode flag. * cmd/utils: remove jitvm flags * cmd/utils: make mutually exclusive flag error prettier It now reads: Fatal: flags --dev, --testnet can't be used at the same time * p2p: fix typo * node: add DefaultConfig, use it for geth * mobile: add missing NoDiscovery option * cmd/utils: drop MakeNode This exposed a couple of places that needed to be updated to use node.DefaultConfig. * node: fix typo * eth: make fast sync the default mode * cmd/utils: remove IPCApiFlag (unused) * node: remove default IPC path Set it in the frontends instead. * cmd/geth: add --syncmode * cmd/utils: make --ipcdisable and --ipcpath mutually exclusive * cmd/utils: don't enable WS, HTTP when setting addr * cmd/utils: fix --identity
87 lines
2.9 KiB
Go
87 lines
2.9 KiB
Go
package toml
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"reflect"
|
|
"strings"
|
|
|
|
stringutil "github.com/naoina/go-stringutil"
|
|
"github.com/naoina/toml/ast"
|
|
)
|
|
|
|
// Config contains options for encoding and decoding.
|
|
type Config struct {
|
|
// NormFieldName is used to match TOML keys to struct fields. The function runs for
|
|
// both input keys and struct field names and should return a string that makes the
|
|
// two match. You must set this field to use the decoder.
|
|
//
|
|
// Example: The function in the default config removes _ and lowercases all keys. This
|
|
// allows a key called 'api_key' to match the struct field 'APIKey' because both are
|
|
// normalized to 'apikey'.
|
|
//
|
|
// Note that NormFieldName is not used for fields which define a TOML
|
|
// key through the struct tag.
|
|
NormFieldName func(typ reflect.Type, keyOrField string) string
|
|
|
|
// FieldToKey determines the TOML key of a struct field when encoding.
|
|
// You must set this field to use the encoder.
|
|
//
|
|
// Note that FieldToKey is not used for fields which define a TOML
|
|
// key through the struct tag.
|
|
FieldToKey func(typ reflect.Type, field string) string
|
|
|
|
// MissingField, if non-nil, is called when the decoder encounters a key for which no
|
|
// matching struct field exists. The default behavior is to return an error.
|
|
MissingField func(typ reflect.Type, key string) error
|
|
}
|
|
|
|
// DefaultConfig contains the default options for encoding and decoding.
|
|
// Snake case (i.e. 'foo_bar') is used for key names.
|
|
var DefaultConfig = Config{
|
|
NormFieldName: defaultNormFieldName,
|
|
FieldToKey: snakeCase,
|
|
}
|
|
|
|
func defaultNormFieldName(typ reflect.Type, s string) string {
|
|
return strings.Replace(strings.ToLower(s), "_", "", -1)
|
|
}
|
|
|
|
func snakeCase(typ reflect.Type, s string) string {
|
|
return stringutil.ToSnakeCase(s)
|
|
}
|
|
|
|
func defaultMissingField(typ reflect.Type, key string) error {
|
|
return fmt.Errorf("field corresponding to `%s' is not defined in %v", key, typ)
|
|
}
|
|
|
|
// NewEncoder returns a new Encoder that writes to w.
|
|
// It is shorthand for DefaultConfig.NewEncoder(w).
|
|
func NewEncoder(w io.Writer) *Encoder {
|
|
return DefaultConfig.NewEncoder(w)
|
|
}
|
|
|
|
// Marshal returns the TOML encoding of v.
|
|
// It is shorthand for DefaultConfig.Marshal(v).
|
|
func Marshal(v interface{}) ([]byte, error) {
|
|
return DefaultConfig.Marshal(v)
|
|
}
|
|
|
|
// Unmarshal parses the TOML data and stores the result in the value pointed to by v.
|
|
// It is shorthand for DefaultConfig.Unmarshal(data, v).
|
|
func Unmarshal(data []byte, v interface{}) error {
|
|
return DefaultConfig.Unmarshal(data, v)
|
|
}
|
|
|
|
// UnmarshalTable applies the contents of an ast.Table to the value pointed at by v.
|
|
// It is shorthand for DefaultConfig.UnmarshalTable(t, v).
|
|
func UnmarshalTable(t *ast.Table, v interface{}) error {
|
|
return DefaultConfig.UnmarshalTable(t, v)
|
|
}
|
|
|
|
// NewDecoder returns a new Decoder that reads from r.
|
|
// It is shorthand for DefaultConfig.NewDecoder(r).
|
|
func NewDecoder(r io.Reader) *Decoder {
|
|
return DefaultConfig.NewDecoder(r)
|
|
}
|