-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
defaults from struct tags, basic http fixes
- Loading branch information
Showing
11 changed files
with
181 additions
and
155 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
|
||
service: | ||
listen: | ||
- mode: 'transparent' | ||
port: 4128 | ||
|
||
timeout: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cnf | ||
|
||
import ( | ||
"github.com/creasty/defaults" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// allow single string to be supplied | ||
type YamlStringArray []string | ||
|
||
func (a *YamlStringArray) UnmarshalYAML(value *yaml.Node) error { | ||
var multi []string | ||
err := value.Decode(&multi) | ||
if err != nil { | ||
var single string | ||
err := value.Decode(&single) | ||
if err != nil { | ||
return err | ||
} | ||
*a = []string{single} | ||
} else { | ||
*a = multi | ||
} | ||
return nil | ||
} | ||
|
||
// apply defaults from tags on unmarshal | ||
func (s *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
defaults.Set(s) | ||
|
||
type plain Config | ||
if err := unmarshal((*plain)(s)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *ServiceListener) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
defaults.Set(s) | ||
|
||
type plain ServiceListener | ||
if err := unmarshal((*plain)(s)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *ServiceTimeout) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
defaults.Set(s) | ||
|
||
type plain ServiceTimeout | ||
if err := unmarshal((*plain)(s)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *ServiceOutput) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
defaults.Set(s) | ||
|
||
type plain ServiceOutput | ||
if err := unmarshal((*plain)(s)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *ServiceMetrics) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
defaults.Set(s) | ||
|
||
type plain ServiceMetrics | ||
if err := unmarshal((*plain)(s)); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package cnf | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/creasty/defaults" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestConfigDefaults(t *testing.T) { | ||
cnfListen1 := ServiceListener{} | ||
if err := defaults.Set(&cnfListen1); err != nil { | ||
t.Errorf("Unmarshal defaults - unable to set defaults #1! (%v)", err) | ||
} | ||
|
||
if len(cnfListen1.IP4) != 1 || cnfListen1.IP4[0] != "127.0.0.1" { | ||
t.Errorf("Unmarshal defaults-service #1 (%+v)", cnfListen1) | ||
} | ||
if len(cnfListen1.IP6) != 1 || cnfListen1.IP6[0] != "::1" { | ||
t.Errorf("Unmarshal defaults-service #2 (%+v)", cnfListen1) | ||
} | ||
if !cnfListen1.Tcp { | ||
t.Errorf("Unmarshal defaults-service #3 (%+v)", cnfListen1) | ||
} | ||
|
||
// test with acutal config file | ||
_, pathToTest, _, _ := runtime.Caller(0) | ||
pathToTestConfig := filepath.Dir(pathToTest) + "/testdata/unmarshal_test_defaults.yml" | ||
|
||
cnf2 := Config{} | ||
configRaw, err := os.ReadFile(pathToTestConfig) | ||
if err != nil { | ||
t.Errorf("Unmarshal defaults - unable to load test-config! (%v)", err) | ||
} | ||
err = yaml.Unmarshal(configRaw, &cnf2) | ||
if err != nil { | ||
t.Errorf("Unmarshal defaults - unable to unmarshal test-config! (%v)", err) | ||
} | ||
|
||
if len(cnf2.Service.Listen[0].IP4) != 1 || cnf2.Service.Listen[0].IP4[0] != "127.0.0.1" { | ||
t.Errorf("Unmarshal defaults-file #1 (%+v)", cnf2.Service.Listen[0]) | ||
} | ||
if len(cnf2.Service.Listen[0].IP6) != 1 || cnf2.Service.Listen[0].IP6[0] != "::1" { | ||
t.Errorf("Unmarshal defaults-file #2 (%+v)", cnf2.Service.Listen[0]) | ||
} | ||
if !cnf2.Service.Listen[0].Tcp { | ||
t.Errorf("Unmarshal defaults-file #3 (%+v)", cnf2.Service.Listen[0]) | ||
} | ||
if cnf2.Service.Timeout.Connect != 2000 || | ||
cnf2.Service.Timeout.Process != 1000 || | ||
cnf2.Service.Timeout.Idle != 30000 { | ||
t.Errorf("Unmarshal defaults-file #4 (%+v)", cnf2.Service.Timeout) | ||
} | ||
if cnf2.Service.Output.Retries != 1 || cnf2.Service.Output.FwMark != 0 { | ||
t.Errorf("Unmarshal defaults-file #5 (%+v)", cnf2.Service.Output) | ||
} | ||
if cnf2.Service.Metrics.Port != 9512 { | ||
t.Errorf("Unmarshal defaults-file #6 (%+v)", cnf2.Service.Metrics) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.