Skip to content

Commit 0d0f00f

Browse files
committed
Clean code
Signed-off-by: Mathieu Champlon <[email protected]>
1 parent 84e269e commit 0d0f00f

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

loader/loader.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
145145
op(opts)
146146
}
147147

148-
configs := []*types.Config{}
148+
var configs []*types.Config
149149
for i, file := range configDetails.ConfigFiles {
150150
configDict := file.Config
151151
if configDict == nil {
@@ -222,14 +222,14 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
222222
}
223223

224224
func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) {
225-
yaml, err := ParseYAML(b)
225+
yml, err := ParseYAML(b)
226226
if err != nil {
227227
return nil, err
228228
}
229229
if !opts.SkipInterpolation {
230-
return interp.Interpolate(yaml, *opts.Interpolate)
230+
return interp.Interpolate(yml, *opts.Interpolate)
231231
}
232-
return yaml, err
232+
return yml, err
233233
}
234234

235235
func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} {
@@ -372,7 +372,7 @@ func createTransformHook(additionalTransformers ...Transformer) mapstructure.Dec
372372
}
373373
}
374374

375-
// keys needs to be converted to strings for jsonschema
375+
// keys need to be converted to strings for jsonschema
376376
func convertToStringKeysRecursive(value interface{}, keyPrefix string) (interface{}, error) {
377377
if mapping, ok := value.(map[interface{}]interface{}); ok {
378378
dict := make(map[string]interface{})
@@ -396,7 +396,7 @@ func convertToStringKeysRecursive(value interface{}, keyPrefix string) (interfac
396396
return dict, nil
397397
}
398398
if list, ok := value.([]interface{}); ok {
399-
convertedList := []interface{}{}
399+
var convertedList []interface{}
400400
for index, entry := range list {
401401
newKeyPrefix := fmt.Sprintf("%s[%d]", keyPrefix, index)
402402
convertedEntry, err := convertToStringKeysRecursive(entry, newKeyPrefix)
@@ -532,7 +532,7 @@ func LoadService(name string, serviceDict map[string]interface{}, workingDir str
532532
}
533533

534534
for i, volume := range serviceConfig.Volumes {
535-
if volume.Type != "bind" {
535+
if volume.Type != types.VolumeTypeBind {
536536
continue
537537
}
538538

@@ -552,8 +552,8 @@ func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, l
552552
environment := types.MappingWithEquals{}
553553

554554
if len(serviceConfig.EnvFile) > 0 {
555-
for _, file := range serviceConfig.EnvFile {
556-
filePath := absPath(workingDir, file)
555+
for _, envFile := range serviceConfig.EnvFile {
556+
filePath := absPath(workingDir, envFile)
557557
file, err := os.Open(filePath)
558558
if err != nil {
559559
return err
@@ -797,7 +797,7 @@ var transformServicePort TransformerFunc = func(data interface{}) (interface{},
797797
// We process the list instead of individual items here.
798798
// The reason is that one entry might be mapped to multiple ServicePortConfig.
799799
// Therefore we take an input of a list and return an output of a list.
800-
ports := []interface{}{}
800+
var ports []interface{}
801801
for _, entry := range entries {
802802
switch value := entry.(type) {
803803
case int:
@@ -971,7 +971,7 @@ func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool
971971
switch value := mappingOrList.(type) {
972972
case map[string]interface{}:
973973
return toMapStringString(value, allowNil)
974-
case ([]interface{}):
974+
case []interface{}:
975975
result := make(map[string]interface{})
976976
for _, value := range value {
977977
parts := strings.SplitN(value.(string), sep, 2)
@@ -1054,7 +1054,7 @@ func toString(value interface{}, allowNil bool) interface{} {
10541054
}
10551055

10561056
func toStringList(value map[string]interface{}, separator string, allowNil bool) []string {
1057-
output := []string{}
1057+
var output []string
10581058
for key, value := range value {
10591059
if value == nil && !allowNil {
10601060
continue

loader/merge.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func toServiceVolumeConfigsMap(s interface{}) (map[interface{}]interface{}, erro
184184
}
185185

186186
func toServiceSecretConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
187-
s := []types.ServiceSecretConfig{}
187+
var s []types.ServiceSecretConfig
188188
for _, v := range m {
189189
s = append(s, v.(types.ServiceSecretConfig))
190190
}
@@ -194,7 +194,7 @@ func toServiceSecretConfigsSlice(dst reflect.Value, m map[interface{}]interface{
194194
}
195195

196196
func toSServiceConfigObjConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
197-
s := []types.ServiceConfigObjConfig{}
197+
var s []types.ServiceConfigObjConfig
198198
for _, v := range m {
199199
s = append(s, v.(types.ServiceConfigObjConfig))
200200
}
@@ -204,7 +204,7 @@ func toSServiceConfigObjConfigsSlice(dst reflect.Value, m map[interface{}]interf
204204
}
205205

206206
func toServicePortConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
207-
s := []types.ServicePortConfig{}
207+
var s []types.ServicePortConfig
208208
for _, v := range m {
209209
s = append(s, v.(types.ServicePortConfig))
210210
}
@@ -225,7 +225,7 @@ func toServicePortConfigsSlice(dst reflect.Value, m map[interface{}]interface{})
225225
}
226226

227227
func toServiceVolumeConfigsSlice(dst reflect.Value, m map[interface{}]interface{}) error {
228-
s := []types.ServiceVolumeConfig{}
228+
var s []types.ServiceVolumeConfig
229229
for _, v := range m {
230230
s = append(s, v.(types.ServiceVolumeConfig))
231231
}
@@ -234,7 +234,7 @@ func toServiceVolumeConfigsSlice(dst reflect.Value, m map[interface{}]interface{
234234
return nil
235235
}
236236

237-
type tomapFn func(s interface{}) (map[interface{}]interface{}, error)
237+
type toMapFn func(s interface{}) (map[interface{}]interface{}, error)
238238
type writeValueFromMapFn func(reflect.Value, map[interface{}]interface{}) error
239239

240240
func safelyMerge(mergeFn func(dst, src reflect.Value) error) func(dst, src reflect.Value) error {
@@ -250,13 +250,13 @@ func safelyMerge(mergeFn func(dst, src reflect.Value) error) func(dst, src refle
250250
}
251251
}
252252

253-
func mergeSlice(tomap tomapFn, writeValue writeValueFromMapFn) func(dst, src reflect.Value) error {
253+
func mergeSlice(toMap toMapFn, writeValue writeValueFromMapFn) func(dst, src reflect.Value) error {
254254
return func(dst, src reflect.Value) error {
255-
dstMap, err := sliceToMap(tomap, dst)
255+
dstMap, err := sliceToMap(toMap, dst)
256256
if err != nil {
257257
return err
258258
}
259-
srcMap, err := sliceToMap(tomap, src)
259+
srcMap, err := sliceToMap(toMap, src)
260260
if err != nil {
261261
return err
262262
}
@@ -267,12 +267,12 @@ func mergeSlice(tomap tomapFn, writeValue writeValueFromMapFn) func(dst, src ref
267267
}
268268
}
269269

270-
func sliceToMap(tomap tomapFn, v reflect.Value) (map[interface{}]interface{}, error) {
270+
func sliceToMap(toMap toMapFn, v reflect.Value) (map[interface{}]interface{}, error) {
271271
// check if valid
272272
if !v.IsValid() {
273273
return nil, errors.Errorf("invalid value : %+v", v)
274274
}
275-
return tomap(v.Interface())
275+
return toMap(v.Interface())
276276
}
277277

278278
func mergeLoggingConfig(dst, src reflect.Value) error {

types/project.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Project struct {
4646

4747
// ServiceNames return names for all services in this Compose config
4848
func (p Project) ServiceNames() []string {
49-
names := []string{}
49+
var names []string
5050
for _, s := range p.Services {
5151
names = append(names, s.Name)
5252
}
@@ -56,7 +56,7 @@ func (p Project) ServiceNames() []string {
5656

5757
// VolumeNames return names for all volumes in this Compose config
5858
func (p Project) VolumeNames() []string {
59-
names := []string{}
59+
var names []string
6060
for k := range p.Volumes {
6161
names = append(names, k)
6262
}
@@ -66,7 +66,7 @@ func (p Project) VolumeNames() []string {
6666

6767
// NetworkNames return names for all volumes in this Compose config
6868
func (p Project) NetworkNames() []string {
69-
names := []string{}
69+
var names []string
7070
for k := range p.Networks {
7171
names = append(names, k)
7272
}
@@ -76,7 +76,7 @@ func (p Project) NetworkNames() []string {
7676

7777
// SecretNames return names for all secrets in this Compose config
7878
func (p Project) SecretNames() []string {
79-
names := []string{}
79+
var names []string
8080
for k := range p.Secrets {
8181
names = append(names, k)
8282
}
@@ -86,7 +86,7 @@ func (p Project) SecretNames() []string {
8686

8787
// ConfigNames return names for all configs in this Compose config
8888
func (p Project) ConfigNames() []string {
89-
names := []string{}
89+
var names []string
9090
for k := range p.Configs {
9191
names = append(names, k)
9292
}
@@ -179,12 +179,12 @@ func (p *Project) RelativePath(path string) string {
179179
}
180180

181181
// HasProfile return true if service has no profile declared or has at least one profile matching
182-
func (service ServiceConfig) HasProfile(profiles []string) bool {
183-
if len(service.Profiles) == 0 {
182+
func (s ServiceConfig) HasProfile(profiles []string) bool {
183+
if len(s.Profiles) == 0 {
184184
return true
185185
}
186186
for _, p := range profiles {
187-
for _, sp := range service.Profiles {
187+
for _, sp := range s.Profiles {
188188
if sp == p {
189189
return true
190190
}
@@ -327,7 +327,6 @@ func (p *Project) ResolveImages(resolver func(named reference.Named) (digest.Dig
327327
if err != nil {
328328
return err
329329
}
330-
331330
named, err = reference.WithDigest(named, digest)
332331
if err != nil {
333332
return err

types/types.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (d Duration) String() string {
3333
return time.Duration(d).String()
3434
}
3535

36-
// ConvertDurationPtr converts a typedefined Duration pointer to a time.Duration pointer with the same value.
36+
// ConvertDurationPtr converts a type defined Duration pointer to a time.Duration pointer with the same value.
3737
func ConvertDurationPtr(d *Duration) *time.Duration {
3838
if d == nil {
3939
return nil
@@ -208,7 +208,7 @@ const (
208208
PullPolicyNever = "never"
209209
//PullPolicyIfNotPresent pull missing images
210210
PullPolicyIfNotPresent = "if_not_present"
211-
//PullPolicyIfNotPresent pull missing images
211+
//PullPolicyMissing pull missing images
212212
PullPolicyMissing = "missing"
213213
//PullPolicyBuild force building images
214214
PullPolicyBuild = "build"
@@ -611,7 +611,7 @@ func ParsePortConfig(value string) ([]ServicePortConfig, error) {
611611
}
612612

613613
func convertPortToPortConfig(port nat.Port, portBindings map[nat.Port][]nat.PortBinding) ([]ServicePortConfig, error) {
614-
portConfigs := []ServicePortConfig{}
614+
var portConfigs []ServicePortConfig
615615
for _, binding := range portBindings[port] {
616616
startHostPort, endHostPort, err := nat.ParsePortRange(binding.HostPort)
617617

@@ -647,13 +647,13 @@ type ServiceVolumeConfig struct {
647647
}
648648

649649
const (
650-
// TypeBind is the type for mounting host dir
650+
// VolumeTypeBind is the type for mounting host dir
651651
VolumeTypeBind = "bind"
652-
// TypeVolume is the type for remote storage volumes
652+
// VolumeTypeVolume is the type for remote storage volumes
653653
VolumeTypeVolume = "volume"
654-
// TypeTmpfs is the type for mounting tmpfs
654+
// VolumeTypeTmpfs is the type for mounting tmpfs
655655
VolumeTypeTmpfs = "tmpfs"
656-
// TypeNamedPipe is the type for mounting Windows named pipes
656+
// VolumeTypeNamedPipe is the type for mounting Windows named pipes
657657
VolumeTypeNamedPipe = "npipe"
658658
)
659659

0 commit comments

Comments
 (0)