Skip to content

Commit

Permalink
fix: default val override. (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
Koooooo-7 committed Apr 1, 2023
1 parent 2e04a44 commit edde61d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions charts/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,17 @@ func (bc *BaseConfiguration) GetAssets() opts.Assets {
return bc.Assets
}

// FillDefaultValues fill default values for chart options.
func (bc *BaseConfiguration) FillDefaultValues() {
opts.SetDefaultValue(bc)
}

func (bc *BaseConfiguration) initBaseConfiguration() {
bc.initSeriesColors()
bc.InitAssets()
bc.initXYAxis()
bc.Initialization.Validate()
bc.FillDefaultValues()
}

func (bc *BaseConfiguration) initSeriesColors() {
Expand Down
1 change: 1 addition & 0 deletions components/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
type Charter interface {
Type() string
GetAssets() opts.Assets
FillDefaultValues()
Validate()
}

Expand Down
23 changes: 16 additions & 7 deletions opts/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,30 @@ type Initialization struct {

// Validate validates the initialization configurations.
func (opt *Initialization) Validate() {
setDefaultValue(opt)
SetDefaultValue(opt)
if opt.ChartID == "" {
opt.ChartID = generateUniqueID()
}
}

// set default values for the struct field.
// origin from: https://github.com/mcuadros/go-defaults
func setDefaultValue(ptr interface{}) {
// inspired from: https://github.com/mcuadros/go-defaults
func SetDefaultValue(ptr interface{}) {
elem := reflect.ValueOf(ptr).Elem()
t := elem.Type()
walkField(elem)
}

func walkField(val reflect.Value) {
t := val.Type()

for i := 0; i < t.NumField(); i++ {
// handle `default` tag only
f := val.Field(i)
if f.Kind() == reflect.Struct {
walkField(f)
}

if defaultVal := t.Field(i).Tag.Get("default"); defaultVal != "" {
setField(elem.Field(i), defaultVal)
setField(val.Field(i), defaultVal)
}
}
}
Expand Down Expand Up @@ -156,7 +164,8 @@ type Title struct {
// https://echarts.apache.org/en/option.html#legend
type Legend struct {
// Whether to show the Legend, default true.
Show bool `json:"show"`
// Once you set other options, need to manually set it to true
Show bool `json:"show" default:"true"`

// Type of legend. Optional values:
// "plain": Simple legend. (default)
Expand Down

0 comments on commit edde61d

Please sign in to comment.