Skip to content

Commit e67a0d0

Browse files
authored
Format generated numbers according to their type (#39)
* Format generated numbers according to their type * Add tests
1 parent 3053b12 commit e67a0d0

File tree

6 files changed

+113
-4
lines changed

6 files changed

+113
-4
lines changed

datamodel/datamodel_test.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package datamodel
22

33
import (
44
"errors"
5-
"fmt"
65
"testing"
76
"time"
87

@@ -86,6 +85,76 @@ func TestGetValue(t *testing.T) {
8685
assert.Equal(t, "Residential Gateway", param.Value)
8786
}
8887

88+
func TestGetValueFormatGen(t *testing.T) {
89+
const path1 = "Device.Ethernet.Interface.5.Stats.BytesReceived"
90+
const path2 = "Device.Ethernet.Interface.5.Enabled"
91+
t.Run("int", func(t *testing.T) {
92+
state := newState()
93+
dm := New(state.WithDefaults(map[string]Parameter{
94+
path1: {
95+
Path: path1,
96+
Type: "xsd:int",
97+
gen: func() float64 { return 123.456 },
98+
},
99+
}))
100+
param, ok := dm.GetValue(path1)
101+
assert.True(t, ok)
102+
assert.Equal(t, "123", param.GetValue())
103+
})
104+
t.Run("uint", func(t *testing.T) {
105+
state := newState()
106+
dm := New(state.WithDefaults(map[string]Parameter{
107+
path1: {
108+
Path: path1,
109+
Type: "unsignedInt",
110+
gen: func() float64 { return 123.456 },
111+
},
112+
}))
113+
param, ok := dm.GetValue(path1)
114+
assert.True(t, ok)
115+
assert.Equal(t, "123", param.GetValue())
116+
})
117+
t.Run("float", func(t *testing.T) {
118+
state := newState()
119+
dm := New(state.WithDefaults(map[string]Parameter{
120+
path1: {
121+
Path: path1,
122+
Type: "double",
123+
gen: func() float64 { return 123.456 },
124+
},
125+
}))
126+
param, ok := dm.GetValue(path1)
127+
assert.True(t, ok)
128+
assert.Equal(t, "123.456", param.GetValue())
129+
})
130+
t.Run("bool", func(t *testing.T) {
131+
state := newState()
132+
dm := New(state.WithDefaults(map[string]Parameter{
133+
path2: {
134+
Path: path2,
135+
Type: "xsd:boolean",
136+
gen: func() float64 { return 1 },
137+
},
138+
}))
139+
param, ok := dm.GetValue(path2)
140+
assert.True(t, ok)
141+
assert.Equal(t, "true", param.GetValue())
142+
})
143+
t.Run("unsupported", func(t *testing.T) {
144+
state := newState()
145+
dm := New(state.WithDefaults(map[string]Parameter{
146+
path2: {
147+
Path: path2,
148+
Type: "xsd:string",
149+
gen: func() float64 { return 123 },
150+
},
151+
}))
152+
param, ok := dm.GetValue(path2)
153+
assert.True(t, ok)
154+
assert.Equal(t, "", param.GetValue())
155+
})
156+
}
157+
89158
func TestGetValues(t *testing.T) {
90159
state := newState()
91160
dm := New(state.WithDefaults(map[string]Parameter{
@@ -106,7 +175,6 @@ func TestGetValues(t *testing.T) {
106175
"Device.DeviceInfo.Description",
107176
"Device.DeviceInfo.HardwareVersion",
108177
)
109-
fmt.Println(ok, params)
110178
assert.True(t, ok)
111179
assert.Len(t, params, 2)
112180
}

datamodel/noise/parser.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"regexp"
77
"strconv"
8+
9+
"github.com/localhots/SimulaTR69/rpc"
810
)
911

1012
// Func represents a noise generator function with its name, arguments, and type.
@@ -58,6 +60,16 @@ func ParseDef(str string) (*Func, error) {
5860
args[k] = val
5961
}
6062

63+
// Validate return type
64+
switch rpc.NoXSD(matches[3]) {
65+
case rpc.TypeInt, rpc.TypeLong:
66+
case rpc.TypeUnsignedInt, rpc.TypeUnsignedLong:
67+
case rpc.TypeFloat, rpc.TypeDouble:
68+
case rpc.TypeBoolean:
69+
default:
70+
return nil, fmt.Errorf("unsupported type: %s", matches[3])
71+
}
72+
6173
return &Func{
6274
Name: matches[1],
6375
Args: args,

datamodel/noise/parser_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func TestParseDef(t *testing.T) {
7373
exp: nil,
7474
err: "invalid generator definition",
7575
},
76+
{
77+
in: "foo(a=1) as banana",
78+
exp: nil,
79+
err: `unsupported type: banana`,
80+
},
7681
}
7782

7883
for _, tt := range tests {

datamodel/parameter.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,18 @@ func (p Parameter) Name() string {
4444
// will be returned.
4545
func (p Parameter) GetValue() string {
4646
if p.gen != nil {
47-
return strconv.FormatFloat(p.gen(), 'f', -1, 64)
47+
switch rpc.NoXSD(p.Type) {
48+
case rpc.TypeInt, rpc.TypeLong:
49+
return strconv.FormatInt(int64(p.gen()), 10)
50+
case rpc.TypeUnsignedInt, rpc.TypeUnsignedLong:
51+
return strconv.FormatUint(uint64(p.gen()), 10)
52+
case rpc.TypeFloat, rpc.TypeDouble:
53+
return strconv.FormatFloat(p.gen(), 'f', -1, 64)
54+
case rpc.TypeBoolean:
55+
return strconv.FormatBool(int(p.gen()) == 1)
56+
default:
57+
return ""
58+
}
4859
}
4960
return p.Value
5061
}

rpc/xsd_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package rpc
22

3+
import (
4+
"strings"
5+
)
6+
37
// List of supported types.
48
const (
59
TypeObject = "object"
@@ -19,6 +23,8 @@ const (
1923
TypeIPv6Address = "IPv6Address"
2024
TypeIPv6Prefix = "IPv6Prefix"
2125
TypeMACAddress = "MACAddress"
26+
TypeFloat = "float"
27+
TypeDouble = "double"
2228

2329
// TypeGenerator is a special type used to define a generator function.
2430
TypeGenerator = "sim:generator"
@@ -28,3 +34,11 @@ const (
2834
func XSD(typ string) string {
2935
return "xsd:" + typ
3036
}
37+
38+
// NoXSD removes the XSD prefix from the type.
39+
func NoXSD(typ string) string {
40+
if strings.HasPrefix(typ, "xsd:") {
41+
return typ[4:]
42+
}
43+
return typ
44+
}

simulator/server.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ func newUDPServer(ctx context.Context, port int, h crHandlerFn) (server, error)
145145
if err != nil {
146146
return nil, fmt.Errorf("get ip address: %w", err)
147147
}
148-
fmt.Println(ip, port)
149148

150149
listener, err := net.ListenUDP("udp4", &net.UDPAddr{
151150
IP: net.ParseIP(ip),

0 commit comments

Comments
 (0)