z_test.go 4.7 KB

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