From 0d4b640857a8a0becef49d3b4939c730e82f61d8 Mon Sep 17 00:00:00 2001 From: Hank Shen Date: Sun, 2 Oct 2022 23:57:01 +0800 Subject: [PATCH] update --- config/config.go | 6 ++++++ config/utils.go | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/config/config.go b/config/config.go index 8a5ad9f..d1d41ff 100644 --- a/config/config.go +++ b/config/config.go @@ -138,3 +138,9 @@ func (c *Config) SetDefaultValue(fieldDefaultValue func(fieldName string) string setDefaultValue(c.Elements, c.Languages, fieldDefaultValue) } } + +func (c *Config) SetValue(fieldValue func(fieldName string) string) { + if fieldValue != nil { + setValue(c.Elements, c.Languages, fieldValue) + } +} diff --git a/config/utils.go b/config/utils.go index f18fa00..d0ca573 100644 --- a/config/utils.go +++ b/config/utils.go @@ -54,3 +54,25 @@ func setDefaultValue(elements []*Element, languages []*Language, fieldDefaultVal } } } + +func setValue(elements []*Element, languages []*Language, fieldValue func(string) string) { + for _, elem := range elements { + if elem.Type == `langset` { + setValue(elem.Elements, elem.Languages, fieldValue) + continue + } + if elem.Type == `fieldset` { + setValue(elem.Elements, languages, fieldValue) + continue + } + if len(elem.Name) > 0 { + if len(languages) == 0 { + elem.Value = fieldValue(elem.Name) + continue + } + for _, lang := range languages { + elem.Value = fieldValue(lang.Name(elem.Name)) + } + } + } +}