Skip to content

Commit

Permalink
Merge pull request #226 from maksym-nazarenko/handle-more-basic-types…
Browse files Browse the repository at this point in the history
…-in-client-marshalling

support more basic types in marshalling/unmarshalling
  • Loading branch information
ddelnano committed Mar 11, 2024
2 parents 2340745 + 362d0eb commit 0c6320a
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 13 deletions.
13 changes: 7 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,10 @@ func Marshal(c string, s interface{}) []string {
}

switch value.Kind() {
case reflect.Int:
intValue := elem.Field(i).Interface().(int)
cmd = append(cmd, fmt.Sprintf("=%s=%d", mikrotikPropName, intValue))
case reflect.Int64:
intValue := elem.Field(i).Interface().(int64)
cmd = append(cmd, fmt.Sprintf("=%s=%d", mikrotikPropName, intValue))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
cmd = append(cmd, fmt.Sprintf("=%s=%d", mikrotikPropName, elem.Field(i).Interface()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
cmd = append(cmd, fmt.Sprintf("=%s=%d", mikrotikPropName, elem.Field(i).Interface()))
case reflect.String:
stringValue := elem.Field(i).Interface().(string)
cmd = append(cmd, fmt.Sprintf("=%s=%s", mikrotikPropName, stringValue))
Expand Down Expand Up @@ -259,6 +257,9 @@ func parseStruct(v *reflect.Value, sentence proto.Sentence) {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intValue, _ := strconv.Atoi(pair.Value)
field.SetInt(int64(intValue))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uintValue, _ := strconv.ParseUint(pair.Value, 10, 0)
field.SetUint(uint64(uintValue))
}
}
}
Expand Down
86 changes: 84 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ func TestUnmarshal(t *testing.T) {
Name string
NotNamedOwner string `mikrotik:"owner"`
RunCount int `mikrotik:"run-count"`
Allowed bool
Schedule types.MikrotikList
RunCount8 int8 `mikrotik:"run-count8"`
RunCount16 int16 `mikrotik:"run-count16"`
RunCount32 int32 `mikrotik:"run-count32"`
RunCount64 int64 `mikrotik:"run-count64"`
CountUint uint `mikrotik:"run-count-uint"`
CountUint8 uint8 `mikrotik:"run-count-uint8"`
CountUint16 uint16 `mikrotik:"run-count-uint16"`
CountUint32 uint32 `mikrotik:"run-count-uint32"`
CountUint64 uint64 `mikrotik:"run-count-uint64"`

Allowed bool
Schedule types.MikrotikList
}

testCases := []struct {
Expand All @@ -43,6 +53,42 @@ func TestUnmarshal(t *testing.T) {
Key: "run-count",
Value: "3",
},
{
Key: "run-count8",
Value: "-3",
},
{
Key: "run-count16",
Value: "12000",
},
{
Key: "run-count32",
Value: "12000000",
},
{
Key: "run-count64",
Value: "12000000000000",
},
{
Key: "run-count-uint",
Value: "500",
},
{
Key: "run-count-uint8",
Value: "5",
},
{
Key: "run-count-uint16",
Value: "15000",
},
{
Key: "run-count-uint32",
Value: "15000000",
},
{
Key: "run-count-uint64",
Value: "15000000000000000",
},
{
Key: "allowed",
Value: "true",
Expand All @@ -55,6 +101,15 @@ func TestUnmarshal(t *testing.T) {
Name: "testing script",
NotNamedOwner: "admin",
RunCount: 3,
RunCount8: -3,
RunCount16: 12000,
RunCount32: 12000000,
RunCount64: 12000000000000,
CountUint: 500,
CountUint8: 5,
CountUint16: 15000,
CountUint32: 15000000,
CountUint64: 15000000000000000,
Allowed: true,
},
},
Expand Down Expand Up @@ -234,19 +289,46 @@ func TestMarshal(t *testing.T) {
Name string `mikrotik:"name"`
NotNamedOwner string `mikrotik:"owner,extraTagNotUsed"`
RunCount int `mikrotik:"run-count"`
RunCount8 int8 `mikrotik:"run-count8"`
RunCount16 int16 `mikrotik:"run-count16"`
RunCount32 int32 `mikrotik:"run-count32"`
RunCount64 int64 `mikrotik:"run-count64"`
CountUint uint `mikrotik:"run-count-uint"`
CountUint8 uint8 `mikrotik:"run-count-uint8"`
CountUint16 uint16 `mikrotik:"run-count-uint16"`
CountUint32 uint32 `mikrotik:"run-count-uint32"`
CountUint64 uint64 `mikrotik:"run-count-uint64"`
ReadOnlyProp bool `mikrotik:"read-only-prop,readonly"`
Allowed bool `mikrotik:"allowed-or-not"`
}{
Name: "test owner",
NotNamedOwner: "admin",
RunCount: 3,
RunCount8: 10,
RunCount16: 12000,
RunCount32: -12_000_000,
RunCount64: 12_000_000_000_000_000,
CountUint: 15000,
CountUint8: 250,
CountUint16: 15000,
CountUint32: 15_000_000,
CountUint64: 15_000_000_000_000_000,
Allowed: true,
},
expectedCmd: []string{
"/test/owner/add",
"=name=test owner",
"=owner=admin",
"=run-count=3",
"=run-count8=10",
"=run-count16=12000",
"=run-count32=-12000000",
"=run-count64=12000000000000000",
"=run-count-uint=15000",
"=run-count-uint8=250",
"=run-count-uint16=15000",
"=run-count-uint32=15000000",
"=run-count-uint64=15000000000000000",
"=allowed-or-not=yes",
},
},
Expand Down
31 changes: 28 additions & 3 deletions mikrotik/internal/utils/struct_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func copyStruct(ctx context.Context, src, dest interface{}) error {

switch kind := srcFieldType.Type.Kind(); kind {
case reflect.Bool,
reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int8,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64,
reflect.String,
reflect.Slice:
Expand Down Expand Up @@ -111,6 +112,8 @@ func coreTypeToTerraformType(src, dest reflect.Value) error {
switch src.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
tfValue = tftypes.Int64Value(src.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
tfValue = tftypes.Int64Value(int64(src.Uint()))
case reflect.String:
tfValue = tftypes.StringValue(src.String())
case reflect.Bool:
Expand All @@ -127,7 +130,8 @@ func coreTypeToTerraformType(src, dest reflect.Value) error {
switch kind := src.Type().Elem().Kind(); kind {
case reflect.Bool:
tfType = tftypes.BoolType
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
tfType = tftypes.Int64Type
case reflect.String:
tfType = tftypes.StringType
Expand Down Expand Up @@ -186,6 +190,16 @@ func terraformTypeToCoreType(src, dest reflect.Value) error {
sliceType = reflect.TypeOf(int32(0))
case reflect.Int64:
sliceType = reflect.TypeOf(int64(0))
case reflect.Uint:
sliceType = reflect.TypeOf(uint(0))
case reflect.Uint8:
sliceType = reflect.TypeOf(uint8(0))
case reflect.Uint16:
sliceType = reflect.TypeOf(uint16(0))
case reflect.Uint32:
sliceType = reflect.TypeOf(uint32(0))
case reflect.Uint64:
sliceType = reflect.TypeOf(uint64(0))
case reflect.String:
sliceType = reflect.TypeOf("")
default:
Expand Down Expand Up @@ -218,6 +232,16 @@ func terraformTypeToCoreType(src, dest reflect.Value) error {
sliceType = reflect.TypeOf(int32(0))
case reflect.Int64:
sliceType = reflect.TypeOf(int64(0))
case reflect.Uint:
sliceType = reflect.TypeOf(uint(0))
case reflect.Uint8:
sliceType = reflect.TypeOf(uint8(0))
case reflect.Uint16:
sliceType = reflect.TypeOf(uint16(0))
case reflect.Uint32:
sliceType = reflect.TypeOf(uint32(0))
case reflect.Uint64:
sliceType = reflect.TypeOf(uint64(0))
case reflect.String:
sliceType = reflect.TypeOf("")
default:
Expand All @@ -231,7 +255,6 @@ func terraformTypeToCoreType(src, dest reflect.Value) error {
if diag.HasError() {
return fmt.Errorf("%s", diag.Errors())
}

dest.Set(targetPtr.Elem())

return nil
Expand All @@ -252,6 +275,8 @@ func coreTypeToCoreType(src, dest reflect.Value) error {
dest.SetBool(src.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
dest.SetInt(src.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
dest.SetUint(src.Uint())
case reflect.Float32, reflect.Float64:
dest.SetFloat(src.Float())
case reflect.String:
Expand Down
Loading

0 comments on commit 0c6320a

Please sign in to comment.