123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- package rest
- import (
- "log"
- "reflect"
- "testing"
- "time"
- "git.ali33.ru/fcg-xvii/go-tools/json"
- )
- func TestFieldsDefault(t *testing.T) {
- type RR struct {
- ID int64 `rest:"default"`
- Name string `rest:"default"`
- GroupID int64
- }
- rr := RR{}
- fields := fieldsDefault(reflect.TypeOf(rr))
- expected := []any{"id", "name"}
- if len(fields) != len(expected) {
- t.Errorf("expected %v, real %v", expected, fields)
- }
- for i, val := range expected {
- if fields[i] != val {
- t.Errorf("expected %v, real %v", expected, fields)
- }
- }
- }
- func TestFielderName(t *testing.T) {
- name := "Name"
- if field, err := parseName(reflect.ValueOf(name)); field.Name != camelToSnake(name) || err != nil {
- if err != nil {
- t.Error(err)
- }
- t.Errorf("expected %v, real %v", camelToSnake(name), field.Name)
- }
- obj := json.Map{
- "name": "Object",
- "fields": []any{
- "okko",
- "cooler",
- },
- }
- field, err := parseName(reflect.ValueOf(obj))
- if err != nil {
- t.Error(err)
- }
- // check name
- if field.Name != "object" {
- t.Errorf("expected %v, real %v", "object", name)
- }
- // check fields
- for i, val := range []any{"okko", "cooler"} {
- if field.Names[i] != val {
- t.Errorf("expected %v, real %v", val, field.Names[i])
- }
- }
- }
- type RR struct {
- ID int64 `rest:"default"`
- Name string `rest:"default"`
- GroupID int64 `rest:"ignore"`
- Child *RR
- Nums []int64
- Childs []*RR
- MM json.Map
- }
- func (s *RR) RestFields(result json.Map, fields FieldList) {
- if _, check := fields.Field("group_id"); check {
- result["group_id"] = s.GroupID * 1000
- }
- }
- func TestFielderVal(t *testing.T) {
- rr := &RR{
- ID: 1,
- Name: "okko",
- GroupID: 2,
- MM: json.Map{
- "one": 1,
- "obj": &RR{
- ID: 1,
- Name: "mm-obj",
- },
- },
- Child: &RR{
- ID: 3,
- Name: "cooler",
- GroupID: 4,
- },
- Nums: []int64{1, 2, 3},
- Childs: []*RR{
- {
- ID: 5,
- Name: "okko1",
- GroupID: 6,
- },
- {
- ID: 7,
- Name: "cooler1",
- GroupID: 8,
- },
- },
- }
- val, err := fieldVal(
- reflect.ValueOf(rr),
- "",
- nil,
- "id",
- "name",
- "nums",
- "group_id",
- "mm",
- json.Map{
- "name": "childs",
- "fields": []any{"id", "name", "childs"},
- },
- json.Map{
- "name": "child",
- "fields": []any{"id"},
- },
- )
- if err != nil {
- t.Error(err, err.Args())
- }
- m := val.Interface().(json.Map)
- m.LogPretty()
- mVal, err := Fields(
- rr,
- nil,
- "id",
- "name",
- "nums",
- "mm",
- "group_id",
- json.Map{
- "name": "childs",
- "fields": []any{"id", "name", "childs"},
- },
- json.Map{
- "name": "child",
- "fields": []any{"id"},
- },
- )
- if err != nil {
- t.Error(err, err.Args())
- }
- log.Println("========================================")
- mVal.LogPretty()
- }
- type RTime struct {
- Time
- }
- type TimeObj struct {
- Created RTime `rest:"default"`
- }
- func TestTime(t *testing.T) {
- obj := TimeObj{
- Created: RTime{Time(time.Now())},
- }
- log.Println(obj)
- f, _ := Fields(&obj, nil)
- f.LogPretty()
- var rObj TimeObj
- if err := Serialize(f, &rObj); err != nil {
- t.Fatal(err.Map().JSONPrettyString())
- }
- log.Println(rObj)
- jm := json.Map{
- "created": 12321311,
- }
- if err := Serialize(jm, &rObj); err != nil {
- t.Fatal(err.Map().JSONPrettyString())
- }
- t.Log(rObj)
- }
- type LObj struct {
- ID int64 `rest:"default"`
- Name string `rest:"default"`
- }
- type LList struct {
- Result []*LObj `rest:"fixed"`
- }
- func (s *LList) RestFields(fieldName string, names ...any) (any, IErrorArgs) {
- if fieldName == "" {
- l, err := FieldsAny(&s.Result, nil, names...)
- if err != nil {
- return nil, err
- }
- rm := json.Map{
- "llist": l,
- }
- return rm, nil
- }
- return nil, ErrorFiled(fieldName, "field is not exists")
- }
- func TestFielderList(t *testing.T) {
- obj := &LList{
- Result: []*LObj{
- {
- ID: 1,
- Name: "Val 1",
- },
- {
- ID: 2,
- Name: "Val 2",
- },
- },
- }
- m, err := Fields(obj, nil, "id")
- if err != nil {
- t.Fatal(err)
- }
- m.LogPretty()
- }
- type RRequest struct {
- Count int
- Okko string `rest:"required"`
- }
- type Tickers struct {
- *RRequest
- Cool *RRequest
- TickerIDS []string
- }
- func TestSerializeRaw(t *testing.T) {
- m := json.Map{
- "ticker_ids": []string{"1", "2", "3"},
- "okko": "10",
- }
- var ti Tickers
- if err := Serialize(m, &ti); err != nil {
- t.Fatal(err)
- }
- log.Println(ti.TickerIDS)
- log.Println(ti.RRequest)
- log.Println(ti.Cool)
- }
- func TestServer(t *testing.T) {
- }
- func TestFieldKeys(t *testing.T) {
- l := []any{
- "id",
- "name",
- json.Map{
- "name": "parent",
- "fields": []any{
- "id",
- "name",
- json.Map{
- "name": "child1",
- "fields": []any{},
- },
- json.Map{
- "name": "child2",
- "fields": []any{
- "one",
- },
- },
- },
- },
- }
- jm := ObjectFieldKeys(l)
- jm.LogPretty()
- }
- type FieldNamesTest struct {
- ID int
- Name string
- ParentID int
- }
- func TestFieldNames(t *testing.T) {
- v := FieldNamesTest{
- ID: 10,
- Name: "Namerrrr",
- ParentID: 1,
- }
- var iv any = &v
- fields, err := FieldNames(iv)
- if err != nil {
- t.Fatal(err)
- }
- t.Log(fields)
- }
|