Skip to content

Commit

Permalink
Support toml source (#4)
Browse files Browse the repository at this point in the history
* fix(bin): remove usless print

* feat(source): Support toml source

* build(go.mod): go get stretchr/testify
  • Loading branch information
tenfyzhong committed Apr 20, 2024
1 parent 8adc60b commit 88757a5
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ GLOBAL OPTIONS:
--input file, -i file Input file, if not set, it will read from stdio
--rc Read input from clipboard (default: false)
--src type, -s type The source data type, it will use the suffix of the input file if not set, available value: `[json,yaml,proto,thrift,go,csv,xml]`
--src type, -s type The source data type, it will use the suffix of the input file if not set, available value: `[json,yaml,proto,thrift,go,csv,xml,toml]`
--xml-attribute-tag-prefix prefix Add prefix to xml attribute tag in go field, only works for xml source and go destination (default: ,)
--xml-content-tag-prefix prefix Add prefix to xml content tag in go field, only works for xml source and go destination
Expand Down
2 changes: 1 addition & 1 deletion cmd/st2/completions/st2.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ complete st2 -f
complete st2 -r -f -s r -l root -d 'The root struct name (default: Root)'
complete st2 -r -F -s i -l input -d 'Input file, if not set, it will read from stdio'
complete st2 -l rc -d 'Read input from clipboard'
complete st2 -r -f -s s -l src -a "json yaml proto thrift go csv xml" -d 'The source data type, it will use the suffix of the input file if not set'
complete st2 -r -f -s s -l src -a "json yaml proto thrift go csv xml toml" -d 'The source data type, it will use the suffix of the input file if not set'
complete st2 -r -f -s d -l dst -a "go proto thrift" -d 'The destination data type, it will use the suffix of the output file if not set'
complete st2 -r -F -s o -l output -d 'Output file, if not set, it will write to stdout'
complete st2 -l wc -d 'Write output to clipboard'
Expand Down
1 change: 0 additions & 1 deletion cmd/st2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ func action(ctx context.Context, cmd *cli.Command) error {
AttributeTagPrefix: cmd.String(flagXMLAttributeTagPrefix),
},
)
fmt.Printf("st2Ctx:%+v\n", st2Ctx)

reader, err := getReader(cmd)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions cmd/st2/testdata/a.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
url = "https://grafana.home/api/annotations"
api_key = "glsa_secret_2b3226ac"
cluster = "Sandbox"

[spikes]
dashboard = "84357279284502"
panel = 23

[logs]
dashboard = "84357279284503"
panel = 24

[cpu]
dashboard = "84357279284504"
panel = 25
4 changes: 4 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
LangYaml = "yaml"
LangYml = "yml"
LangXML = "xml"
LangToml = "toml"

RootDefault = "Root"

Expand Down Expand Up @@ -83,6 +84,9 @@ var (
{
Lang: LangXML,
},
{
Lang: LangToml,
},
}

DestinationLangs = []Lang{
Expand Down
2 changes: 2 additions & 0 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func CreateParser(ctx Context) Parse {
return NewCsvParser(ctx)
case LangXML:
return NewXMLParser(ctx)
case LangToml:
return NewTomlParser(ctx)
}
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ module github.com/tenfyzhong/st2
go 1.18

require (
github.com/basgys/goxml2json v1.1.0
github.com/cloudwego/thriftgo v0.2.4
github.com/iancoleman/strcase v0.2.0
github.com/json-iterator/go v1.1.12
github.com/stretchr/testify v1.8.4
github.com/pelletier/go-toml/v2 v2.2.1
github.com/stretchr/testify v1.9.0
github.com/urfave/cli/v3 v3.0.0-alpha8
github.com/yoheimuta/go-protoparser/v4 v4.7.0
golang.design/x/clipboard v0.6.3
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/basgys/goxml2json v1.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.2.1 h1:9TA9+T8+8CUCO2+WYnDLCgrYi9+omqKXyjDtosvtEhg=
github.com/pelletier/go-toml/v2 v2.2.1/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v3 v3.0.0-alpha8 h1:H+qxFPoCkGzdF8KUMs2fEOZl5io/1QySgUiGfar8occ=
github.com/urfave/cli/v3 v3.0.0-alpha8/go.mod h1:0kK/RUFHyh+yIKSfWxwheGndfnrvYSmYFVeKCh03ZUc=
github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI=
Expand Down Expand Up @@ -77,5 +86,6 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19 changes: 19 additions & 0 deletions toml_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package st2

import (
"github.com/pelletier/go-toml/v2"
)

type TomlUnmarshalTagFormat struct{}

func (t TomlUnmarshalTagFormat) Unmarshal(data []byte, v any) error {
return toml.Unmarshal(data, v)
}

func (t TomlUnmarshalTagFormat) TagFormat() string {
return `toml:"%s"`
}

func NewTomlParser(ctx Context) *StructuredParser {
return NewStructuredParser(ctx, &TomlUnmarshalTagFormat{})
}

0 comments on commit 88757a5

Please sign in to comment.