mirror of https://github.com/joho/godotenv.git
rename WriteString/ReadString to Marshal/Unmarshal
parent
5d289f4405
commit
b1bb9d9fc3
11
godotenv.go
11
godotenv.go
|
@ -122,8 +122,8 @@ func Parse(r io.Reader) (envMap map[string]string, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//ParseString reads an env file from a string, returning a map of keys and values.
|
//Unmarshal reads an env file from a string, returning a map of keys and values.
|
||||||
func ParseString(str string) (envMap map[string]string, err error) {
|
func Unmarshal(str string) (envMap map[string]string, err error) {
|
||||||
return Parse(strings.NewReader(str))
|
return Parse(strings.NewReader(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@ func Exec(filenames []string, cmd string, cmdArgs []string) error {
|
||||||
|
|
||||||
// Write serializes the given environment and writes it to a file
|
// Write serializes the given environment and writes it to a file
|
||||||
func Write(envMap map[string]string, filename string) error {
|
func Write(envMap map[string]string, filename string) error {
|
||||||
content, error := WriteString(envMap)
|
content, error := Marshal(envMap)
|
||||||
if error != nil {
|
if error != nil {
|
||||||
return error
|
return error
|
||||||
}
|
}
|
||||||
|
@ -158,10 +158,9 @@ func Write(envMap map[string]string, filename string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteString outputs the given environment as a dotenv-formatted environment file.
|
// Marshal outputs the given environment as a dotenv-formatted environment file.
|
||||||
//
|
|
||||||
// Each line is in the format: KEY="VALUE" where VALUE is backslash-escaped.
|
// Each line is in the format: KEY="VALUE" where VALUE is backslash-escaped.
|
||||||
func WriteString(envMap map[string]string) (string, error) {
|
func Marshal(envMap map[string]string) (string, error) {
|
||||||
lines := make([]string, 0, len(envMap))
|
lines := make([]string, 0, len(envMap))
|
||||||
for k, v := range envMap {
|
for k, v := range envMap {
|
||||||
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v)))
|
lines = append(lines, fmt.Sprintf(`%s="%s"`, k, doubleQuoteEscape(v)))
|
||||||
|
|
|
@ -331,8 +331,8 @@ func TestErrorParsing(t *testing.T) {
|
||||||
|
|
||||||
func TestWrite(t *testing.T) {
|
func TestWrite(t *testing.T) {
|
||||||
writeAndCompare := func(env string, expected string) {
|
writeAndCompare := func(env string, expected string) {
|
||||||
envMap, _ := ParseString(env)
|
envMap, _ := Unmarshal(env)
|
||||||
actual, _ := WriteString(envMap)
|
actual, _ := Marshal(envMap)
|
||||||
if expected != actual {
|
if expected != actual {
|
||||||
t.Errorf("Expected '%v' (%v) to write as '%v', got '%v' instead.", env, envMap, expected, actual)
|
t.Errorf("Expected '%v' (%v) to write as '%v', got '%v' instead.", env, envMap, expected, actual)
|
||||||
}
|
}
|
||||||
|
@ -358,11 +358,11 @@ func TestRoundtrip(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
rep, err := WriteString(env)
|
rep, err := Marshal(env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
roundtripped, err := ParseString(rep)
|
roundtripped, err := Unmarshal(rep)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue