config.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package toml
  2. import (
  3. "fmt"
  4. "io"
  5. "reflect"
  6. "strings"
  7. stringutil "github.com/naoina/go-stringutil"
  8. "github.com/naoina/toml/ast"
  9. )
  10. // Config contains options for encoding and decoding.
  11. type Config struct {
  12. // NormFieldName is used to match TOML keys to struct fields. The function runs for
  13. // both input keys and struct field names and should return a string that makes the
  14. // two match. You must set this field to use the decoder.
  15. //
  16. // Example: The function in the default config removes _ and lowercases all keys. This
  17. // allows a key called 'api_key' to match the struct field 'APIKey' because both are
  18. // normalized to 'apikey'.
  19. //
  20. // Note that NormFieldName is not used for fields which define a TOML
  21. // key through the struct tag.
  22. NormFieldName func(typ reflect.Type, keyOrField string) string
  23. // FieldToKey determines the TOML key of a struct field when encoding.
  24. // You must set this field to use the encoder.
  25. //
  26. // Note that FieldToKey is not used for fields which define a TOML
  27. // key through the struct tag.
  28. FieldToKey func(typ reflect.Type, field string) string
  29. // MissingField, if non-nil, is called when the decoder encounters a key for which no
  30. // matching struct field exists. The default behavior is to return an error.
  31. MissingField func(typ reflect.Type, key string) error
  32. }
  33. // DefaultConfig contains the default options for encoding and decoding.
  34. // Snake case (i.e. 'foo_bar') is used for key names.
  35. var DefaultConfig = Config{
  36. NormFieldName: defaultNormFieldName,
  37. FieldToKey: snakeCase,
  38. }
  39. func defaultNormFieldName(typ reflect.Type, s string) string {
  40. return strings.Replace(strings.ToLower(s), "_", "", -1)
  41. }
  42. func snakeCase(typ reflect.Type, s string) string {
  43. return stringutil.ToSnakeCase(s)
  44. }
  45. func defaultMissingField(typ reflect.Type, key string) error {
  46. return fmt.Errorf("field corresponding to `%s' is not defined in %v", key, typ)
  47. }
  48. // NewEncoder returns a new Encoder that writes to w.
  49. // It is shorthand for DefaultConfig.NewEncoder(w).
  50. func NewEncoder(w io.Writer) *Encoder {
  51. return DefaultConfig.NewEncoder(w)
  52. }
  53. // Marshal returns the TOML encoding of v.
  54. // It is shorthand for DefaultConfig.Marshal(v).
  55. func Marshal(v interface{}) ([]byte, error) {
  56. return DefaultConfig.Marshal(v)
  57. }
  58. // Unmarshal parses the TOML data and stores the result in the value pointed to by v.
  59. // It is shorthand for DefaultConfig.Unmarshal(data, v).
  60. func Unmarshal(data []byte, v interface{}) error {
  61. return DefaultConfig.Unmarshal(data, v)
  62. }
  63. // UnmarshalTable applies the contents of an ast.Table to the value pointed at by v.
  64. // It is shorthand for DefaultConfig.UnmarshalTable(t, v).
  65. func UnmarshalTable(t *ast.Table, v interface{}) error {
  66. return DefaultConfig.UnmarshalTable(t, v)
  67. }
  68. // NewDecoder returns a new Decoder that reads from r.
  69. // It is shorthand for DefaultConfig.NewDecoder(r).
  70. func NewDecoder(r io.Reader) *Decoder {
  71. return DefaultConfig.NewDecoder(r)
  72. }