Skip to content

Commit 66ada09

Browse files
committed
Fix null/list encoding + example
Signed-off-by: Christian Mesh <[email protected]>
1 parent ce1d19a commit 66ada09

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

lib.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,14 @@ end
55
function echo(value)
66
return value
77
end
8+
9+
-- If input array contains single value, return that value, otherwise return the original array.
10+
function normalize(arr)
11+
if type(arr) == "table" and next(arr) == nil then
12+
return {}
13+
end
14+
if type(arr) == "table" and #arr == 0 then
15+
return arr[0]
16+
end
17+
return arr
18+
end

main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,12 @@ func CtyToLua(arg cty.Value, l *lua.State) error {
392392

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

398398
switch t := l.TypeOf(-1); t {
399399
case lua.TypeNil:
400-
return cty.NilVal, nil
400+
return cty.NullVal(cty.DynamicPseudoType), nil
401401
case lua.TypeBoolean:
402402
return cty.BoolVal(l.ToBoolean(-1)), nil
403403
case lua.TypeNumber:
@@ -421,7 +421,7 @@ func LuaToCty(l *lua.State) (cty.Value, error) {
421421
// Decode key (also modifies)
422422
key, ok := l.ToString(-1)
423423
if !ok {
424-
return cty.NilVal, fmt.Errorf("bad table index")
424+
return cty.NullVal(cty.DynamicPseudoType), fmt.Errorf("bad table index")
425425
}
426426

427427
l.Pop(1)
@@ -453,8 +453,8 @@ func LuaToCty(l *lua.State) (cty.Value, error) {
453453
return cty.ObjectVal(mv), nil
454454
}
455455
}
456-
return cty.ListVal(av), nil
456+
return cty.TupleVal(av), nil
457457
default:
458-
return cty.NilVal, fmt.Errorf("unhanded return type %s!", t)
458+
return cty.NullVal(cty.DynamicPseudoType), fmt.Errorf("unhanded return type %s!", t)
459459
}
460460
}

main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ provider "tester" {
1818
output "test" {
1919
value = provider::tester::echo(tomap({"foo": {"bar": 190}}))
2020
}
21+
22+
output "norm_0" {
23+
value = provider::tester::normalize([])
24+
}
25+
26+
output "norm_1" {
27+
value = provider::tester::normalize([1])
28+
}
29+
30+
output "norm_2" {
31+
value = provider::tester::normalize([1, {"foo": "bar"}])
32+
}

0 commit comments

Comments
 (0)