z_test.go 4.8 KB

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