Skip to content

Commit 7ed909d

Browse files
Merge pull request #249 from xconnio/parser-return-list-dict
parser: return list instead of []any and Dict instead of map[string]any in List and Dict functions
2 parents aa87cf3 + 99a21c1 commit 7ed909d

File tree

5 files changed

+80
-66
lines changed

5 files changed

+80
-66
lines changed

management_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ func TestManagementStatsAPIs(t *testing.T) {
4444

4545
callResp = session.Call(xconn.ManagementProcedureStatsStatusGet).Do()
4646
require.NoError(t, callResp.Err)
47-
require.Equal(t, map[string]any{"interval": int64(100), "running": true}, callResp.ArgDictOr(0, map[string]any{}))
47+
require.Equal(t, map[string]any{"interval": int64(100), "running": true}, callResp.ArgDictOr(0, xconn.Dict{}).Raw())
4848

4949
callResp = session.Call(xconn.ManagementProcedureStatsStatusSet).Kwarg("disable", true).Do()
5050
require.NoError(t, callResp.Err)
5151
require.NoError(t, callResp.Err)
5252

5353
callResp = session.Call(xconn.ManagementProcedureStatsStatusGet).Do()
5454
require.NoError(t, callResp.Err)
55-
require.Equal(t, map[string]any{"interval": int64(100), "running": false}, callResp.ArgDictOr(0, map[string]any{}))
55+
require.Equal(t, map[string]any{"interval": int64(100), "running": false}, callResp.ArgDictOr(0, xconn.Dict{}).Raw())
5656

5757
callResp = session.Call(xconn.ManagementProcedureStatsGet).Do()
5858
require.NoError(t, callResp.Err)
@@ -65,15 +65,15 @@ func TestManagementRealmListApi(t *testing.T) {
6565

6666
callResp := session.Call(xconn.ManagementProcedureListRealms).Do()
6767
require.NoError(t, callResp.Err)
68-
require.ElementsMatch(t, []any{"io.xconn.mgmt", "test"}, callResp.ArgListOr(0, []any{}))
68+
require.ElementsMatch(t, []any{"io.xconn.mgmt", "test"}, callResp.ArgListOr(0, []xconn.Value{}).Raw())
6969
}
7070

7171
func TestManagementSessionList(t *testing.T) {
7272
r, session := startRouterWithManagementAPIs(t)
7373

7474
callResp := session.Call(xconn.ManagementProcedureListSession).Arg("io.xconn.mgmt").Do()
7575
require.NoError(t, callResp.Err)
76-
require.Len(t, callResp.ArgListOr(0, []any{}), 2)
76+
require.Len(t, callResp.ArgListOr(0, []xconn.Value{}), 2)
7777

7878
for i := 0; i < 20; i++ {
7979
_, err := xconn.ConnectInMemory(r, xconn.ManagementRealm)
@@ -82,28 +82,28 @@ func TestManagementSessionList(t *testing.T) {
8282

8383
callResp = session.Call(xconn.ManagementProcedureListSession).Arg("io.xconn.mgmt").Do()
8484
require.NoError(t, callResp.Err)
85-
require.Len(t, callResp.ArgListOr(0, []any{}), 22)
85+
require.Len(t, callResp.ArgListOr(0, []xconn.Value{}), 22)
8686

8787
callResp = session.Call(xconn.ManagementProcedureListSession).Arg("io.xconn.mgmt").Kwargs(map[string]any{
8888
"limit": 10,
8989
"offset": 0,
9090
}).Do()
9191
require.NoError(t, callResp.Err)
92-
require.Len(t, callResp.ArgListOr(0, []any{}), 10)
92+
require.Len(t, callResp.ArgListOr(0, []xconn.Value{}), 10)
9393

9494
callResp = session.Call(xconn.ManagementProcedureListSession).Arg("io.xconn.mgmt").Kwargs(map[string]any{
9595
"limit": 10,
9696
"offset": 10,
9797
}).Do()
9898
require.NoError(t, callResp.Err)
99-
require.Len(t, callResp.ArgListOr(0, []any{}), 10)
99+
require.Len(t, callResp.ArgListOr(0, []xconn.Value{}), 10)
100100

101101
callResp = session.Call(xconn.ManagementProcedureListSession).Arg("io.xconn.mgmt").Kwargs(map[string]any{
102102
"limit": 10,
103103
"offset": 20,
104104
}).Do()
105105
require.NoError(t, callResp.Err)
106-
require.Len(t, callResp.ArgListOr(0, []any{}), 2)
106+
require.Len(t, callResp.ArgListOr(0, []xconn.Value{}), 2)
107107
}
108108

109109
func TestManagementSessionKill(t *testing.T) {

meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (m *meta) forEachSession(invocation *Invocation, fn func(sess BaseSession))
199199
if err != nil {
200200
return fmt.Errorf("wamp.error.invalid_argument")
201201
}
202-
roles = r
202+
roles = r.Raw()
203203
}
204204

205205
rlm, ok := m.router.realms.Load(m.realm)

parser.go

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -178,28 +178,42 @@ func (v Value) Decode(out any) error {
178178
return nil
179179
}
180180

181-
func (v Value) List() ([]any, error) {
182-
if b, ok := v.data.([]any); ok {
183-
return b, nil
181+
func (v Value) List() (List, error) {
182+
items, ok := v.data.([]any)
183+
if !ok {
184+
return nil, fmt.Errorf("value is not []any, got %T", v.data)
185+
}
186+
187+
list := make(List, len(items))
188+
for i, item := range items {
189+
list[i] = NewValue(item)
184190
}
185-
return nil, fmt.Errorf("value is not data []any, got %T", v.data)
191+
192+
return list, nil
186193
}
187194

188-
func (v Value) ListOr(def []any) []any {
195+
func (v Value) ListOr(def List) List {
189196
if b, err := v.List(); err == nil {
190197
return b
191198
}
192199
return def
193200
}
194201

195-
func (v Value) Dict() (map[string]any, error) {
196-
if b, ok := v.data.(map[string]any); ok {
197-
return b, nil
202+
func (v Value) Dict() (Dict, error) {
203+
items, ok := v.data.(map[string]any)
204+
if !ok {
205+
return nil, fmt.Errorf("value is not map[string]any, got %T", v.data)
206+
}
207+
208+
dict := make(Dict, len(items))
209+
for k, item := range items {
210+
dict[k] = NewValue(item)
198211
}
199-
return nil, fmt.Errorf("value is not data map[string]any, got %T", v.data)
212+
213+
return dict, nil
200214
}
201215

202-
func (v Value) DictOr(def map[string]any) map[string]any {
216+
func (v Value) DictOr(def Dict) Dict {
203217
if b, err := v.Dict(); err == nil {
204218
return b
205219
}
@@ -324,27 +338,27 @@ func (l List) Decode(out any) error {
324338
return nil
325339
}
326340

327-
func (l List) List(i int) ([]any, error) {
341+
func (l List) List(i int) (List, error) {
328342
v, err := l.Get(i)
329343
if err != nil {
330344
return nil, err
331345
}
332346
return v.List()
333347
}
334348

335-
func (l List) ListOr(i int, def []any) []any {
349+
func (l List) ListOr(i int, def List) List {
336350
return l.GetOr(i, def).ListOr(def)
337351
}
338352

339-
func (l List) Dict(i int) (map[string]any, error) {
353+
func (l List) Dict(i int) (Dict, error) {
340354
v, err := l.Get(i)
341355
if err != nil {
342356
return nil, err
343357
}
344358
return v.Dict()
345359
}
346360

347-
func (l List) DictOr(i int, def map[string]any) map[string]any {
361+
func (l List) DictOr(i int, def Dict) Dict {
348362
return l.GetOr(i, def).DictOr(def)
349363
}
350364

@@ -479,27 +493,27 @@ func (d Dict) Decode(out any) error {
479493
return nil
480494
}
481495

482-
func (d Dict) List(key string) ([]any, error) {
496+
func (d Dict) List(key string) (List, error) {
483497
v, err := d.Get(key)
484498
if err != nil {
485499
return nil, err
486500
}
487501
return v.List()
488502
}
489503

490-
func (d Dict) ListOr(key string, def []any) []any {
504+
func (d Dict) ListOr(key string, def List) List {
491505
return d.GetOr(key, def).ListOr(def)
492506
}
493507

494-
func (d Dict) Dict(key string) (map[string]any, error) {
508+
func (d Dict) Dict(key string) (Dict, error) {
495509
v, err := d.Get(key)
496510
if err != nil {
497511
return nil, err
498512
}
499513
return v.Dict()
500514
}
501515

502-
func (d Dict) DictOr(key string, def map[string]any) map[string]any {
516+
func (d Dict) DictOr(key string, def Dict) Dict {
503517
return d.GetOr(key, def).DictOr(def)
504518
}
505519

@@ -583,19 +597,19 @@ func (inv *Invocation) ArgsLen() int {
583597
return inv.args.Len()
584598
}
585599

586-
func (inv *Invocation) ArgList(index int) ([]any, error) {
600+
func (inv *Invocation) ArgList(index int) (List, error) {
587601
return inv.args.List(index)
588602
}
589603

590-
func (inv *Invocation) ArgListOr(index int, def []any) []any {
604+
func (inv *Invocation) ArgListOr(index int, def List) List {
591605
return inv.args.ListOr(index, def)
592606
}
593607

594-
func (inv *Invocation) ArgDict(index int) (map[string]any, error) {
608+
func (inv *Invocation) ArgDict(index int) (Dict, error) {
595609
return inv.args.Dict(index)
596610
}
597611

598-
func (inv *Invocation) ArgDictOr(index int, def map[string]any) map[string]any {
612+
func (inv *Invocation) ArgDictOr(index int, def Dict) Dict {
599613
return inv.args.DictOr(index, def)
600614
}
601615

@@ -651,19 +665,19 @@ func (inv *Invocation) KwargBytesOr(key string, def []byte) []byte {
651665
return inv.kwargs.BytesOr(key, def)
652666
}
653667

654-
func (inv *Invocation) KwargList(key string) ([]any, error) {
668+
func (inv *Invocation) KwargList(key string) (List, error) {
655669
return inv.kwargs.List(key)
656670
}
657671

658-
func (inv *Invocation) KwargListOr(key string, def []any) []any {
672+
func (inv *Invocation) KwargListOr(key string, def List) List {
659673
return inv.kwargs.ListOr(key, def)
660674
}
661675

662-
func (inv *Invocation) KwargDict(key string) (map[string]any, error) {
676+
func (inv *Invocation) KwargDict(key string) (Dict, error) {
663677
return inv.kwargs.Dict(key)
664678
}
665679

666-
func (inv *Invocation) KwargDictOr(key string, def map[string]any) map[string]any {
680+
func (inv *Invocation) KwargDictOr(key string, def Dict) Dict {
667681
return inv.kwargs.DictOr(key, def)
668682
}
669683

@@ -769,19 +783,19 @@ func (e *Event) ArgBytesOr(index int, def []byte) []byte {
769783
return e.args.BytesOr(index, def)
770784
}
771785

772-
func (e *Event) ArgList(index int) (list []any, err error) {
786+
func (e *Event) ArgList(index int) (list List, err error) {
773787
return e.args.List(index)
774788
}
775789

776-
func (e *Event) ArgListOr(index int, def []any) []any {
790+
func (e *Event) ArgListOr(index int, def List) List {
777791
return e.args.ListOr(index, def)
778792
}
779793

780-
func (e *Event) ArgDict(index int) (dict map[string]any, err error) {
794+
func (e *Event) ArgDict(index int) (dict Dict, err error) {
781795
return e.args.Dict(index)
782796
}
783797

784-
func (e *Event) ArgDictOr(index int, def map[string]any) map[string]any {
798+
func (e *Event) ArgDictOr(index int, def Dict) Dict {
785799
return e.args.DictOr(index, def)
786800
}
787801

@@ -841,19 +855,19 @@ func (e *Event) KwargBytesOr(key string, def []byte) []byte {
841855
return e.kwargs.BytesOr(key, def)
842856
}
843857

844-
func (e *Event) KwargList(key string) ([]any, error) {
858+
func (e *Event) KwargList(key string) (List, error) {
845859
return e.kwargs.List(key)
846860
}
847861

848-
func (e *Event) KwargListOr(key string, def []any) []any {
862+
func (e *Event) KwargListOr(key string, def List) List {
849863
return e.kwargs.ListOr(key, def)
850864
}
851865

852-
func (e *Event) KwargDict(key string) (map[string]any, error) {
866+
func (e *Event) KwargDict(key string) (Dict, error) {
853867
return e.kwargs.Dict(key)
854868
}
855869

856-
func (e *Event) KwargDictOr(key string, def map[string]any) map[string]any {
870+
func (e *Event) KwargDictOr(key string, def Dict) Dict {
857871
return e.kwargs.DictOr(key, def)
858872
}
859873

router_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func TestRouterMetaKillByAuthID(t *testing.T) {
8787
require.NoError(t, resp.Err)
8888
sessionList, err := resp.ArgList(0)
8989
require.NoError(t, err)
90-
require.Contains(t, sessionList, session1.ID())
91-
require.Contains(t, sessionList, session2.ID())
90+
require.Contains(t, sessionList.Raw(), session1.ID())
91+
require.Contains(t, sessionList.Raw(), session2.ID())
9292

9393
// Verify both sessions are disconnected
9494
require.Eventually(t, func() bool {
@@ -129,8 +129,8 @@ func TestRouterMetaKillByAuthRole(t *testing.T) {
129129
require.NoError(t, resp.Err)
130130
sessionList, err := resp.ArgList(0)
131131
require.NoError(t, err)
132-
require.Contains(t, sessionList, session1.ID())
133-
require.Contains(t, sessionList, session2.ID())
132+
require.Contains(t, sessionList.Raw(), session1.ID())
133+
require.Contains(t, sessionList.Raw(), session2.ID())
134134

135135
// Verify both sessions are disconnected
136136
require.Eventually(t, func() bool {
@@ -167,8 +167,8 @@ func TestRouterMetaKillAll(t *testing.T) {
167167
require.NoError(t, resp.Err)
168168
sessionList, err := resp.ArgList(0)
169169
require.NoError(t, err)
170-
require.Contains(t, sessionList, session1.ID())
171-
require.Contains(t, sessionList, session2.ID())
170+
require.Contains(t, sessionList.Raw(), session1.ID())
171+
require.Contains(t, sessionList.Raw(), session2.ID())
172172

173173
// Verify both sessions are disconnected
174174
require.Eventually(t, func() bool {
@@ -257,7 +257,7 @@ func TestRouterMetaSessionList(t *testing.T) {
257257
require.NoError(t, resp.Err)
258258
ids := resp.ArgListOr(0, nil)
259259
require.Len(t, ids, 1)
260-
require.Equal(t, session2.ID(), ids[0])
260+
require.Equal(t, session2.ID(), ids.UInt64Or(0, 0))
261261
})
262262

263263
// Disconnect admin session

0 commit comments

Comments
 (0)