go-ini.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package main
  2. import (
  3. "fmt"
  4. "git.giaever.org/joachimmg/go-ini.git/ini"
  5. "strings"
  6. "time"
  7. )
  8. type Test struct {
  9. Str string `ini:"String"`
  10. UnquotedString string
  11. MultipleLines string
  12. WithoutValue bool
  13. Date ini.Date
  14. Time ini.Time
  15. TestTime time.Time
  16. IP ini.IP
  17. Int struct {
  18. Int int
  19. Int8 int8
  20. Int16 int16
  21. Int32 int32
  22. Int64 int64
  23. Uint struct {
  24. Uint uint
  25. Uint8 uint8
  26. Uint16 uint16
  27. Uint32 uint32
  28. Uint64 uint64
  29. }
  30. }
  31. Float struct {
  32. Float32 float32
  33. Float64 float64
  34. }
  35. }
  36. func (t *Test) String() string {
  37. return fmt.Sprintf(`
  38. STRUCT DATA:
  39. Str: %v
  40. UnquotedString: %v
  41. MultipleLines: %v
  42. WithoutValue: %v
  43. Date: %v
  44. Time: %v
  45. TestTime: %v
  46. IP: %v
  47. Int {
  48. Int: %v
  49. Int8: %v
  50. Int16: %v
  51. Int32: %v
  52. Int64: %v
  53. Uint {
  54. Uint: %v
  55. Uint8: %v
  56. Uint16: %v
  57. Uint32: %v
  58. Uint64: %v
  59. }
  60. }
  61. Float {
  62. Float32: %v
  63. Float64: %v
  64. }`,
  65. t.Str, t.UnquotedString, strings.Replace(t.MultipleLines, "\n", "\n\t\t\t\t", -1), t.WithoutValue, t.Date, t.Time, t.TestTime, t.IP,
  66. t.Int.Int, t.Int.Int8, t.Int.Int16, t.Int.Int32, t.Int.Int64,
  67. t.Int.Uint.Uint, t.Int.Uint.Uint8, t.Int.Uint.Uint16, t.Int.Uint.Uint32, t.Int.Uint.Uint64,
  68. t.Float.Float32, t.Float.Float64,
  69. )
  70. }
  71. func sep(f, e string) {
  72. var s []byte
  73. for i := 0; i < 80; i++ {
  74. s = append(s, '/')
  75. }
  76. fmt.Println(f, string(s), e)
  77. }
  78. func main() {
  79. text := `
  80. String = "This is some text"
  81. UnquotedString = This is some unquoted text
  82. MultipleLines = This is \
  83. multiple lines \
  84. going on
  85. WithoutValue
  86. Date = 2017-9-28-Local-0
  87. Time = 01 Jan 01 00:00 UTC
  88. IP = ::
  89. [Int]
  90. Int = -1
  91. Int8 = -8
  92. Int16 = -16
  93. Int32 = -32
  94. Int64 = -64
  95. [Int.Uint]
  96. Uint = 1
  97. Uint8 = 8
  98. Uint16 = 16
  99. Uint32 = 32
  100. Uint64 = 64
  101. Uint64 = 64
  102. [Float]
  103. Float32 = 3.14722354762387648273648726348
  104. Float64 = 10.00003423472342734897234872347293748`
  105. first := &Test{}
  106. sep("", "")
  107. fmt.Println(" ### INI TO UNMARSHAL:")
  108. sep("", "")
  109. fmt.Println(text)
  110. sep("\n", "")
  111. fmt.Println(" ### INITIAL STRUCT VALUES (empty/unset values):")
  112. sep("", "")
  113. fmt.Println(first)
  114. sep("\n", "")
  115. fmt.Println(" --- NOW MARSHALING: INI -> STRUCT ---")
  116. err := ini.Unmarshal([]byte(text), first)
  117. fmt.Println(" !!! ERROR ON UMARSHALING:", err)
  118. sep("", "")
  119. fmt.Println(" ### NEW STRUCT VALUES (unmarshaled from INI):")
  120. sep("", "\n")
  121. fmt.Println(first)
  122. sep("\n", "")
  123. fmt.Println(" --- NOW UNMARSHALING: STRUCT -> INI ---")
  124. unm, err := ini.Marshal(first)
  125. fmt.Println(" !!! ERROR ON MARCHALING:", err)
  126. sep("", "")
  127. fmt.Println(" ### PRODUCED INI (marshaled from STRUCT)")
  128. sep("", "")
  129. fmt.Println(string(unm))
  130. sep("", "")
  131. }