config.coalesceUint16   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 2
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
package config
2
3
import "github.com/kelseyhightower/envconfig"
4
5
type environmentConfiguration struct {
6
	SpecificationURL *string `split_words:"true"`
7
8
	CORSEnabled     *bool `split_words:"true"`
9
	Port            *uint16
10
	ResponseTimeout *float64 `split_words:"true"`
11
12
	Debug     *bool
13
	LogFormat *string `split_words:"true"`
14
	LogLevel  *string `split_words:"true"`
15
16
	DefaultMinFloat *float64 `split_words:"true"`
17
	DefaultMaxFloat *float64 `split_words:"true"`
18
	DefaultMinInt   *int64   `split_words:"true"`
19
	DefaultMaxInt   *int64   `split_words:"true"`
20
	NullProbability *float64 `split_words:"true"`
21
	SuppressErrors  *bool    `split_words:"true"`
22
	UseExamples     *string  `split_words:"true"`
23
}
24
25
func updateConfigFromEnvironment(fileConfig *fileConfiguration) {
26 1
	var envConfig environmentConfiguration
27 1
	envconfig.MustProcess("OPENAPI_MOCK", &envConfig)
28
29 1
	if envConfig.SpecificationURL != nil {
30 1
		fileConfig.OpenAPI.SpecificationURL = *envConfig.SpecificationURL
31 1
		fileConfig.OpenAPI.urlFromEnv = true
32
	}
33
34 1
	fileConfig.HTTP.CORSEnabled = coalesceBool(fileConfig.HTTP.CORSEnabled, envConfig.CORSEnabled)
35 1
	fileConfig.HTTP.Port = coalesceUint16(fileConfig.HTTP.Port, envConfig.Port)
36 1
	fileConfig.HTTP.ResponseTimeout = *coalesceFloat(&fileConfig.HTTP.ResponseTimeout, envConfig.ResponseTimeout)
37
38 1
	fileConfig.Application.Debug = coalesceBool(fileConfig.Application.Debug, envConfig.Debug)
39 1
	fileConfig.Application.LogFormat = coalesceString(fileConfig.Application.LogFormat, envConfig.LogFormat)
40 1
	fileConfig.Application.LogLevel = coalesceString(fileConfig.Application.LogLevel, envConfig.LogLevel)
41
42 1
	fileConfig.Generation.DefaultMinFloat = coalesceFloat(fileConfig.Generation.DefaultMinFloat, envConfig.DefaultMinFloat)
43 1
	fileConfig.Generation.DefaultMaxFloat = coalesceFloat(fileConfig.Generation.DefaultMaxFloat, envConfig.DefaultMaxFloat)
44 1
	fileConfig.Generation.DefaultMinInt = coalesceInt64(fileConfig.Generation.DefaultMinInt, envConfig.DefaultMinInt)
45 1
	fileConfig.Generation.DefaultMaxInt = coalesceInt64(fileConfig.Generation.DefaultMaxInt, envConfig.DefaultMaxInt)
46 1
	fileConfig.Generation.NullProbability = coalesceFloat(fileConfig.Generation.NullProbability, envConfig.NullProbability)
47 1
	fileConfig.Generation.SuppressErrors = coalesceBool(fileConfig.Generation.SuppressErrors, envConfig.SuppressErrors)
48 1
	fileConfig.Generation.UseExamples = coalesceString(fileConfig.Generation.UseExamples, envConfig.UseExamples)
49
}
50
51
func coalesceString(v1 string, v2 *string) string {
52 1
	if v2 != nil {
53 1
		return *v2
54
	}
55
56 1
	return v1
57
}
58
59
func coalesceBool(v1 bool, v2 *bool) bool {
60 1
	if v2 != nil {
61 1
		return *v2
62
	}
63
64 1
	return v1
65
}
66
67
func coalesceUint16(v1 *uint16, v2 *uint16) *uint16 {
68 1
	if v2 != nil {
69 1
		return v2
70
	}
71
72 1
	return v1
73
}
74
75
func coalesceInt64(v1 *int64, v2 *int64) *int64 {
76 1
	if v2 != nil {
77 1
		return v2
78
	}
79
80 1
	return v1
81
}
82
83
func coalesceFloat(v1 *float64, v2 *float64) *float64 {
84 1
	if v2 != nil {
85 1
		return v2
86
	}
87
88 1
	return v1
89
}
90