encode.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package ini
  2. // Encode an interface (typically a struct) into INI-formatted data
  3. // Known types:
  4. // - Any kind of ints
  5. // - Any kind of unsigned ints
  6. // - Structs + pointers
  7. // - Boolean
  8. // - Strings
  9. // - Types with the method `MarshalText`
  10. //
  11. // Unknown types (also known types!) can impement the method MarshalINI
  12. // to be to override standard marshaling method, or make it marshalable if it's not.
  13. //
  14. // e.g: see types.go and the Time example. Time does implement MarshalText, but it doesnt
  15. // store the time-value in a very readable/editable manner, so the ini.Time is just a
  16. // wrapper-struct with the Marshaler-interface to read/write the time-value differently.
  17. import (
  18. "bytes"
  19. "reflect"
  20. )
  21. // Marshaler is an interface to implement for unknown types
  22. type Marshaler interface {
  23. MarshalINI() ([]byte, error)
  24. }
  25. type encoder struct {
  26. bytes.Buffer
  27. /*cache sync.Map TODO */
  28. }
  29. // Marshal encode interface v into INI-format
  30. func Marshal(v interface{}) ([]byte, error) {
  31. enc := &encoder{}
  32. if err := enc.marshal(v); err != nil {
  33. return nil, err
  34. }
  35. return enc.Bytes(), nil
  36. }
  37. // marshal maps struct to ini
  38. func (e *encoder) marshal(v interface{}) error {
  39. return e.imap("", reflect.ValueOf(v), nil, 0)
  40. }
  41. // write to byte buffer whenever a successfull encoding occurs
  42. func (e *encoder) write(ns string, f *field, fn encodeFn) error {
  43. f.fn = fn
  44. b, err := f.encode()
  45. if b != nil && err == nil {
  46. if _, err := e.Write(append(b, '\n')); err != nil {
  47. return err
  48. }
  49. }
  50. return err
  51. }
  52. // imap does the actual mapping on a specific namespace
  53. func (e *encoder) imap(ns string, v reflect.Value, p interface{}, i int) error {
  54. f := newField(v, p, i)
  55. f.setNS(ns)
  56. if f.implements(marshalerType) || f.implements(textMarshalerType) {
  57. return e.write(ns, f, f.marshalerDecoder)
  58. }
  59. switch v.Kind() {
  60. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  61. return e.write(ns, f, f.intEncoder)
  62. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  63. return e.write(ns, f, f.uintEncoder)
  64. case reflect.Float32:
  65. return e.write(ns, f, f.float32Encoder)
  66. case reflect.Float64:
  67. return e.write(ns, f, f.float64Encoder)
  68. case reflect.String:
  69. return e.write(ns, f, f.strEncoder)
  70. case reflect.Bool:
  71. return e.write(ns, f, f.boolEncoder)
  72. case reflect.Struct:
  73. if err := e.write(f.ns, f, f.structEncoder); err != nil {
  74. return err
  75. }
  76. for i := 0; i < f.v.NumField(); i++ {
  77. if err := e.imap(f.ns, f.v.Field(i), f.t, i); err != nil {
  78. return err
  79. }
  80. }
  81. case reflect.Ptr:
  82. if v.IsNil() {
  83. return nil
  84. }
  85. return e.imap(ns, v.Elem(), p, i)
  86. }
  87. return nil
  88. }