Skip to content

Commit

Permalink
convert null values to empty strings and ON/OFF to true/false
Browse files Browse the repository at this point in the history
  • Loading branch information
jcvrabo committed Mar 11, 2024
1 parent 0530b31 commit 0523328
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
14 changes: 14 additions & 0 deletions sources/git_source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ func flattenProperties(prefix string, object interface{}, properties *map[string

errors := err.Errors()

if object == nil {
object = ""
}

t := reflect.ValueOf(object).Kind()
if t == reflect.Pointer {
object = reflect.ValueOf(object).Elem().Interface()
Expand Down Expand Up @@ -318,6 +322,16 @@ func flattenProperties(prefix string, object interface{}, properties *map[string
}
}
default:
if t == reflect.String {
// special string-to-boolean cases
switch strings.ToUpper(object.(string)) {
case "OFF":
object = false
case "ON":
object = true
}
}

if len(prefix) == 0 || prefix[0] != '.' {
(*properties)[""] = object
} else {
Expand Down
9 changes: 8 additions & 1 deletion sources/git_source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git_source

import (
"encoding/json"
"fmt"
"os"
"reflect"
"testing"
Expand All @@ -19,6 +20,10 @@ func TestReadYamlFile(t *testing.T) {
if !areEqual(flattenedProperties, expectedProperties) {
t.Error("Flattened properties are not as expected")
}
// enc := json.NewEncoder(os.Stdout)
// enc.SetIndent("", " ")
//
// enc.Encode(flattenedProperties)
}
}

Expand All @@ -29,11 +34,13 @@ func areEqual(flattened map[string]interface{}, expected map[string]interface{})
for k, v := range expected {
if v2, ok := flattened[k]; !ok {
return false
} else if reflect.TypeOf(v2).Kind() == reflect.Int {
} else if v2 != nil && reflect.TypeOf(v2).Kind() == reflect.Int {
if int(v.(float64)) != v2.(int) {
fmt.Println(k)
return false
}
} else if v2 != v {
fmt.Println(k)
return false
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/configuration-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"test.object.array[1].sub-object.attribute-3": "cluster-2",
"test.object.sub-object.attr-1": "value",
"test.object.sub-object.attr-2": true,
"test.object.sub-object.attr-3": 2
"test.object.sub-object.attr-3": 2,
"test.object.attr-1": "",
"test.object.attr-2": false,
"test.object.attr-3": true
}
3 changes: 3 additions & 0 deletions tests/configuration-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ test:
attr-1: value
attr-2: true
attr-3: 2
attr-1:
attr-2: OFF
attr-3: ON

0 comments on commit 0523328

Please sign in to comment.