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

Fix null/list encoding + example #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ end
function echo(value)
return value
end

-- If input array contains single value, return that value, otherwise return the original array.
function normalize(arr)
if type(arr) == "table" and next(arr) == nil then
return {}
end
if type(arr) == "table" and #arr == 0 then
return arr[0]
end
return arr
end
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,12 @@ func CtyToLua(arg cty.Value, l *lua.State) error {

func LuaToCty(l *lua.State) (cty.Value, error) {
if l.IsNone(-1) {
return cty.NilVal, fmt.Errorf("none value should not be returned")
return cty.NullVal(cty.DynamicPseudoType), fmt.Errorf("none value should not be returned")
}

switch t := l.TypeOf(-1); t {
case lua.TypeNil:
return cty.NilVal, nil
return cty.NullVal(cty.DynamicPseudoType), nil
case lua.TypeBoolean:
return cty.BoolVal(l.ToBoolean(-1)), nil
case lua.TypeNumber:
Expand All @@ -421,7 +421,7 @@ func LuaToCty(l *lua.State) (cty.Value, error) {
// Decode key (also modifies)
key, ok := l.ToString(-1)
if !ok {
return cty.NilVal, fmt.Errorf("bad table index")
return cty.NullVal(cty.DynamicPseudoType), fmt.Errorf("bad table index")
}

l.Pop(1)
Expand Down Expand Up @@ -453,8 +453,8 @@ func LuaToCty(l *lua.State) (cty.Value, error) {
return cty.ObjectVal(mv), nil
}
}
return cty.ListVal(av), nil
return cty.TupleVal(av), nil
default:
return cty.NilVal, fmt.Errorf("unhanded return type %s!", t)
return cty.NullVal(cty.DynamicPseudoType), fmt.Errorf("unhanded return type %s!", t)
}
}
12 changes: 12 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ provider "tester" {
output "test" {
value = provider::tester::echo(tomap({"foo": {"bar": 190}}))
}

output "norm_0" {
value = provider::tester::normalize([])
}

output "norm_1" {
value = provider::tester::normalize([1])
}

output "norm_2" {
value = provider::tester::normalize([1, {"foo": "bar"}])
}