z_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. }
  22. func TestInterface(t *testing.T) {
  23. m := MapFromInterface(Map{"one": 1})
  24. log.Println(m)
  25. }
  26. type TimeInterval struct {
  27. Start int
  28. Finish int
  29. }
  30. func (s *TimeInterval) JSONDecode(dec *JSONDecoder) (err error) {
  31. var sl []int
  32. if err = dec.Decode(&sl); err == nil {
  33. if len(sl) != 2 {
  34. return errors.New("TimeInterval: expected 2 int values")
  35. }
  36. s.Start, s.Finish = sl[0], sl[1]
  37. }
  38. return
  39. }
  40. type TimeIntervals []TimeInterval
  41. func (s *TimeIntervals) JSONDecode(dec *JSONDecoder) (isNil bool, err error) {
  42. var sl [][]int
  43. if err = dec.Decode(&sl); err == nil && sl != nil {
  44. *s = make(TimeIntervals, 0, len(sl))
  45. for i, interval := range sl {
  46. if len(interval) != 2 {
  47. return true, fmt.Errorf("Index %v - expected 2 elements list", i)
  48. }
  49. *s = append(*s, TimeInterval{
  50. Start: interval[0],
  51. Finish: interval[1],
  52. })
  53. }
  54. }
  55. isNil = sl == nil
  56. return
  57. }
  58. type NestedObject struct {
  59. ID int
  60. Name string
  61. Embedded *TObject
  62. Intervals TimeIntervals
  63. }
  64. type TObject struct {
  65. ID int
  66. Name string
  67. Embedded *NestedObject
  68. Intervals TimeIntervals
  69. }
  70. func (s *TObject) JSONField(fieldName string, storeTemp Map) (ptr interface{}, err error) {
  71. switch fieldName {
  72. case "id":
  73. ptr = &s.ID
  74. case "name":
  75. storeTemp["idd"] = &s.Name
  76. ptr = &s.Name
  77. case "embedded":
  78. ptr = &s.Embedded
  79. case "intervals":
  80. ptr = &s.Intervals
  81. }
  82. return
  83. }
  84. func (s *TObject) JSONFinish(storeTemp Map) error {
  85. log.Println("FINISH...", storeTemp.String("idd", "!"))
  86. return nil
  87. }
  88. func TestDecoder(t *testing.T) {
  89. /*
  90. //src := []byte("[ [ 1, 2 ], [ 3, 4 ] ]")
  91. src := []byte("null")
  92. var sl *TimeIntervals
  93. err := DecodeBytes(src, &sl)
  94. log.Println(sl, err)
  95. */
  96. // object
  97. fObj, err := os.Open("test_object.json")
  98. if err != nil {
  99. t.Error(err)
  100. }
  101. var obj TObject
  102. if err := Decode(fObj, &obj); err != nil {
  103. t.Error(err)
  104. }
  105. fObj.Close()
  106. log.Println("OBJ", obj, err, obj.Embedded, obj.Intervals)
  107. //log.Println("OBJ", obj, obj.embedded, obj.intervals)
  108. // slice
  109. fObj, err = os.Open("test_array.json")
  110. if err != nil {
  111. t.Error(err)
  112. }
  113. var arr []*TObject
  114. if err := Decode(fObj, &arr); err != nil {
  115. t.Error(err)
  116. }
  117. fObj.Close()
  118. log.Println("ARR", arr)
  119. for _, v := range arr {
  120. log.Println(v)
  121. }
  122. }
  123. /////////////////////////////////////////////////////////////////// project
  124. type LeadVerifySettings struct {
  125. BotEnabled bool
  126. ManagerEnabled bool
  127. CallDecryption bool
  128. //CallLeadIntervals []*TimeInterval
  129. CallsMaxCount int
  130. FirstDelay int
  131. //QualityCriteries []string
  132. RecallMinPeriod int
  133. ProductDescriptionFilename string
  134. ScriptFilename string
  135. }
  136. type Project struct {
  137. ID int64
  138. Name string
  139. LeadVerifySettings *LeadVerifySettings
  140. }
  141. func (s *Project) JSONField(fieldName string, storeTemp Map) (ptr interface{}, err error) {
  142. switch fieldName {
  143. case "id":
  144. ptr = &s.ID
  145. case "name":
  146. ptr = &s.Name
  147. case "lead_verify_settings":
  148. ptr = &s.LeadVerifySettings
  149. }
  150. return
  151. }
  152. func TestProject(t *testing.T) {
  153. 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}}`
  154. log.Println(string(src))
  155. var proj Project
  156. err := DecodeBytes([]byte(src), &proj)
  157. log.Println(proj, err)
  158. }
  159. func TestMap(t *testing.T) {
  160. m := Map{
  161. "one": float64(10.2),
  162. }
  163. m.StorePtr("two", "Coooool")
  164. log.Println(m.Int32("one", 0), m.String("two", "!"))
  165. }
  166. func TestMapVariable(t *testing.T) {
  167. m := Map{
  168. "one": int64(10),
  169. "two": "okko",
  170. }
  171. val := 10.05
  172. check := m.Variable("one", &val)
  173. t.Log(check, val)
  174. }