Skip to content

Commit

Permalink
Added function getEnvVar() (#196)
Browse files Browse the repository at this point in the history
* Added function readEnvVar()

* Address linter error

* Addressed review comments

* Update missed variable names
  • Loading branch information
jaredoconnell committed Jul 9, 2024
1 parent b16b8ff commit ee454cc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
34 changes: 34 additions & 0 deletions internal/builtinfunctions/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func GetFunctions() map[string]schema.CallableFunction {
toUpperFunction := getToUpperFunction()
splitStringFunction := getSplitStringFunction()
loadFileFunction := getReadFileFunction()
// OS call functions
getEnvVarFunction := getGetEnvVarFunction()
// Data transformation functions
bindConstantsFunction := getBindConstantsFunction()

Expand All @@ -57,6 +59,7 @@ func GetFunctions() map[string]schema.CallableFunction {
toUpperFunction.ID(): toUpperFunction,
splitStringFunction.ID(): splitStringFunction,
loadFileFunction.ID(): loadFileFunction,
getEnvVarFunction.ID(): getEnvVarFunction,
bindConstantsFunction.ID(): bindConstantsFunction,
}

Expand Down Expand Up @@ -529,6 +532,37 @@ func getReadFileFunction() schema.CallableFunction {
return funcSchema
}

func getGetEnvVarFunction() schema.CallableFunction {
funcSchema, err := schema.NewCallableFunction(
"getEnvVar",
[]schema.Type{
schema.NewStringSchema(nil, nil, nil), // env var name
schema.NewStringSchema(nil, nil, nil), // default value
},
schema.NewStringSchema(nil, nil, nil),
false,
schema.NewDisplayValue(
schema.PointerTo("getEnvVar"),
schema.PointerTo(
"Returns the value of a system environment variable.\n"+
"Param 1: The name of the environment variable.\n"+
"Param 2: The value to return if the environment variable is not present."),
nil,
),
func(envVarName string, defaultValue string) string {
envVarValue, envVarPresent := os.LookupEnv(envVarName)
if envVarPresent {
return envVarValue
}
return defaultValue
},
)
if err != nil {
panic(err)
}
return funcSchema
}

// CombinedObjPropertyConstantName is the identifier string key that points to the container of constant or repeated values.
const CombinedObjPropertyConstantName = "constant"

Expand Down
21 changes: 21 additions & 0 deletions internal/builtinfunctions/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"go.flow.arcalot.io/engine/internal/builtinfunctions"
"go.flow.arcalot.io/pluginsdk/schema"
"math"
"os"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -1012,3 +1013,23 @@ func TestBuildSchemaNames(t *testing.T) {
})
}
}

func TestGetEnvVarFunction(t *testing.T) {
// Set the env variables to get below.
knownPresentEnvVarKey := "test_known_present_env_var_key"
knownNotPresentEnvVarKey := "test_known_not_present_env_var_key"
knownEnvVarValue := "known value"
defaultEnvVarValue := "default"
assert.NoError(t, os.Setenv(knownPresentEnvVarKey, knownEnvVarValue))
assert.NoError(t, os.Unsetenv(knownNotPresentEnvVarKey))
assert.MapContainsKey(t, "getEnvVar", builtinfunctions.GetFunctions())
getEnvVarFunction := builtinfunctions.GetFunctions()["getEnvVar"]
// Test the present env var
result, err := getEnvVarFunction.Call([]any{knownPresentEnvVarKey, defaultEnvVarValue})
assert.NoError(t, err)
assert.Equals(t, result, any(knownEnvVarValue))
// Test the missing env var
result, err = getEnvVarFunction.Call([]any{knownNotPresentEnvVarKey, defaultEnvVarValue})
assert.NoError(t, err)
assert.Equals(t, result, any(defaultEnvVarValue))
}

0 comments on commit ee454cc

Please sign in to comment.