z_test.go 4.8 KB

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