diff --git a/config/config.go b/config/config.go index 7b91422..8a5ad9f 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { diff --git a/config/element.go b/config/element.go index dc22e99..44a85a3 100644 --- a/config/element.go +++ b/config/element.go @@ -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 { diff --git a/config/element_choice.go b/config/element_choice.go index be98ef5..be89649 100644 --- a/config/element_choice.go +++ b/config/element_choice.go @@ -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 +} diff --git a/forms_test.go b/forms_test.go index d9dff88..4796932 100644 --- a/forms_test.go +++ b/forms_test.go @@ -85,4 +85,5 @@ func TestParseConfig(t *testing.T) { f.Theme = common.BOOTSTRAP f.Init(&cfg) f.ParseFromConfig(true) + com.Dump(cfg.Clone()) }