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

terminal/starbind: fix starlark conversion of named consts #3802

Merged
merged 2 commits into from
Sep 3, 2024
Merged
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
28 changes: 25 additions & 3 deletions _fixtures/testvariables2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,23 @@ import (
"unsafe"
)

type Byte byte
type String string
type (
Byte byte
String string
Uint32 uint32
Int64 int64
Bool bool
Float64 float64
)

const (
ByteConst Byte = 255
StringConst String = "hi"
UintConst Uint32 = 42
IntConst Int64 = 9001
BoolConst Bool = true
FloatConst Float64 = 3.14
)

type astruct struct {
A int
Expand Down Expand Up @@ -394,6 +409,13 @@ func main() {
var ptrinf2 pptr
ptrinf2 = &ptrinf2

enum1 := ByteConst
enum2 := StringConst
enum3 := UintConst
enum4 := IntConst
enum5 := BoolConst
enum6 := FloatConst

var amb1 = 1
runtime.Breakpoint()
for amb1 := 0; amb1 < 10; amb1++ {
Expand All @@ -404,5 +426,5 @@ func main() {
longslice := make([]int, 100, 100)

runtime.Breakpoint()
fmt.Println(i1, i2, i3, p1, pp1, amb1, s1, s3, a0, a1, p2, p3, s2, as1, str1, f1, fn1, fn2, nilslice, nilptr, ch1, chnil, m1, mnil, m2, m3, m4, m5, upnil, up1, i4, i5, i6, err1, err2, errnil, iface1, iface2, ifacenil, arr1, parr, cpx1, const1, iface3, iface4, recursive1, recursive1.x, iface5, iface2fn1, iface2fn2, bencharr, benchparr, mapinf, mainMenu, b, b2, sd, anonstruct1, anonstruct2, anoniface1, anonfunc, mapanonstruct1, ifacearr, efacearr, ni8, ni16, ni32, ni64, pinf, ninf, nan, zsvmap, zsslice, zsvar, tm, rettm, errtypednil, emptyslice, emptymap, byteslice, bytestypeslice, runeslice, bytearray, bytetypearray, runearray, longstr, nilstruct, as2, as2.NonPointerReceiverMethod, s4, iface2map, issue1578, ll, unread, w2, w3, w4, w5, longarr, longslice, val, m6, m7, cl, tim1, tim2, typedstringvar, namedA1, namedA2, astructName1(namedA2), badslice, tim3, int3chan, longbyteslice)
fmt.Println(i1, i2, i3, p1, pp1, amb1, s1, s3, a0, a1, p2, p3, s2, as1, str1, f1, fn1, fn2, nilslice, nilptr, ch1, chnil, m1, mnil, m2, m3, m4, m5, upnil, up1, i4, i5, i6, err1, err2, errnil, iface1, iface2, ifacenil, arr1, parr, cpx1, const1, iface3, iface4, recursive1, recursive1.x, iface5, iface2fn1, iface2fn2, bencharr, benchparr, mapinf, mainMenu, b, b2, sd, anonstruct1, anonstruct2, anoniface1, anonfunc, mapanonstruct1, ifacearr, efacearr, ni8, ni16, ni32, ni64, pinf, ninf, nan, zsvmap, zsslice, zsvar, tm, rettm, errtypednil, emptyslice, emptymap, byteslice, bytestypeslice, runeslice, bytearray, bytetypearray, runearray, longstr, nilstruct, as2, as2.NonPointerReceiverMethod, s4, iface2map, issue1578, ll, unread, w2, w3, w4, w5, longarr, longslice, val, m6, m7, cl, tim1, tim2, typedstringvar, namedA1, namedA2, astructName1(namedA2), badslice, tim3, int3chan, longbyteslice, enum1, enum2, enum3, enum4, enum5, enum6)
}
4 changes: 2 additions & 2 deletions pkg/terminal/starbind/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ func (env *Env) variableValueToStarlarkValue(v *api.Variable, top bool) (starlar
case reflect.String:
return starlark.String(v.Value), nil
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
n, _ := strconv.ParseInt(v.Value, 0, 64)
n, _ := strconv.ParseInt(api.ExtractIntValue(v.Value), 0, 64)
return starlark.MakeInt64(n), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint, reflect.Uintptr:
n, _ := strconv.ParseUint(v.Value, 0, 64)
n, _ := strconv.ParseUint(api.ExtractIntValue(v.Value), 0, 64)
return starlark.MakeUint64(n), nil
case reflect.Bool:
n, _ := strconv.ParseBool(v.Value)
Expand Down
8 changes: 8 additions & 0 deletions pkg/terminal/starlark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ func TestStarlarkVariable(t *testing.T) {
{`v = eval(cur_scope(), "as1").Variable; print(v.Value.A)`, "1"},
{`v = eval(None, "as1", default_load_config()).Variable; print(v.Value.A)`, "1"},

// named constant values
{`v = eval(None, "enum1").Variable; print(v.Value)`, "255"},
{`v = eval(None, "enum2").Variable; print(v.Value)`, "hi"},
{`v = eval(None, "enum3").Variable; print(v.Value)`, "42"},
{`v = eval(None, "enum4").Variable; print(v.Value)`, "9001"},
{`v = eval(None, "enum5").Variable; print(v.Value)`, "True"},
{`v = eval(None, "enum6").Variable; print(v.Value)`, "3.14"},

// From starlark.md's examples
{`v = eval(None, "s2").Variable; print(v.Value[0])`, "main.astruct {A: 1, B: 2}"},
{`v = eval(None, "s2").Variable; print(v.Value[1].A)`, "3"},
Expand Down