mirror of
https://github.com/spf13/viper
synced 2025-04-28 16:27:17 +00:00
Merge b3171c42b6
into 1508a7ba44
This commit is contained in:
commit
b5300f0397
1 changed files with 48 additions and 0 deletions
48
viper.go
48
viper.go
|
@ -1625,6 +1625,11 @@ func (v *Viper) SafeWriteConfigAs(filename string) error {
|
||||||
return v.writeConfig(filename, false)
|
return v.writeConfig(filename, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalToString writes current configuration to a string
|
||||||
|
func MarshalToString(configTypeOps ...string) (string, error) {
|
||||||
|
return v.MarshalToString(configTypeOps...)
|
||||||
|
}
|
||||||
|
|
||||||
func (v *Viper) writeConfig(filename string, force bool) error {
|
func (v *Viper) writeConfig(filename string, force bool) error {
|
||||||
v.logger.Info("attempting to write configuration to file")
|
v.logger.Info("attempting to write configuration to file")
|
||||||
|
|
||||||
|
@ -1663,6 +1668,30 @@ func (v *Viper) writeConfig(filename string, force bool) error {
|
||||||
return f.Sync()
|
return f.Sync()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *Viper) MarshalToString(configTypeOps ...string) (string, error) {
|
||||||
|
v.logger.Info("MarshalToString: attempting to marshal configuration to string")
|
||||||
|
|
||||||
|
//default by origin configType
|
||||||
|
var configType = v.configType
|
||||||
|
//set from param if configTypeOps len > 0
|
||||||
|
if len(configTypeOps) > 0 {
|
||||||
|
configType = configTypeOps[0]
|
||||||
|
}
|
||||||
|
if configType == "" {
|
||||||
|
return "", fmt.Errorf("MarshalToString: config type could not be determined")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !stringInSlice(configType, SupportedExts) {
|
||||||
|
return "", UnsupportedConfigError(configType)
|
||||||
|
}
|
||||||
|
|
||||||
|
str, err := v.marshalToString(configType)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return str, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error {
|
func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error {
|
||||||
format := strings.ToLower(v.getConfigType())
|
format := strings.ToLower(v.getConfigType())
|
||||||
if format == "" {
|
if format == "" {
|
||||||
|
@ -1715,6 +1744,25 @@ func (v *Viper) marshalWriter(w io.Writer, configType string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Marshal a map into string.
|
||||||
|
func (v *Viper) marshalToString(configType string) (string, error) {
|
||||||
|
c := v.AllSettings()
|
||||||
|
switch configType {
|
||||||
|
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "prop", "props", "properties", "dotenv", "env":
|
||||||
|
encoder, err := v.encoderRegistry.Encoder(configType)
|
||||||
|
if err != nil {
|
||||||
|
return "", ConfigMarshalError{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := encoder.Encode(c)
|
||||||
|
if err != nil {
|
||||||
|
return "", ConfigMarshalError{err}
|
||||||
|
}
|
||||||
|
return string(b), nil
|
||||||
|
}
|
||||||
|
return "", ConfigMarshalError{fmt.Errorf("config type could not be determined, type:%s", configType)}
|
||||||
|
}
|
||||||
|
|
||||||
func keyExists(k string, m map[string]any) string {
|
func keyExists(k string, m map[string]any) string {
|
||||||
lk := strings.ToLower(k)
|
lk := strings.ToLower(k)
|
||||||
for mk := range m {
|
for mk := range m {
|
||||||
|
|
Loading…
Add table
Reference in a new issue