Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Oct 2, 2022
1 parent 06dd25a commit cb93876
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
34 changes: 32 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,38 @@ func (c *Config) Set(name string, value interface{}) *Config {
}

func (c *Config) Clone() *Config {
r := *c
return &r
elements := make([]*Element, len(c.Elements))
for index, elem := range c.Elements {
elements[index] = elem.Clone()
}
languages := make([]*Language, len(c.Languages))
for index, value := range c.Languages {
languages[index] = value.Clone()
}
r := &Config{
ID: c.ID,
Theme: c.Theme,
Template: c.Template,
Method: c.Method,
Action: c.Action,
Attributes: make([][]string, len(c.Attributes)),
WithButtons: c.WithButtons,
Buttons: make([]string, len(c.Buttons)),
BtnsTemplate: c.BtnsTemplate,
Elements: elements,
Languages: languages,
Data: map[string]interface{}{},
}
copy(r.Buttons, c.Buttons)
for k, v := range c.Data {
r.Data[k] = v
}
for k, v := range c.Attributes {
cv := make([]string, len(v))
copy(cv, v)
r.Attributes[k] = cv
}
return r
}

func (c *Config) HasName(name string) bool {
Expand Down
41 changes: 39 additions & 2 deletions config/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,45 @@ type Element struct {
}

func (e *Element) Clone() *Element {
r := *e
return &r
elements := make([]*Element, len(e.Elements))
languages := make([]*Language, len(e.Languages))
choices := make([]*Choice, len(e.Choices))
for index, elem := range e.Elements {
elements[index] = elem.Clone()
}
for index, value := range e.Languages {
languages[index] = value.Clone()
}
for index, value := range e.Choices {
choices[index] = value.Clone()
}
r := &Element{
ID: e.ID,
Type: e.Type,
Name: e.Name,
Label: e.Label,
LabelCols: e.LabelCols,
FieldCols: e.FieldCols,
Value: e.Value,
HelpText: e.HelpText,
Template: e.Template,
Valid: e.Valid,
Attributes: make([][]string, len(e.Attributes)),
Choices: choices,
Elements: elements,
Format: e.Format,
Languages: languages,
Data: map[string]interface{}{},
}
for k, v := range e.Data {
r.Data[k] = v
}
for k, v := range e.Attributes {
cv := make([]string, len(v))
copy(cv, v)
r.Attributes[k] = cv
}
return r
}

func (e *Element) HasAttr(attrs ...string) bool {
Expand Down
10 changes: 10 additions & 0 deletions config/element_choice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@ type Choice struct {
Option []string `json:"option"` //["value","text"]
Checked bool `json:"checked"`
}

func (c *Choice) Clone() *Choice {
r := &Choice{
Group: c.Group,
Option: make([]string, len(c.Option)),
Checked: c.Checked,
}
copy(r.Option, r.Option)
return r
}
1 change: 1 addition & 0 deletions forms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ func TestParseConfig(t *testing.T) {
f.Theme = common.BOOTSTRAP
f.Init(&cfg)
f.ParseFromConfig(true)
com.Dump(cfg.Clone())
}

0 comments on commit cb93876

Please sign in to comment.