/
dotenv.go
51 lines (41 loc) · 1.07 KB
/
dotenv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Package dotenv implements a koanf.Parser that parses DOTENV bytes as conf maps.
package dotenv
import (
"fmt"
"github.com/joho/godotenv"
)
// DotEnv implements a DotEnv parser.
type DotEnv struct{}
// Parser returns a DOTENV Parser.
func Parser() *DotEnv {
return &DotEnv{}
}
// Unmarshal parses the given DOTENV bytes.
func (p *DotEnv) Unmarshal(b []byte) (map[string]interface{}, error) {
// Unmarshal DOTENV from []byte
r, err := godotenv.Unmarshal(string(b))
if err != nil {
return nil, err
}
// Convert a map[string]string to a map[string]interface{}
mp := make(map[string]interface{})
for k, v := range r {
mp[k] = v
}
return mp, nil
}
// Marshal marshals the given config map to DOTENV bytes.
func (p *DotEnv) Marshal(o map[string]interface{}) ([]byte, error) {
// Convert a map[string]interface{} to a map[string]string
mp := make(map[string]string)
for k, v := range o {
mp[k] = fmt.Sprint(v)
}
// Unmarshal to string
out, err := godotenv.Marshal(mp)
if err != nil {
return nil, err
}
// Convert to []byte and return
return []byte(out), nil
}