Skip to content

Commit

Permalink
addressing nil results in jq
Browse files Browse the repository at this point in the history
  • Loading branch information
xpositivityx committed Mar 22, 2024
1 parent 7b62d0c commit b85a3d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
20 changes: 20 additions & 0 deletions pkg/bundle/environment_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,23 @@ func TestMultipleValues(t *testing.T) {
t.Errorf("Wanted %s but got %s", want, asJSON)
}
}

func TestNilResult(t *testing.T) {
query := map[string]string{
"DATABASE_URL": ".connections.postgres.data.authentication.userName",
}

got := bundle.ParseEnvironmentVariables(params, query)

asJSON, err := json.Marshal(got)

if err != nil {
t.Fatalf("Failed to unmarshal result")
}

want := "{\"DATABASE_URL\":{\"error\":\"failed to produce a result\",\"value\":\"\"}}"

if string(asJSON) != want {
t.Errorf("Wanted %s but got %s", want, asJSON)
}
}
12 changes: 8 additions & 4 deletions pkg/bundle/environment_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,25 @@ func ParseEnvironmentVariables(params map[string]interface{}, query map[string]s
for {
v, ok := iter.Next()

if !ok || v == nil {
result.Error = "Failed to return a result"
results[k] = result
if !ok {
break
}

if valueErr, valOk := v.(error); valOk {
result.Error = fmt.Sprint(valueErr)
results[k] = result
break
continue
}
ofType := reflect.TypeOf(v)

var castValue string

if ofType == nil {
result.Error = "failed to produce a result"
results[k] = result
continue
}

switch ofType.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
castValue = fmt.Sprintf("%d", v)
Expand Down

0 comments on commit b85a3d9

Please sign in to comment.