Skip to content

Commit

Permalink
rework jsonify after table struct changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Dec 18, 2024
1 parent e21511c commit f8a1ccb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions jsonify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type DumpListNode[V any] struct {
// MarshalJSON dumps the table into two sorted lists: for ipv4 and ipv6.
// Every root and subnet is an array, not a map, because the order matters.
func (t *Table[V]) MarshalJSON() ([]byte, error) {
if t == nil {
return nil, nil
}

result := struct {
Ipv4 []DumpListNode[V] `json:"ipv4,omitempty"`
Ipv6 []DumpListNode[V] `json:"ipv6,omitempty"`
Expand All @@ -38,12 +42,18 @@ func (t *Table[V]) MarshalJSON() ([]byte, error) {
// DumpList4 dumps the ipv4 tree into a list of roots and their subnets.
// It can be used to analyze the tree or build custom json representation.
func (t *Table[V]) DumpList4() []DumpListNode[V] {
if t == nil {
return nil
}
return t.root4.dumpListRec(0, zeroPath, 0, true)
}

// DumpList6 dumps the ipv6 tree into a list of roots and their subnets.
// It can be used to analyze the tree or build custom json representation.
func (t *Table[V]) DumpList6() []DumpListNode[V] {
if t == nil {
return nil
}
return t.root6.dumpListRec(0, zeroPath, 0, false)
}

Expand Down
15 changes: 15 additions & 0 deletions jsonify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ type jsonTest struct {
want string
}

func TestJSONTableIsNil(t *testing.T) {
t.Parallel()
var tbl *Table[any]
checkJSON(t, tbl, jsonTest{
want: "null",
})
}

func TestJSONEmpty(t *testing.T) {
tbl := new(Table[any])
checkJSON(t, tbl, jsonTest{
want: "{}",
})
}

func TestJSONDefaultRouteV4(t *testing.T) {
t.Parallel()
tbl := new(Table[any])
Expand Down

0 comments on commit f8a1ccb

Please sign in to comment.