z_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package json
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "os"
  7. "testing"
  8. )
  9. func TestJSON(t *testing.T) {
  10. m := Map{
  11. "jsrc": []int{1, 2, 3, 4},
  12. "kate": nil,
  13. "m1": Map{"one": 1},
  14. "m2": map[string]interface{}{"one": 2},
  15. }
  16. t.Log(m, string(m.JSON()))
  17. t.Log(string(m.ValueJSON("jsrc", []byte{})))
  18. t.Log(string(m.ValueJSON("jsrc1", []byte("{ }"))))
  19. t.Log(m.Map("m1", Map{}))
  20. t.Log(m.Map("m2", Map{}))
  21. t.Log(string(m.JSON()))
  22. t.Log(string(m.JSONIndent("", "\t")))
  23. }
  24. func TestInterface(t *testing.T) {
  25. m := MapFromInterface(Map{"one": 1})
  26. log.Println(m)
  27. }
  28. type TimeInterval struct {
  29. Start int
  30. Finish int
  31. }
  32. func (s *TimeInterval) JSONDecode(dec *JSONDecoder) (err error) {
  33. var sl []int
  34. if err = dec.Decode(&sl); err == nil {
  35. if len(sl) != 2 {
  36. return errors.New("TimeInterval: expected 2 int values")
  37. }
  38. s.Start, s.Finish = sl[0], sl[1]
  39. }
  40. return
  41. }
  42. type TimeIntervals []TimeInterval
  43. func (s *TimeIntervals) JSONDecode(dec *JSONDecoder) (isNil bool, err error) {
  44. var sl [][]int
  45. if err = dec.Decode(&sl); err == nil && sl != nil {
  46. *s = make(TimeIntervals, 0, len(sl))
  47. for i, interval := range sl {
  48. if len(interval) != 2 {
  49. return true, fmt.Errorf("Index %v - expected 2 elements list", i)
  50. }
  51. *s = append(*s, TimeInterval{
  52. Start: interval[0],
  53. Finish: interval[1],
  54. })
  55. }
  56. }
  57. isNil = sl == nil
  58. return
  59. }
  60. type NestedObject struct {
  61. ID int
  62. Name string
  63. Embedded *TObject
  64. Intervals TimeIntervals
  65. }
  66. type TObject struct {
  67. ID int
  68. Name string
  69. Embedded *NestedObject
  70. Intervals TimeIntervals
  71. }
  72. func (s *TObject) JSONField(fieldName string, storeTemp Map) (ptr interface{}, err error) {
  73. switch fieldName {
  74. case "id":
  75. ptr = &s.ID
  76. case "name":
  77. storeTemp["idd"] = &s.Name
  78. ptr = &s.Name
  79. case "embedded":
  80. ptr = &s.Embedded
  81. case "intervals":
  82. ptr = &s.Intervals
  83. }
  84. return
  85. }
  86. func (s *TObject) JSONFinish(storeTemp Map) error {
  87. log.Println("FINISH...", storeTemp.String("idd", "!"))
  88. return nil
  89. }
  90. func TestDecoder(t *testing.T) {
  91. /*
  92. //src := []byte("[ [ 1, 2 ], [ 3, 4 ] ]")
  93. src := []byte("null")
  94. var sl *TimeIntervals
  95. err := DecodeBytes(src, &sl)
  96. log.Println(sl, err)
  97. */
  98. // object
  99. fObj, err := os.Open("test_object.json")
  100. if err != nil {
  101. t.Error(err)
  102. }
  103. var obj TObject
  104. if err := Decode(fObj, &obj); err != nil {
  105. t.Error(err)
  106. }
  107. fObj.Close()
  108. log.Println("OBJ", obj, err, obj.Embedded, obj.Intervals)
  109. //log.Println("OBJ", obj, obj.embedded, obj.intervals)
  110. // slice
  111. fObj, err = os.Open("test_array.json")
  112. if err != nil {
  113. t.Error(err)
  114. }
  115. var arr []*TObject
  116. if err := Decode(fObj, &arr); err != nil {
  117. t.Error(err)
  118. }
  119. fObj.Close()
  120. log.Println("ARR", arr)
  121. for _, v := range arr {
  122. log.Println(v)
  123. }
  124. }
  125. /////////////////////////////////////////////////////////////////// project
  126. type LeadVerifySettings struct {
  127. BotEnabled bool
  128. ManagerEnabled bool
  129. CallDecryption bool
  130. //CallLeadIntervals []*TimeInterval
  131. CallsMaxCount int
  132. FirstDelay int
  133. //QualityCriteries []string
  134. RecallMinPeriod int
  135. ProductDescriptionFilename string
  136. ScriptFilename string
  137. }
  138. type Project struct {
  139. ID int64
  140. Name string
  141. LeadVerifySettings *LeadVerifySettings
  142. }
  143. func (s *Project) JSONField(fieldName string, storeTemp Map) (ptr interface{}, err error) {
  144. switch fieldName {
  145. case "id":
  146. ptr = &s.ID
  147. case "name":
  148. ptr = &s.Name
  149. case "lead_verify_settings":
  150. ptr = &s.LeadVerifySettings
  151. }
  152. return
  153. }
  154. func TestProject(t *testing.T) {
  155. src := `{"id": 1536, "name": "Первый проект 2", "lead_verify_settings": {"bot_enabled": false, "first_delay": 81000, "call_decryption": true, "calls_max_count": 100, "manager_enabled": false, "script_filename": null, "quality_criteries": ["Ок (качественный лид)", "номер не существует", "номер не отвечает/заблокирован/сбрасывает/молчание в трубке ХХХ дней", "номер заблокирован", "не автор заявки", "не наше ГЕО", "не заинтересован в продукте (у клиента другой вопрос)", "нет 18 лет", "несоответствие языка", "неразборчивая речь"], "recall_min_period": 9600, "call_lead_day_intervals": [[3600, 7200]], "product_description_filename": null}}`
  156. log.Println(string(src))
  157. var proj Project
  158. err := DecodeBytes([]byte(src), &proj)
  159. log.Println(proj, err)
  160. }
  161. func TestMap(t *testing.T) {
  162. m := Map{
  163. "one": float64(10.2),
  164. }
  165. m.StorePtr("two", "Coooool")
  166. log.Println(m.Int32("one", 0), m.String("two", "!"))
  167. }
  168. func TestMapVariable(t *testing.T) {
  169. m := Map{
  170. "one": int64(10),
  171. "two": "okko",
  172. }
  173. val := 10.05
  174. check := m.Variable("one", &val)
  175. t.Log(check, val)
  176. }