Passed
Push — master ( f80f95...7fc31c )
by Igor
06:13 queued 02:21
created

config.*fileConfiguration.Validate   A

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 17
nop 0
dl 0
loc 21
ccs 1
cts 1
cp 1
crap 1
rs 9.55
c 0
b 0
f 0
1
package config
2
3
import (
4
	"fmt"
5
	"io/ioutil"
6
	"strings"
7
8
	validation "github.com/go-ozzo/ozzo-validation/v4"
9
	"gopkg.in/yaml.v3"
10
)
11
12
type fileConfiguration struct {
13
	OpenAPI     openapiConfiguration     `json:"openapi" yaml:"openapi"`
14
	HTTP        httpConfiguration        `json:"http" yaml:"http"`
15
	Application applicationConfiguration `json:"application" yaml:"application"`
16
	Generation  generationConfiguration  `json:"generation" yaml:"generation"`
17
}
18
19
type openapiConfiguration struct {
20
	SpecificationURL string `json:"specification_url" yaml:"specification_url"`
21
	urlFromEnv       bool
22
}
23
24
type httpConfiguration struct {
25
	Port            *uint16 `json:"port" yaml:"port"`
26
	CORSEnabled     bool    `json:"cors_enabled" yaml:"cors_enabled"`
27
	ResponseTimeout float64 `json:"response_timeout" yaml:"response_timeout"`
28
}
29
30
type applicationConfiguration struct {
31
	Debug     bool   `json:"debug" yaml:"debug"`
32
	LogFormat string `json:"log_format" yaml:"log_format"`
33
	LogLevel  string `json:"log_level" yaml:"log_level"`
34
}
35
36
type generationConfiguration struct {
37
	DefaultMinFloat *float64 `json:"default_min_float" yaml:"default_min_float"`
38
	DefaultMaxFloat *float64 `json:"default_max_float" yaml:"default_max_float"`
39
	DefaultMinInt   *int64   `json:"default_min_int" yaml:"default_min_int"`
40
	DefaultMaxInt   *int64   `json:"default_max_int" yaml:"default_max_int"`
41
	NullProbability *float64 `json:"null_probability" yaml:"null_probability"`
42
	SuppressErrors  bool     `json:"suppress_errors" yaml:"suppress_errors"`
43
	UseExamples     string   `json:"use_examples" yaml:"use_examples"`
44
}
45
46
func loadFileConfiguration(filename string) (*fileConfiguration, error) {
47 1
	data, err := ioutil.ReadFile(filename)
48 1
	if err != nil {
49 1
		return nil, &ErrLoadFailed{Previous: err}
50
	}
51
52 1
	var fileConfig fileConfiguration
53 1
	err = yaml.Unmarshal(data, &fileConfig)
54 1
	if err != nil {
55 1
		return nil, &ErrLoadFailed{Previous: err}
56
	}
57
58 1
	return &fileConfig, nil
59
}
60
61
var logFormats = []string{"tty", "json"}
62
var logLevels = []string{"panic", "fatal", "error", "warn", "warning", "info", "debug", "trace"}
63
var useExampleOptions = []string{"no", "if_present", "exclusively"}
64
65
var invalidLogFormat = fmt.Sprintf("must be one of: %s", strings.Join(logFormats, ", "))
66
var invalidLogLevel = fmt.Sprintf("must be one of: %s", strings.Join(logLevels, ", "))
67
var invalidUseExample = fmt.Sprintf("must be one of: %s", strings.Join(useExampleOptions, ", "))
68
69
func stringsAsInterfaces(ss []string) []interface{} {
70 1
	ii := make([]interface{}, len(ss))
71 1
	for i, s := range ss {
72 1
		ii[i] = s
73
	}
74 1
	return ii
75
}
76
77
func (config *fileConfiguration) Validate() error {
78 1
	return validation.Errors{
79
		"http.port": validation.Validate(
80
			config.HTTP.Port,
81
			validation.Required.When(config.HTTP.Port != nil),
82
			validation.Min(uint16(1)),
83
			validation.Max(uint16(65535)),
84
		),
85
		"application.log_format": validation.Validate(
86
			config.Application.LogFormat,
87
			validation.In(stringsAsInterfaces(logFormats)...).Error(invalidLogFormat),
88
		),
89
		"application.log_level": validation.Validate(
90
			config.Application.LogLevel,
91
			validation.In(stringsAsInterfaces(logLevels)...).Error(invalidLogLevel),
92
		),
93
		"generation.use_examples": validation.Validate(
94
			config.Generation.UseExamples,
95
			validation.In(stringsAsInterfaces(useExampleOptions)...).Error(invalidUseExample),
96
		),
97
	}.Filter()
98
}
99