Skip to content

Commit bdd1165

Browse files
11 bug fixing and minor improvements (#12)
* fix: wrong server path calculation * feat: now unknown objected are managed as json * test: add test
1 parent 4e40da8 commit bdd1165

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

internal/client/clienttools.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func buildPath(baseUrl string, path string, parameters map[string]string, query
9696
if err != nil {
9797
return nil
9898
}
99-
parsed.Path = path
99+
parsed.Path = parsed.Path + path
100100
parsed.RawQuery = params.Encode()
101101
return parsed
102102
}

internal/restResources/support.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,12 @@ func populateStatusFields(clientInfo *getter.Info, mg *unstructured.Unstructured
422422
for k, v := range *body {
423423
for _, identifier := range clientInfo.Resource.Identifiers {
424424
if k == identifier {
425-
err := unstructured.SetNestedField(mg.Object, text.GenericToString(v), "status", identifier)
425+
stringValue, err := text.GenericToString(v)
426+
if err != nil {
427+
log.Err(err).Msg("Converting value to string")
428+
return err
429+
}
430+
err = unstructured.SetNestedField(mg.Object, stringValue, "status", identifier)
426431
if err != nil {
427432
log.Err(err).Msg("Setting identifier")
428433
return err

internal/text/genericstringer.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
package text
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"reflect"
67
)
78

8-
func GenericToString(i interface{}) string {
9+
func GenericToString(i interface{}) (string, error) {
910
if reflect.TypeOf(i).Kind() == reflect.String {
10-
return i.(string)
11+
return i.(string), nil
1112
}
1213
if reflect.TypeOf(i).Kind() == reflect.Float32 || reflect.TypeOf(i).Kind() == reflect.Float64 {
13-
return fmt.Sprintf("%d", int(i.(float64)))
14+
return fmt.Sprintf("%d", int(i.(float64))), nil
1415
}
1516
if reflect.TypeOf(i).Kind() == reflect.Int || reflect.TypeOf(i).Kind() == reflect.Int32 || reflect.TypeOf(i).Kind() == reflect.Int64 || reflect.TypeOf(i).Kind() == reflect.Uint || reflect.TypeOf(i).Kind() == reflect.Uint32 || reflect.TypeOf(i).Kind() == reflect.Uint64 {
16-
return fmt.Sprintf("%d", i)
17+
return fmt.Sprintf("%d", i), nil
1718
}
1819
if reflect.TypeOf(i).Kind() == reflect.Bool {
19-
return fmt.Sprintf("%t", i.(bool))
20+
return fmt.Sprintf("%t", i.(bool)), nil
2021
}
21-
return ""
22+
b, err := json.Marshal(i)
23+
return string(b), err
2224
}

internal/text/genericstringer_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package text
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGenericToString(t *testing.T) {
8+
tests := []struct {
9+
input interface{}
10+
expected string
11+
hasError bool
12+
}{
13+
{"hello", "hello", false},
14+
{123, "123", false},
15+
{123.456, "123", false},
16+
{true, "true", false},
17+
{false, "false", false},
18+
{[]int{1, 2, 3}, "[1,2,3]", false},
19+
}
20+
21+
for _, test := range tests {
22+
result, err := GenericToString(test.input)
23+
if (err != nil) != test.hasError {
24+
t.Errorf("GenericToString(%v) returned error: %v, expected error: %v", test.input, err, test.hasError)
25+
}
26+
if result != test.expected {
27+
t.Errorf("GenericToString(%v) = %v, expected %v", test.input, result, test.expected)
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)