| 1 |
// Package dotenv implements a koanf.Parser that parses DOTENV bytes as conf maps. |
| 2 |
package dotenv |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"github.com/joho/godotenv" |
| 9 |
"github.com/knadh/koanf/maps" |
| 10 |
) |
| 11 |
|
| 12 |
// DotEnv implements a DOTENV parser. |
| 13 |
type DotEnv struct { |
| 14 |
delim string |
| 15 |
prefix string |
| 16 |
|
| 17 |
cb func(key string, value string) (string, any) |
| 18 |
reverseCB map[string]string |
| 19 |
} |
| 20 |
|
| 21 |
// Parser returns a DOTENV Parser. |
| 22 |
func Parser() *DotEnv { |
| 23 |
return &DotEnv{} |
| 24 |
} |
| 25 |
|
| 26 |
// ParserEnv allows to make the DOTENV Parser behave like the env.Provider. |
| 27 |
func ParserEnv(prefix, delim string, cb func(s string) string) *DotEnv { |
| 28 |
res := &DotEnv{ |
| 29 |
delim: delim, |
| 30 |
prefix: prefix, |
| 31 |
reverseCB: make(map[string]string), |
| 32 |
} |
| 33 |
|
| 34 |
if cb != nil { |
| 35 |
res.cb = func(key string, value string) (string, interface{}) { |
| 36 |
return cb(key), value |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return res |
| 41 |
} |
| 42 |
|
| 43 |
// ParserEnvWithValue allows to make the DOTENV Parser behave like the env.ProviderWithValue. |
| 44 |
func ParserEnvWithValue(prefix, delim string, cb func(key string, value string) (string, any)) *DotEnv { |
| 45 |
return &DotEnv{ |
| 46 |
delim: delim, |
| 47 |
prefix: prefix, |
| 48 |
cb: cb, |
| 49 |
reverseCB: make(map[string]string), |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Unmarshal parses the given DOTENV bytes. |
| 54 |
func (p *DotEnv) Unmarshal(b []byte) (map[string]any, error) { |
| 55 |
// Unmarshal DOTENV from []byte |
| 56 |
r, err := godotenv.Unmarshal(string(b)) |
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
|
| 61 |
// Convert a map[string]string to a map[string]any |
| 62 |
mp := make(map[string]any) |
| 63 |
for sourceKey, v := range r { |
| 64 |
if !strings.HasPrefix(sourceKey, p.prefix) { |
| 65 |
continue |
| 66 |
} |
| 67 |
|
| 68 |
if p.cb != nil { |
| 69 |
targetKey, value := p.cb(sourceKey, v) |
| 70 |
p.reverseCB[targetKey] = sourceKey |
| 71 |
mp[targetKey] = value |
| 72 |
} else { |
| 73 |
mp[sourceKey] = v |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
if p.delim != "" { |
| 79 |
mp = maps.Unflatten(mp, p.delim) |
| 80 |
} |
| 81 |
return mp, nil |
| 82 |
} |
| 83 |
|
| 84 |
// Marshal marshals the given config map to DOTENV bytes. |
| 85 |
func (p *DotEnv) Marshal(o map[string]any) ([]byte, error) { |
| 86 |
if p.delim != "" { |
| 87 |
o, _ = maps.Flatten(o, nil, p.delim) |
| 88 |
} |
| 89 |
|
| 90 |
// Convert a map[string]any to a map[string]string |
| 91 |
mp := make(map[string]string) |
| 92 |
for targetKey, v := range o { |
| 93 |
if sourceKey, found := p.reverseCB[targetKey]; found { |
| 94 |
targetKey = sourceKey |
| 95 |
} |
| 96 |
|
| 97 |
mp[targetKey] = fmt.Sprint(v) |
| 98 |
} |
| 99 |
|
| 100 |
// Unmarshal to string |
| 101 |
out, err := godotenv.Marshal(mp) |
| 102 |
if err != nil { |
| 103 |
return nil, err |
| 104 |
} |
| 105 |
|
| 106 |
// Convert to []byte and return |
| 107 |
return []byte(out), nil |
| 108 |
} |