Skip to content

Commit

Permalink
Add toml support for config read (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
laojianzi authored Sep 2, 2020
1 parent c78928d commit a1927a1
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 25 deletions.
20 changes: 16 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"autoAPI/config/fields/docker"
"encoding/json"
"errors"
"github.com/pelletier/go-toml"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"io/ioutil"
Expand All @@ -15,9 +16,9 @@ import (
)

type Config struct {
Docker *docker.Docker `yaml:"docker" json:"docker"`
CICD *cicd.CICD `yaml:"cicd" json:"cicd"`
Database *database.Database `yaml:"database" json:"database"`
Docker *docker.Docker `yaml:"docker" json:"docker" toml:"docker"`
CICD *cicd.CICD `yaml:"cicd" json:"cicd" toml:"cicd"`
Database *database.Database `yaml:"database" json:"database" toml:"database"`
}

func FromCommandLine(c *cli.Context) (*Config, error) {
Expand Down Expand Up @@ -88,6 +89,15 @@ func FromJson(data []byte) (*Config, error) {
return &result, err
}

// FromToml scan to config from toml format content
func FromToml(data []byte) (*Config, error) {
var result Config
err := toml.Unmarshal(data, &result)
return &result, err
}

// FromConfigFile scan to config from file content
// only support json/yml/yaml and toml now
func FromConfigFile(path string) (*Config, error) {
file, err := os.Open(path)
if err != nil {
Expand All @@ -102,8 +112,10 @@ func FromConfigFile(path string) (*Config, error) {
return FromJson(content)
case ".yaml", ".yml":
return FromYaml(content)
case ".toml":
return FromToml(content)
default:
return nil, errors.New("only support json or yaml now")
return nil, errors.New("only support json/yml/yaml and toml now")
}
}

Expand Down
4 changes: 2 additions & 2 deletions config/fields/cicd/cicd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cicd
import "github.com/urfave/cli/v2"

type CICD struct {
GithubAction *bool `yaml:"GitHubAction" json:"GitHubAction"`
K8s *bool `yaml:"k8s" json:"k8s"`
GithubAction *bool `yaml:"GitHubAction" json:"GitHubAction" toml:"GitHubAction"`
K8s *bool `yaml:"k8s" json:"k8s" toml:"k8s"`
}

func (cicd *CICD) MergeWithDefault() error {
Expand Down
18 changes: 9 additions & 9 deletions config/fields/database/complex/complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import (
)

type QueryResult struct {
Array bool `yaml:"array" json:"array"`
Fields []field.Field `yaml:"fields" json:"fields"`
Array bool `yaml:"array" json:"array" toml:"array"`
Fields []field.Field `yaml:"fields" json:"fields" toml:"fields"`
}

type QueryParam struct {
OnThis *withCase.WithCase `yaml:"onThis,omitempty" json:"onThis,omitempty"`
Name *withCase.WithCase `yaml:"name,omitempty" json:"name,omitempty"`
Type string `yaml:"type" json:"type"`
OnThis *withCase.WithCase `yaml:"onThis,omitempty" json:"onThis,omitempty" toml:"onThis,omitempty"`
Name *withCase.WithCase `yaml:"name,omitempty" json:"name,omitempty" toml:"name,omitempty"`
Type string `yaml:"type" json:"type" toml:"type"`
}

type Complex struct {
Name withCase.WithCase `yaml:"name" json:"name"`
SQL string `yaml:"sql" json:"sql"`
Params []QueryParam `yaml:"params" json:"params"`
Result QueryResult `yaml:"result" json:"result"`
Name withCase.WithCase `yaml:"name" json:"name" toml:"name"`
SQL string `yaml:"sql" json:"sql" toml:"sql"`
Params []QueryParam `yaml:"params" json:"params" toml:"params"`
Result QueryResult `yaml:"result" json:"result" toml:"result"`
}

func (complex *Complex) Validate() error {
Expand Down
6 changes: 3 additions & 3 deletions config/fields/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
)

type Database struct {
DBEngine *string `yaml:"dbengine" json:"dbengine"`
Url *string `yaml:"url" json:"url"`
Table *table.Table `yaml:",inline" json:",inline"`
DBEngine *string `yaml:"dbengine" json:"dbengine" toml:"dbengine"`
Url *string `yaml:"url" json:"url" toml:"url"`
Table *table.Table `yaml:",inline" json:",inline" toml:",inline"`
}

var Default = Database{}
Expand Down
4 changes: 2 additions & 2 deletions config/fields/database/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package field
import "autoAPI/utility/withCase"

type Field struct {
Name withCase.WithCase `yaml:"name" json:"name"`
Type string `yaml:"type" json:"type"`
Name withCase.WithCase `yaml:"name" json:"name" toml:"name"`
Type string `yaml:"type" json:"type" toml:"type"`
}
6 changes: 3 additions & 3 deletions config/fields/database/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

type Table struct {
Name *withCase.WithCase `yaml:"tablename" json:"tablename"`
Fields []field.Field `yaml:"fields" json:"fields"`
Complex []complex.Complex `yaml:"complex" json:"complex"`
Name *withCase.WithCase `yaml:"tablename" json:"tablename" toml:"tablename"`
Fields []field.Field `yaml:"fields" json:"fields" toml:"fields"`
Complex []complex.Complex `yaml:"complex" json:"complex" toml:"complex"`
}

func (table *Table) MergeWith(other *Table) {
Expand Down
4 changes: 2 additions & 2 deletions config/fields/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type Docker struct {
Username *string `yaml:"username" json:"username"`
Tag *string `yaml:"tag" json:"tag"`
Username *string `yaml:"username" json:"username" toml:"username"`
Tag *string `yaml:"tag" json:"tag" toml:"tag"`
}

func (docker *Docker) MergeWith(other *Docker) {
Expand Down
57 changes: 57 additions & 0 deletions example/student.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[docker]
username = 'zhoudian64'
tag = '0.0.1'

[cicd]
GitHubAction = true
k8s = true

[database]
dbengine = 'pgsql'
url = 'postgresql://localhost:5432/postgres?sslmode=disable'

[database.table]
tablename = 'student'

[[database.table.fields]]
name = 'name'
type = 'varchar(24)'

[[database.table.fields]]
name = 'birthday'
type = 'date'

[[database.table.fields]]
name = 'school_id'
type = 'int'

[[database.table.complex]]
name = 'schools'
sql = 'SELECT school.id, name, place FROM school, school_student, school_date WHERE school_student.student_id = $1 AND school_date.start_date >= $2 AND school_date.end_date <= $3 AND school_student.school_id = school.id'

[[database.table.complex.params]]
onThis = 'id'
type = 'int'

[[database.table.complex.params]]
name = 'start_date'
type = 'varchar(24)'

[[database.table.complex.params]]
name = 'end_date'
type = 'varchar(24)'

[database.table.complex.result]
array = true

[[database.table.complex.result.fields]]
name = 'id'
type = 'bigserial'

[[database.table.complex.result.fields]]
name = 'name'
type = 'varchar(24)'

[[database.table.complex.result.fields]]
name = 'place'
type = 'varchar(128)'
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/golang/protobuf v1.4.2 // indirect
github.com/iancoleman/strcase v0.1.0
github.com/lib/pq v1.8.0
github.com/pelletier/go-toml v1.8.0
github.com/pingcap/parser v0.0.0-20200623164729-3a18f1e5dceb
github.com/pingcap/tidb v1.1.0-beta.0.20200630082100-328b6d0a955c
github.com/sirupsen/logrus v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pelletier/go-toml v1.3.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo=
github.com/pelletier/go-toml v1.8.0 h1:Keo9qb7iRJs2voHvunFtuuYFsbWeOBh8/P9v/kVMFtw=
github.com/pelletier/go-toml v1.8.0/go.mod h1:D6yutnOGMveHEPV7VQOuvI/gXY61bv+9bAOTRnLElKs=
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o=
github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d/go.mod h1:lXfE4PvvTW5xOjO6Mba8zDPyw8M93B6AQ7frTGnMlA8=
github.com/pingcap-incubator/tidb-dashboard v0.0.0-20200407064406-b2b8ad403d01/go.mod h1:77fCh8d3oKzC5ceOJWeZXAS/mLzVgdZ7rKniwmOyFuo=
Expand Down
13 changes: 13 additions & 0 deletions utility/withCase/withCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package withCase
import (
"encoding/json"
"github.com/iancoleman/strcase"
"github.com/pelletier/go-toml"
"gopkg.in/yaml.v3"
)

Expand Down Expand Up @@ -38,6 +39,7 @@ func (s *WithCase) UnmarshalYAML(node *yaml.Node) error {
err := node.Decode(&s.string)
return err
}

func (s *WithCase) UnmarshalJSON(b []byte) error {
var temp string
err := json.Unmarshal(b, &temp)
Expand All @@ -47,6 +49,17 @@ func (s *WithCase) UnmarshalJSON(b []byte) error {
s.string = temp
return nil
}

func (s *WithCase) MarshalJson() ([]byte, error) {
return json.Marshal(s.string)
}

// MarshalTOML marshal `WithCase` to toml format value
func (s *WithCase) MarshalTOML() ([]byte, error) {
return toml.Marshal(s.string)
}

// UnmarshalTOML unmarshal the value in toml format to `WithCase`
func (s *WithCase) UnmarshalTOML(b []byte) error {
return toml.Unmarshal(b, s.string)
}

0 comments on commit a1927a1

Please sign in to comment.