From ec980500745c379fc09f6fabf2986a3581a9a197 Mon Sep 17 00:00:00 2001 From: Jared O'Connell Date: Fri, 5 Jul 2024 18:37:15 -0400 Subject: [PATCH 1/4] Added function readEnvVar() --- internal/builtinfunctions/functions.go | 35 +++++++++++++++++++++ internal/builtinfunctions/functions_test.go | 21 +++++++++++++ 2 files changed, 56 insertions(+) diff --git a/internal/builtinfunctions/functions.go b/internal/builtinfunctions/functions.go index fa3fd05c..1d6ad848 100644 --- a/internal/builtinfunctions/functions.go +++ b/internal/builtinfunctions/functions.go @@ -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() @@ -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, } @@ -529,6 +532,38 @@ 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"), + schema.PointerTo( + "Returns the value of a system environment variable.\n"+ + "Param 1: The key 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 + } else { + 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" diff --git a/internal/builtinfunctions/functions_test.go b/internal/builtinfunctions/functions_test.go index d9bb70cb..90e52244 100644 --- a/internal/builtinfunctions/functions_test.go +++ b/internal/builtinfunctions/functions_test.go @@ -6,6 +6,7 @@ import ( "go.flow.arcalot.io/engine/internal/builtinfunctions" "go.flow.arcalot.io/pluginsdk/schema" "math" + "os" "reflect" "regexp" "strings" @@ -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)) +} From 7bb3787cf261e4da367ab803f86b6049b26f207e Mon Sep 17 00:00:00 2001 From: Jared O'Connell Date: Fri, 5 Jul 2024 18:44:10 -0400 Subject: [PATCH 2/4] Address linter error --- internal/builtinfunctions/functions.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/builtinfunctions/functions.go b/internal/builtinfunctions/functions.go index 1d6ad848..fb339f10 100644 --- a/internal/builtinfunctions/functions.go +++ b/internal/builtinfunctions/functions.go @@ -553,9 +553,8 @@ func getReadEnvVarFunction() schema.CallableFunction { envVarValue, envVarPresent := os.LookupEnv(envVarName) if envVarPresent { return envVarValue - } else { - return defaultValue } + return defaultValue }, ) if err != nil { From c5d625ce00e7f63ab931b2e06138f88b39616c64 Mon Sep 17 00:00:00 2001 From: Jared O'Connell Date: Mon, 8 Jul 2024 21:11:24 -0400 Subject: [PATCH 3/4] Addressed review comments --- internal/builtinfunctions/functions.go | 14 +++++++------- internal/builtinfunctions/functions_test.go | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/builtinfunctions/functions.go b/internal/builtinfunctions/functions.go index fb339f10..4a594f2d 100644 --- a/internal/builtinfunctions/functions.go +++ b/internal/builtinfunctions/functions.go @@ -36,7 +36,7 @@ func GetFunctions() map[string]schema.CallableFunction { splitStringFunction := getSplitStringFunction() loadFileFunction := getReadFileFunction() // OS call functions - readEnvVarFunction := getReadEnvVarFunction() + getEnvVarFunction := getGetEnvVarFunction() // Data transformation functions bindConstantsFunction := getBindConstantsFunction() @@ -59,7 +59,7 @@ func GetFunctions() map[string]schema.CallableFunction { toUpperFunction.ID(): toUpperFunction, splitStringFunction.ID(): splitStringFunction, loadFileFunction.ID(): loadFileFunction, - readEnvVarFunction.ID(): readEnvVarFunction, + getEnvVarFunction.ID(): getEnvVarFunction, bindConstantsFunction.ID(): bindConstantsFunction, } @@ -532,20 +532,20 @@ func getReadFileFunction() schema.CallableFunction { return funcSchema } -func getReadEnvVarFunction() schema.CallableFunction { +func getGetEnvVarFunction() schema.CallableFunction { funcSchema, err := schema.NewCallableFunction( - "readEnvVar", + "getEnvVar", []schema.Type{ - schema.NewStringSchema(nil, nil, nil), // env var key name + 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("readFile"), + schema.PointerTo("getEnvVar"), schema.PointerTo( "Returns the value of a system environment variable.\n"+ - "Param 1: The key name of the 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, ), diff --git a/internal/builtinfunctions/functions_test.go b/internal/builtinfunctions/functions_test.go index 90e52244..5106dc97 100644 --- a/internal/builtinfunctions/functions_test.go +++ b/internal/builtinfunctions/functions_test.go @@ -1022,8 +1022,8 @@ func TestReadEnvVarFunction(t *testing.T) { 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"] + assert.MapContainsKey(t, "getEnvVar", builtinfunctions.GetFunctions()) + readEnvVarFunction := builtinfunctions.GetFunctions()["getEnvVar"] // Test the present env var result, err := readEnvVarFunction.Call([]any{knownPresentEnvVarKey, defaultEnvVarValue}) assert.NoError(t, err) From c52269d06b6dae3794eac3d98d59adbf54557395 Mon Sep 17 00:00:00 2001 From: Jared O'Connell Date: Tue, 9 Jul 2024 09:31:57 -0400 Subject: [PATCH 4/4] Update missed variable names --- internal/builtinfunctions/functions_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/builtinfunctions/functions_test.go b/internal/builtinfunctions/functions_test.go index 5106dc97..5bcb701e 100644 --- a/internal/builtinfunctions/functions_test.go +++ b/internal/builtinfunctions/functions_test.go @@ -1014,7 +1014,7 @@ func TestBuildSchemaNames(t *testing.T) { } } -func TestReadEnvVarFunction(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" @@ -1023,13 +1023,13 @@ func TestReadEnvVarFunction(t *testing.T) { assert.NoError(t, os.Setenv(knownPresentEnvVarKey, knownEnvVarValue)) assert.NoError(t, os.Unsetenv(knownNotPresentEnvVarKey)) assert.MapContainsKey(t, "getEnvVar", builtinfunctions.GetFunctions()) - readEnvVarFunction := builtinfunctions.GetFunctions()["getEnvVar"] + getEnvVarFunction := builtinfunctions.GetFunctions()["getEnvVar"] // Test the present env var - result, err := readEnvVarFunction.Call([]any{knownPresentEnvVarKey, defaultEnvVarValue}) + result, err := getEnvVarFunction.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}) + result, err = getEnvVarFunction.Call([]any{knownNotPresentEnvVarKey, defaultEnvVarValue}) assert.NoError(t, err) assert.Equals(t, result, any(defaultEnvVarValue)) }