Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added function getEnvVar() #196

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
readEnvVarFunction := getReadEnvVarFunction()
// 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,
readEnvVarFunction.ID(): readEnvVarFunction,
bindConstantsFunction.ID(): bindConstantsFunction,
}

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

func getReadEnvVarFunction() schema.CallableFunction {
funcSchema, err := schema.NewCallableFunction(
"readEnvVar",
[]schema.Type{
schema.NewStringSchema(nil, nil, nil), // env var key name
schema.NewStringSchema(nil, nil, nil), // default value
},
schema.NewStringSchema(nil, nil, nil),
false,
schema.NewDisplayValue(
schema.PointerTo("readFile"),
dbutenhof marked this conversation as resolved.
Show resolved Hide resolved
schema.PointerTo(
"Returns the value of a system environment variable.\n"+
"Param 1: The key name of the environment variable.\n"+
dbutenhof marked this conversation as resolved.
Show resolved Hide resolved
"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 TestReadEnvVarFunction(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, "readEnvVar", builtinfunctions.GetFunctions())
readEnvVarFunction := builtinfunctions.GetFunctions()["readEnvVar"]
// Test the present env var
result, err := readEnvVarFunction.Call([]any{knownPresentEnvVarKey, defaultEnvVarValue})
assert.NoError(t, err)
assert.Equals(t, result, any(knownEnvVarValue))
// Test the missing env var
result, err = readEnvVarFunction.Call([]any{knownNotPresentEnvVarKey, defaultEnvVarValue})
assert.NoError(t, err)
assert.Equals(t, result, any(defaultEnvVarValue))
}
Loading