Merge merges the given section to current section. Settings from source section overwites the values in the current section
(source *Section)
| 317 | // Merge merges the given section to current section. Settings from source |
| 318 | // section overwites the values in the current section |
| 319 | func (section *Section) Merge(source *Section) error { |
| 320 | for _, key := range source.Keys() { |
| 321 | sourceValue, _ := source.Get(key) |
| 322 | targetValue, err := section.Get(key) |
| 323 | |
| 324 | // not found, so add it |
| 325 | if err != nil { |
| 326 | section.Set(key, sourceValue) |
| 327 | continue |
| 328 | } |
| 329 | |
| 330 | // found existing one and it's type SECTION, merge it |
| 331 | if targetValue.GetType() == SECTION { |
| 332 | // Source value have to be SECTION type here |
| 333 | if sourceValue.GetType() != SECTION { |
| 334 | return fmt.Errorf("source (%v) and target (%v) type doesn't match: %v", |
| 335 | sourceValue.GetType(), |
| 336 | targetValue.GetType(), |
| 337 | key) |
| 338 | } |
| 339 | |
| 340 | if err = targetValue.(*Section).Merge(sourceValue.(*Section)); err != nil { |
| 341 | return err |
| 342 | } |
| 343 | |
| 344 | continue |
| 345 | } |
| 346 | |
| 347 | // found existing one, update it |
| 348 | if err = targetValue.UpdateValue(sourceValue.GetValue()); err != nil { |
| 349 | return fmt.Errorf("%v: %v", err, key) |
| 350 | } |
| 351 | } |
| 352 | return nil |
| 353 | } |
| 354 | |
| 355 | // ToJSON will convert this Section and all it's underlying values and Sections |
| 356 | // into JSON as a []byte |