Skip to content

Commit

Permalink
set custom struct tag, fix permission lint, update v6 flag (#42)
Browse files Browse the repository at this point in the history
* try to fix permission lint

* for starlight 0.0.8 version

* better naming for kf

* json post 123:456

* use custom tag in machine

* Testing Custom tag for json
  • Loading branch information
hyorigo authored Jan 12, 2024
1 parent cbe5a88 commit aaa198b
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 81 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defaults:
run:
shell: bash

permissions: read-all

jobs:
build:
name: Test with ${{ matrix.go-version }} on ${{ matrix.vm-os }}
Expand All @@ -31,6 +33,8 @@ jobs:
1.20.12,
1.21.5,
]
permissions:
contents: read
# Steps to execute
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion call.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (m *Machine) Call(name string, args ...interface{}) (out interface{}, err e
// convert arguments
sl := starlark.Tuple{}
for _, arg := range args {
sv, err := convert.ToValue(arg)
sv, err := convert.ToValueWithTag(arg, m.customTag)
if err != nil {
return nil, errorStarlightConvert("args", err)
}
Expand Down
17 changes: 17 additions & 0 deletions call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func TestMachine_Call_Preconditions(t *testing.T) {
}

func TestMachine_Call_Functions(t *testing.T) {
type mockData struct {
Apple string `json:"app"`
Banana int64 `json:"bana"`
Coconut bool `json:"coco"`
}
tests := []struct {
name string
code string
Expand Down Expand Up @@ -165,12 +170,24 @@ def work(x, y):
args: []interface{}{1, 2},
wantErr: `starlark: call: panic: as expected`,
},
{
name: "custom tag for arg",
args: []interface{}{
mockData{Apple: "red", Banana: 2, Coconut: true},
},
code: `
def work(x):
return "apple is " + x.app + ", banana is " + str(x.bana) + ", coconut is " + str(x.coco)
`,
want: "apple is red, banana is 2, coconut is True",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// prepare to load
m := starlet.NewDefault()
m.SetCustomTag("json")
_, err := m.RunScript([]byte(tt.code), map[string]interface{}{
"panic": starlark.NewBuiltin("panic", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
panic(errors.New("as expected"))
Expand Down
2 changes: 1 addition & 1 deletion cmd/starlet/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
)

require (
github.com/1set/starlight v0.0.7 // indirect
github.com/1set/starlight v0.0.8 // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/gonutz/w32 v1.0.0 // indirect
Expand Down
71 changes: 2 additions & 69 deletions cmd/starlet/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,19 @@ bitbucket.org/ai69/colorlogo v0.1.1 h1:5XChDTGcnO7g7Poomv8sD2KR6mtEjNQzI0QE10Nia
bitbucket.org/ai69/colorlogo v0.1.1/go.mod h1:opCygeOoMcE5aPB2AQenx2dEh6BgYD1WmmlJ3vbOTio=
bitbucket.org/neiku/winornot v0.0.4 h1:I3XCWgA5DP93Cy4Mip5UmUlboymnFvb18q9UU7gl9lo=
bitbucket.org/neiku/winornot v0.0.4/go.mod h1:xzKlwxkpGIumHbxwUrB41kFIra8gz/ZZgJzltE3TArA=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/1set/gut v0.0.0-20201117175203-a82363231997 h1:za2jSkE1Rx56hTzBko3ZZ4gA/nq+rA/jVovWuAF4jyo=
github.com/1set/gut v0.0.0-20201117175203-a82363231997/go.mod h1:DpCCAL0dgBMQdiqPUIIRpdU9zNcIZwJjW+L/8Mb30mw=
github.com/1set/starlight v0.0.7 h1:OSXHuebIQpybQGo16v0JuXKoePfjLVaH2RxMKID22Cg=
github.com/1set/starlight v0.0.7/go.mod h1:qdWUluPSV8w9jLWdh3CRinNCJovSL6TuPc+DB9CbeKM=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/1set/starlight v0.0.8 h1:s86EmbDySwEjFOZTCcDoiC6CCgCubxl19fICNrCJ/EM=
github.com/1set/starlight v0.0.8/go.mod h1:qdWUluPSV8w9jLWdh3CRinNCJovSL6TuPc+DB9CbeKM=
github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/gonutz/w32 v1.0.0 h1:3t1z6ZfkFvirjFYBx9pHeHBuKoN/VBVk9yHb/m2Ll/k=
github.com/gonutz/w32 v1.0.0/go.mod h1:Rc/YP5K9gv0FW4p6X9qL3E7Y56lfMflEol1fLElfMW4=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
Expand All @@ -47,7 +23,6 @@ github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWV
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=
github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rodolfoag/gow32 v0.0.0-20160917004320-d95ff468acf8 h1:p7tJTb+Rqvp8dS82oMnL1M1Yt9ersQyJU7E1C8Bl+7Q=
Expand All @@ -56,51 +31,9 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
go.starlark.net v0.0.0-20231121155337-90ade8b19d09 h1:hzy3LFnSN8kuQK8h9tHl4ndF6UruMj47OqwqsS+/Ai4=
go.starlark.net v0.0.0-20231121155337-90ade8b19d09/go.mod h1:LcLNIzVOMp4oV+uusnpk+VU+SzXaJakUuBjoCSWH5dM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
8 changes: 4 additions & 4 deletions dataconv/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func Unmarshal(x starlark.Value) (val interface{}, err error) {
keys []interface{}
vals []interface{}
// use interface{} as key type if found one key is not a string
ki bool
keyIf bool
)
for _, k := range v.Keys() {
dictVal, _, err = v.Get(k)
Expand All @@ -148,7 +148,7 @@ func Unmarshal(x starlark.Value) (val interface{}, err error) {

if _, ok := kval.(string); !ok {
// found key as not a string
ki = true
keyIf = true
}

keys = append(keys, kval)
Expand All @@ -160,14 +160,14 @@ func Unmarshal(x starlark.Value) (val interface{}, err error) {
ri := map[interface{}]interface{}{}
for i, key := range keys {
// key as interface
if ki {
if keyIf {
ri[key] = vals[i]
} else {
rs[key.(string)] = vals[i]
}
}

if ki {
if keyIf {
val = ri // map[interface{}]interface{}
} else {
val = rs // map[string]interface{}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/1set/starlet
go 1.18

require (
github.com/1set/starlight v0.0.7
github.com/1set/starlight v0.0.8
go.starlark.net v0.0.0-20231121155337-90ade8b19d09
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/1set/starlight v0.0.7 h1:OSXHuebIQpybQGo16v0JuXKoePfjLVaH2RxMKID22Cg=
github.com/1set/starlight v0.0.7/go.mod h1:qdWUluPSV8w9jLWdh3CRinNCJovSL6TuPc+DB9CbeKM=
github.com/1set/starlight v0.0.8 h1:s86EmbDySwEjFOZTCcDoiC6CCgCubxl19fICNrCJ/EM=
github.com/1set/starlight v0.0.8/go.mod h1:qdWUluPSV8w9jLWdh3CRinNCJovSL6TuPc+DB9CbeKM=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down
2 changes: 1 addition & 1 deletion internal/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
package internal

// StarletVersion is the current version of Starlet.
const StarletVersion = "v0.0.5"
const StarletVersion = "v0.0.6"
12 changes: 12 additions & 0 deletions lib/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,18 @@ func TestLoadModule_HTTP(t *testing.T) {
assert.eq('{"a":"b","c":"d"}' in b, True)
`),
},
{
name: `POST JSON Dict Number`,
script: itn.HereDoc(`
load('http', 'post')
res = post(test_server_url, json_body={ 123: 456, 789: 0.123})
assert.eq(res.status_code, 200)
b = res.body()
assert.eq(b.startswith("POST "), True)
assert.eq('/json' in b, True)
assert.eq('{"123":456,"789":0.123}' in b, True)
`),
},
{
name: `POST JSON Struct`,
script: itn.HereDoc(`
Expand Down
9 changes: 9 additions & 0 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Machine struct {
allowRecursion bool
enableInConv bool
enableOutConv bool
customTag string
// source code
scriptName string
scriptContent []byte
Expand Down Expand Up @@ -246,6 +247,14 @@ func (m *Machine) SetOutputConversionEnabled(enabled bool) {
m.enableOutConv = enabled
}

// SetCustomTag sets the custom annotation tag of Go struct fields for Starlark.
func (m *Machine) SetCustomTag(tag string) {
m.mu.Lock() // Locking to avoid concurrent access
defer m.mu.Unlock()

m.customTag = tag
}

// Export returns the current variables of the Starlark runtime environment.
func (m *Machine) Export() StringAnyMap {
m.mu.RLock()
Expand Down
2 changes: 1 addition & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func (m *Machine) Reset() {
// convertInput converts a StringAnyMap to a starlark.StringDict, usually for output variable.
func (m *Machine) convertInput(a StringAnyMap) (starlark.StringDict, error) {
if m.enableInConv {
return convert.MakeStringDict(a)
return convert.MakeStringDictWithTag(a, m.customTag)
}
return castStringAnyMapToStringDict(a)
}
Expand Down
11 changes: 10 additions & 1 deletion run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,24 @@ func Test_DefaultMachine_Run_LoadNonExist(t *testing.T) {
}

func Test_Machine_Run_Globals(t *testing.T) {
type dataMore struct {
More string `json:"one"`
Extra int `json:"two"`
}
sm := starlark.NewDict(1)
_ = sm.SetKey(starlark.String("bee"), starlark.MakeInt(2))
m := starlet.NewWithNames(map[string]interface{}{
"a": 2,
"c": sm,
"d": &dataMore{More: "more", Extra: 5},
}, nil, nil)
// set code
code := `
c = 100
b = a * 10 + c
e = d.one + str(d.two)
`
m.SetCustomTag("json")
m.SetScript("test.star", []byte(code), nil)
// run
out, err := m.Run()
Expand All @@ -209,7 +216,9 @@ b = a * 10 + c
if out == nil {
t.Errorf("unexpected nil output")
} else if out["b"].(int64) != int64(120) {
t.Errorf("unexpected output: %v", out)
t.Errorf("unexpected output b: %v", out)
} else if out["e"].(string) != "more5" {
t.Errorf("unexpected output e: %v", out)
}
}

Expand Down

0 comments on commit aaa198b

Please sign in to comment.