Skip to content

Commit

Permalink
Add internal/config/config test (#1039)
Browse files Browse the repository at this point in the history
* fix: add test

Signed-off-by: hlts2 <[email protected]>

* feat: add test case for other function

Signed-off-by: hlts2 <[email protected]>

* feat: add missing test case

Signed-off-by: hlts2 <[email protected]>

* fix: add test case name

Signed-off-by: hlts2 <[email protected]>

* feat: add implementation comment

Signed-off-by: hlts2 <[email protected]>

* Apply suggestions from code review

* fix: error handling for generating test data

Signed-off-by: hlts2 <[email protected]>

* fix: apply suggestion

Signed-off-by: hlts2 <[email protected]>

* fix: apply suggestion

Signed-off-by: hlts2 <[email protected]>

* fix: add test case for read config function

Signed-off-by: hlts2 <[email protected]>

* fix: dog warning

Signed-off-by: hlts2 <[email protected]>

* feat: add test for util

Signed-off-by: hlts2 <[email protected]>

* Revert "feat: add test for util"

This reverts commit 16e2546.
  • Loading branch information
hlts2 authored Mar 15, 2021
1 parent 30f3fe5 commit 0beb8ae
Show file tree
Hide file tree
Showing 2 changed files with 1,421 additions and 223 deletions.
8 changes: 7 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
envSymbol = "_"
)

// Bind binds the actual data from the receiver field.
func (c *GlobalConfig) Bind() *GlobalConfig {
c.Version = GetActualValue(c.Version)
c.TZ = GetActualValue(c.TZ)
Expand All @@ -58,6 +59,7 @@ func (c *GlobalConfig) Bind() *GlobalConfig {
return c
}

// UnmarshalJSON parses the JSON-encoded data and stores the result in the field of receiver.
func (c *GlobalConfig) UnmarshalJSON(data []byte) (err error) {
ic := new(struct {
Ver string `json:"version"`
Expand All @@ -74,7 +76,7 @@ func (c *GlobalConfig) UnmarshalJSON(data []byte) (err error) {
return nil
}

// New returns config struct or error when decode the configuration file to actually *Config struct.
// Read returns config struct or error when decoding the configuration file to actually *Config struct.
func Read(path string, cfg interface{}) (err error) {
f, err := os.OpenFile(path, os.O_RDONLY, 0o600)
if err != nil {
Expand Down Expand Up @@ -117,6 +119,9 @@ func GetActualValue(val string) (res string) {
return
}

// GetActualValues returns the environment variable values if the vals has string slice that has prefix and suffix "_",
// if actual value start with file://{path} the return value will read from file
// otherwise the val will directly return.
func GetActualValues(vals []string) []string {
for i, val := range vals {
vals[i] = GetActualValue(val)
Expand All @@ -129,6 +134,7 @@ func checkPrefixAndSuffix(str, pref, suf string) bool {
return strings.HasPrefix(str, pref) && strings.HasSuffix(str, suf)
}

// ToRawYaml writes the YAML encoding of v to the stream and returns the string written to stream.
func ToRawYaml(data interface{}) string {
buf := bytes.NewBuffer(nil)
err := yaml.NewEncoder(buf).Encode(data)
Expand Down
Loading

0 comments on commit 0beb8ae

Please sign in to comment.