Config to string, JSON and YAML methods added and updated print methods to use new methods

This commit is contained in:
bwilliams committed 2026-09-29 11:55:11 +01:00
1 parent e715024cbd
commit a0fad30e99
3 files changed
+32 -33

No files matched your search

+27
View File
@@ -17,12 +17,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package config
import (
"encoding/json"
"errors"
"fmt"
"maps"
"reflect"
"strings"
"github.com/spf13/viper"
"go.yaml.in/yaml/v3"
)
const EnvPrefix string = "JOBSEARCH"
@@ -112,3 +115,27 @@ func LoadToViper(conf JobSearchConfig) {
viper.SetDefault(key, flatMap[key])
}
}
// Converts configuration to a string in flat dotted format
func (conf JobSearchConfig) String() string {
var sb strings.Builder
for k, v := range conf.Flatten() {
typ, _ := conf.TypeOf(k)
sb.WriteString(fmt.Sprint(k, " = ", v, " <", typ, ">"))
}
return sb.String()
}
// Converts configuration to JSON
func (conf JobSearchConfig) JSON() string {
confMap := conf.Map()
jbytes, _ := json.MarshalIndent(confMap, "", " ")
return string(jbytes)
}
// Converts configuration to YAML
func (conf JobSearchConfig) YAML() string {
confMap := conf.Map()
ybytes, _ := yaml.Marshal(confMap)
return string(ybytes)
}