Skip to content
Snippets Groups Projects

One more change to detect when custom types (like time.Time) has their own parsers, then do not step into those structs.

Closed Pieter van Staden requested to merge 18-use-scan-for-param-values into main
1 file
+ 28
5
Compare changes
  • Side-by-side
  • Inline
package struct_utils_test
package struct_utils_test
import (
import (
 
"fmt"
"testing"
"testing"
"gitlab.com/uafrica/go-utils/struct_utils"
"gitlab.com/uafrica/go-utils/struct_utils"
@@ -61,11 +62,6 @@ func TestAnonymous(t *testing.T) {
@@ -61,11 +62,6 @@ func TestAnonymous(t *testing.T) {
}
}
func TestMapParams(t *testing.T) {
func TestMapParams(t *testing.T) {
type paramsStruct struct {
ID int64 `json:"id,omitempty"`
IDs []int64 `json:"ids,omitempty"`
}
ps := paramsStruct{ID: 123}
ps := paramsStruct{ID: 123}
pm := struct_utils.MapParams(ps)
pm := struct_utils.MapParams(ps)
if len(pm) != 1 || pm["id"] != "123" {
if len(pm) != 1 || pm["id"] != "123" {
@@ -79,4 +75,31 @@ func TestMapParams(t *testing.T) {
@@ -79,4 +75,31 @@ func TestMapParams(t *testing.T) {
t.Fatalf("wrong params: %+v != %+v", ps, pm)
t.Fatalf("wrong params: %+v != %+v", ps, pm)
}
}
t.Logf("ps=%+v -> pm=%+v", ps, pm)
t.Logf("ps=%+v -> pm=%+v", ps, pm)
 
 
ps = paramsStruct{X1: xtype{1}, X3: &xtype{3}}
 
pm = struct_utils.MapParams(ps)
 
if len(pm) != 2 || pm["x1"] != ">>1<<" || pm["x3"] != ">>3<<" {
 
t.Fatalf("wrong params: %+v != %+v", ps, pm)
 
}
 
t.Logf("ps=%+v -> pm=%+v", ps, pm)
 
}
 
 
type paramsStruct struct {
 
ID int64 `json:"id,omitempty"`
 
IDs []int64 `json:"ids,omitempty"`
 
X1 xtype `json:"x1,omitempty"`
 
X2 xtype `json:"x2,omitempty"`
 
X3 *xtype `json:"x3,omitempty"`
 
X4 *xtype `json:"x4,omitempty"`
 
}
 
 
type xtype struct {
 
xvalue int
 
}
 
 
func (x xtype) String() string {
 
if x.xvalue == 0 {
 
return ""
 
}
 
return fmt.Sprintf(">>%d<<", x.xvalue)
}
}
Loading