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

Refactor for another solution to convert: dataconv #41

Merged
merged 33 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a82ee0f
for go1.18 and starlight v0.0.7
hyorigo Jan 3, 2024
a1341cb
update go.sum
hyorigo Jan 3, 2024
ea9f66f
Update for cmd
hyorigo Jan 3, 2024
c290141
clean and add
hyorigo Jan 3, 2024
212db9c
fix for map any any
hyorigo Jan 3, 2024
392a3bd
move in
hyorigo Jan 8, 2024
f54c9dd
split internal into dataconv
hyorigo Jan 8, 2024
170d1bf
split files
hyorigo Jan 8, 2024
4b04aa6
for individual test files
hyorigo Jan 8, 2024
d9d8f4d
clean up names
hyorigo Jan 8, 2024
89aa335
for go 1.18 in actions
hyorigo Jan 8, 2024
722968d
who encodes
hyorigo Jan 8, 2024
ac8f01e
for dict with number as key in json
hyorigo Jan 8, 2024
7eaf585
for bool dict key
hyorigo Jan 8, 2024
ff3e11b
for func and builtin in json testing
hyorigo Jan 8, 2024
bec8efe
upside down
hyorigo Jan 8, 2024
6739868
for func and builtin in general testing
hyorigo Jan 8, 2024
47caf68
add comments and test funcs
hyorigo Jan 8, 2024
bb4a2dc
test nil interface
hyorigo Jan 8, 2024
b315cb0
how to test that one
hyorigo Jan 8, 2024
b063233
nil map
hyorigo Jan 8, 2024
402db4f
test internal loader
hyorigo Jan 8, 2024
d4f0673
for embbed errros
hyorigo Jan 8, 2024
48433f4
for more marshal tests
hyorigo Jan 8, 2024
51bc486
maybe enough
hyorigo Jan 8, 2024
0dbaca2
for testing 1234
hyorigo Jan 8, 2024
7ecbcf2
for zeros
hyorigo Jan 8, 2024
629dd47
another yet
hyorigo Jan 8, 2024
c2b8980
two errors
hyorigo Jan 8, 2024
77b2d31
go struct chan
hyorigo Jan 9, 2024
f1995bc
for 32
hyorigo Jan 9, 2024
70e6d88
dumps dict then
hyorigo Jan 9, 2024
dd2b41b
for dumps json errors
hyorigo Jan 9, 2024
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
7 changes: 3 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ jobs:
windows-2022
]
go-version: [
1.16.15,
1.17.13,
1.18.10,
1.19.9,
1.20.4,
1.19.13,
1.20.12,
1.21.5,
]
# Steps to execute
steps:
Expand Down
20 changes: 17 additions & 3 deletions cmd/starlet/go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
module github.com/1set/starlet/cmd/starlet

go 1.16
go 1.18

require (
bitbucket.org/ai69/colorlogo v0.1.1
bitbucket.org/neiku/winornot v0.0.4
github.com/1set/gut v0.0.0-20201117175203-a82363231997
github.com/1set/starlet v0.0.2
github.com/1set/starlet v0.0.5
github.com/spf13/pflag v1.0.5
go.starlark.net v0.0.0-20230814145427-12f4cb8177e4
go.starlark.net v0.0.0-20231121155337-90ade8b19d09
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467
)

require (
github.com/1set/starlight v0.0.7 // 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
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rodolfoag/gow32 v0.0.0-20160917004320-d95ff468acf8 // indirect
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
)

replace github.com/1set/starlet => ../..
8 changes: 4 additions & 4 deletions cmd/starlet/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ bitbucket.org/neiku/winornot v0.0.4/go.mod h1:xzKlwxkpGIumHbxwUrB41kFIra8gz/ZZgJ
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.6 h1:pF0BUoGuE4Ni9wjK8JQByKqU6KGsf4sEpnB7ES2m3EU=
github.com/1set/starlight v0.0.6/go.mod h1:Dh/+wQGuhu4GngosT4Otzn5S5Hj+GVlVbeJJmZYX5Wo=
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/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=
github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=
Expand Down Expand Up @@ -54,8 +54,8 @@ github.com/rodolfoag/gow32 v0.0.0-20160917004320-d95ff468acf8 h1:p7tJTb+Rqvp8dS8
github.com/rodolfoag/gow32 v0.0.0-20160917004320-d95ff468acf8/go.mod h1:w/ebPUfAcyZMYjstwPIWTEGSahChHx5R3Y+xElrvxDc=
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-20230814145427-12f4cb8177e4 h1:Ydko8M6UfXgvSpGOnbAjRMQDIvBheUsjBjkm6Azcpf4=
go.starlark.net v0.0.0-20230814145427-12f4cb8177e4/go.mod h1:jxU+3+j+71eXOW14274+SmmuW82qJzl6iZSeqEtTGds=
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=
Expand Down
69 changes: 69 additions & 0 deletions dataconv/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dataconv

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strings"

"go.starlark.net/starlark"
)

var (
emptyStr string
)

// IsEmptyString checks is a starlark string is empty ("" for a go string)
// starlark.String.String performs repr-style quotation, which is necessary
// for the starlark.Value contract but a frequent source of errors in API
// clients. This helper method makes sure it'll work properly
func IsEmptyString(s starlark.String) bool {
return s.String() == `""`
}

// IsInterfaceNil returns true if the given interface is nil.
func IsInterfaceNil(i interface{}) bool {
if i == nil {
return true
}
defer func() { recover() }()
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Struct, reflect.Slice, reflect.Map, reflect.Chan, reflect.Func:
return reflect.ValueOf(i).IsNil()
}
return false
}

// MarshalStarlarkJSON marshals a starlark.Value into a JSON string.
// It first converts the starlark.Value into a Golang value, then marshals it into JSON.
func MarshalStarlarkJSON(data starlark.Value, indent int) (string, error) {
// convert starlark value to a go value
v, err := Unmarshal(data)
if err != nil {
return emptyStr, err
}

// fix map[interface {}]interface {}
if m, ok := v.(map[interface{}]interface{}); ok {
mm := make(map[string]interface{})
for k, v := range m {
mm[fmt.Sprintf("%v", k)] = v
}
v = mm
}

// prepare json encoder
var bf bytes.Buffer
enc := json.NewEncoder(&bf)
enc.SetEscapeHTML(false)
if indent > 0 {
enc.SetIndent("", strings.Repeat(" ", indent))
}

// convert go to string
if err = enc.Encode(v); err != nil {
return emptyStr, err
}
return strings.TrimSpace(bf.String()), nil
}
Loading