mirror of
https://github.com/spf13/viper
synced 2025-05-11 22:57:21 +00:00
change name
This commit is contained in:
parent
d41059169e
commit
5e05190e2d
1 changed files with 28 additions and 28 deletions
56
parsers.go
56
parsers.go
|
@ -32,13 +32,13 @@ func AddParser(parser Parser, names ...string) {
|
||||||
|
|
||||||
func parserInit() {
|
func parserInit() {
|
||||||
SupportedParsers = make(map[string]Parser, 32)
|
SupportedParsers = make(map[string]Parser, 32)
|
||||||
AddParser(&JsonParser{}, "json")
|
AddParser(&JSONParser{}, "json")
|
||||||
AddParser(&TomlParser{}, "toml")
|
AddParser(&TOMLParser{}, "toml")
|
||||||
AddParser(&YamlParser{}, "yaml", "yml")
|
AddParser(&YAMLParser{}, "yaml", "yml")
|
||||||
AddParser(&PropsParser{}, "properties", "props", "prop")
|
AddParser(&PROPSParser{}, "properties", "props", "prop")
|
||||||
AddParser(&HclParser{}, "hcl")
|
AddParser(&HCLParser{}, "hcl")
|
||||||
AddParser(&DotenvParser{}, "dotenv", "env")
|
AddParser(&DOTENVParser{}, "dotenv", "env")
|
||||||
AddParser(&IniParser{}, "ini")
|
AddParser(&INIParser{}, "ini")
|
||||||
}
|
}
|
||||||
|
|
||||||
func parserReset() {
|
func parserReset() {
|
||||||
|
@ -46,15 +46,15 @@ func parserReset() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// json parser
|
// json parser
|
||||||
type JsonParser struct {
|
type JSONParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *JsonParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
func (pp *JSONParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
return json.Unmarshal(buf.Bytes(), &c)
|
return json.Unmarshal(buf.Bytes(), &c)
|
||||||
}
|
}
|
||||||
func (pp *JsonParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
func (pp *JSONParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
||||||
b, err := json.MarshalIndent(c, "", " ")
|
b, err := json.MarshalIndent(c, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -64,10 +64,10 @@ func (pp *JsonParser) MarshalWriter(v *Viper, f afero.File, c map[string]interfa
|
||||||
}
|
}
|
||||||
|
|
||||||
// toml parser
|
// toml parser
|
||||||
type TomlParser struct {
|
type TOMLParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *TomlParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
func (pp *TOMLParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
tree, err := toml.LoadReader(buf)
|
tree, err := toml.LoadReader(buf)
|
||||||
|
@ -80,7 +80,7 @@ func (pp *TomlParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]inter
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (pp *TomlParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
func (pp *TOMLParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
||||||
t, err := toml.TreeFromMap(c)
|
t, err := toml.TreeFromMap(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -93,15 +93,15 @@ func (pp *TomlParser) MarshalWriter(v *Viper, f afero.File, c map[string]interfa
|
||||||
}
|
}
|
||||||
|
|
||||||
// yaml parser
|
// yaml parser
|
||||||
type YamlParser struct {
|
type YAMLParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *YamlParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
func (pp *YAMLParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
return yaml.Unmarshal(buf.Bytes(), &c)
|
return yaml.Unmarshal(buf.Bytes(), &c)
|
||||||
}
|
}
|
||||||
func (pp *YamlParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
func (pp *YAMLParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
||||||
b, err := yaml.Marshal(c)
|
b, err := yaml.Marshal(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -113,10 +113,10 @@ func (pp *YamlParser) MarshalWriter(v *Viper, f afero.File, c map[string]interfa
|
||||||
}
|
}
|
||||||
|
|
||||||
// ini parser
|
// ini parser
|
||||||
type IniParser struct {
|
type INIParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *IniParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
func (pp *INIParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
cfg := ini.Empty()
|
cfg := ini.Empty()
|
||||||
|
@ -136,7 +136,7 @@ func (pp *IniParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interf
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (pp *IniParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
func (pp *INIParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
||||||
keys := v.AllKeys()
|
keys := v.AllKeys()
|
||||||
cfg := ini.Empty()
|
cfg := ini.Empty()
|
||||||
ini.PrettyFormat = false
|
ini.PrettyFormat = false
|
||||||
|
@ -155,10 +155,10 @@ func (pp *IniParser) MarshalWriter(v *Viper, f afero.File, c map[string]interfac
|
||||||
}
|
}
|
||||||
|
|
||||||
// hcl parser
|
// hcl parser
|
||||||
type HclParser struct {
|
type HCLParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *HclParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
func (pp *HCLParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
|
|
||||||
|
@ -171,7 +171,7 @@ func (pp *HclParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interf
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (pp *HclParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
func (pp *HCLParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
||||||
|
|
||||||
b, err := json.Marshal(c)
|
b, err := json.Marshal(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -189,10 +189,10 @@ func (pp *HclParser) MarshalWriter(v *Viper, f afero.File, c map[string]interfac
|
||||||
}
|
}
|
||||||
|
|
||||||
// dot env parser
|
// dot env parser
|
||||||
type DotenvParser struct {
|
type DOTENVParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *DotenvParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
func (pp *DOTENVParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ func (pp *DotenvParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]int
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (pp *DotenvParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
func (pp *DOTENVParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
||||||
lines := []string{}
|
lines := []string{}
|
||||||
for _, key := range v.AllKeys() {
|
for _, key := range v.AllKeys() {
|
||||||
envName := strings.ToUpper(strings.Replace(key, ".", "_", -1))
|
envName := strings.ToUpper(strings.Replace(key, ".", "_", -1))
|
||||||
|
@ -221,10 +221,10 @@ func (pp *DotenvParser) MarshalWriter(v *Viper, f afero.File, c map[string]inter
|
||||||
}
|
}
|
||||||
|
|
||||||
// props parser
|
// props parser
|
||||||
type PropsParser struct {
|
type PROPSParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pp *PropsParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
func (pp *PROPSParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]interface{}) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
|
|
||||||
|
@ -244,7 +244,7 @@ func (pp *PropsParser) UnmarshalReader(v *Viper, in io.Reader, c map[string]inte
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (pp *PropsParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
func (pp *PROPSParser) MarshalWriter(v *Viper, f afero.File, c map[string]interface{}) error {
|
||||||
if v.properties == nil {
|
if v.properties == nil {
|
||||||
v.properties = properties.NewProperties()
|
v.properties = properties.NewProperties()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue