go-ini.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "git.giaever.org/joachimmg/go-ini.git/ini"
  8. )
  9. type IniDate time.Time
  10. func (i IniDate) UnmarshalINI(b []byte) error {
  11. return nil
  12. }
  13. type TestUnm struct {
  14. myname string
  15. mydate int
  16. mymonth int
  17. myyear int
  18. }
  19. func (t *TestUnm) UnmarshalINI(b []byte) error {
  20. str := strings.Split(string(b), ",")
  21. if len(str) != 4 {
  22. return fmt.Errorf("must contain 4 entries, got %d", len(str))
  23. }
  24. d, err := strconv.Atoi(str[1])
  25. if err != nil {
  26. return err
  27. }
  28. m, err := strconv.Atoi(str[2])
  29. if err != nil {
  30. return err
  31. }
  32. y, err := strconv.Atoi(str[3])
  33. if err != nil {
  34. return err
  35. }
  36. t.myname = str[0]
  37. t.mydate = d
  38. t.mymonth = m
  39. t.myyear = y
  40. fmt.Println(str[0], d, m, y, t)
  41. return nil
  42. }
  43. type Test struct {
  44. String string
  45. UnquotedString string
  46. MultipleLines string
  47. WithoutValue bool
  48. AboutMe *TestUnm
  49. Date IniDate `ini:"date"`
  50. Int struct {
  51. Int int
  52. Int8 int8
  53. Int16 int16
  54. Int32 int32
  55. Int64 int64
  56. Uint struct {
  57. Uint uint
  58. Uint8 uint8
  59. Uint16 uint16
  60. Uint32 uint32
  61. Uint64 uint64
  62. }
  63. }
  64. }
  65. func main() {
  66. text := `
  67. String = "This is some text"
  68. UnquotedString = This is some unquoted text
  69. MultipleLines = This is \
  70. multiple lines \
  71. going on
  72. WithoutValue
  73. AboutMe = joachim,23,09,85
  74. [Int]
  75. Int = -1
  76. Int8 = -8
  77. Int16 = -16
  78. Int32 = -32
  79. Int64 = -64
  80. [Int.Uint]
  81. Uint = 1
  82. Uint8 = 8
  83. Uint16 = 16
  84. Uint32 = 32
  85. Uint64 = 64
  86. Uint64 = 64
  87. `
  88. fmt.Println("INI", text)
  89. test1 := &Test{}
  90. test2 := Test{
  91. String: "This is some text",
  92. UnquotedString: "Helloooo... Not possible to not quote this",
  93. MultipleLines: "This is \nmultiple lines \ngoing on",
  94. WithoutValue: true,
  95. Date: IniDate(time.Now()),
  96. }
  97. test2.Int.Int = -1
  98. test2.Int.Int8 = -8
  99. test2.Int.Int16 = -16
  100. test2.Int.Int32 = -32
  101. test2.Int.Int64 = -64
  102. test2.Int.Uint.Uint = 1
  103. test2.Int.Uint.Uint8 = 8
  104. test2.Int.Uint.Uint16 = 16
  105. test2.Int.Uint.Uint32 = 32
  106. test2.Int.Uint.Uint64 = 64
  107. fmt.Println("Test1", test1)
  108. fmt.Println("Result", ini.Unmarshal([]byte(text), test1), test1, test1.AboutMe)
  109. //fmt.Println(ini.Marshal(test2))
  110. }