Skip to content

Commit 4fc0069

Browse files
committed
decode Healthcheck.Test using DecodeMapstructure
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 74c59b5 commit 4fc0069

File tree

3 files changed

+53
-28
lines changed

3 files changed

+53
-28
lines changed

loader/loader.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ type Transformer struct {
607607
func createTransformHook(additionalTransformers ...Transformer) mapstructure.DecodeHookFuncType {
608608
transforms := map[reflect.Type]func(interface{}) (interface{}, error){
609609
reflect.TypeOf(types.External{}): transformExternal,
610-
reflect.TypeOf(types.HealthCheckTest{}): transformHealthCheckTest,
611610
reflect.TypeOf(types.Options{}): transformMapStringString,
612611
reflect.TypeOf(types.UlimitsConfig{}): transformUlimits,
613612
reflect.TypeOf([]types.ServicePortConfig{}): transformServicePort,
@@ -1253,17 +1252,6 @@ func transformValueToMapEntry(value string, separator string, allowNil bool) (st
12531252
}
12541253
}
12551254

1256-
var transformHealthCheckTest TransformerFunc = func(data interface{}) (interface{}, error) {
1257-
switch value := data.(type) {
1258-
case string:
1259-
return append([]string{"CMD-SHELL"}, value), nil
1260-
case []interface{}:
1261-
return value, nil
1262-
default:
1263-
return value, errors.Errorf("invalid type %T for healthcheck.test", value)
1264-
}
1265-
}
1266-
12671255
func toMapStringString(value map[string]interface{}, allowNil bool) map[string]interface{} {
12681256
output := make(map[string]interface{})
12691257
for key, value := range value {

types/healthcheck.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2020 The Compose Specification Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package types
18+
19+
import (
20+
"fmt"
21+
)
22+
23+
// HealthCheckConfig the healthcheck configuration for a service
24+
type HealthCheckConfig struct {
25+
Test HealthCheckTest `yaml:"test,omitempty" json:"test,omitempty"`
26+
Timeout *Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"`
27+
Interval *Duration `yaml:"interval,omitempty" json:"interval,omitempty"`
28+
Retries *uint64 `yaml:"retries,omitempty" json:"retries,omitempty"`
29+
StartPeriod *Duration `yaml:"start_period,omitempty" json:"start_period,omitempty"`
30+
StartInterval *Duration `yaml:"start_interval,omitempty" json:"start_interval,omitempty"`
31+
Disable bool `yaml:"disable,omitempty" json:"disable,omitempty"`
32+
33+
Extensions Extensions `yaml:"#extensions,inline" json:"-"`
34+
}
35+
36+
// HealthCheckTest is the command run to test the health of a service
37+
type HealthCheckTest []string
38+
39+
func (l *HealthCheckTest) DecodeMapstructure(value interface{}) error {
40+
switch v := value.(type) {
41+
case string:
42+
*l = []string{"CMD-SHELL", v}
43+
case []interface{}:
44+
seq := make([]string, len(v))
45+
for i, e := range v {
46+
seq[i] = e.(string)
47+
}
48+
*l = seq
49+
default:
50+
return fmt.Errorf("unexpected value type %T for healthcheck.test", value)
51+
}
52+
return nil
53+
}

types/types.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -529,22 +529,6 @@ type DeployConfig struct {
529529
Extensions Extensions `yaml:"#extensions,inline" json:"-"`
530530
}
531531

532-
// HealthCheckConfig the healthcheck configuration for a service
533-
type HealthCheckConfig struct {
534-
Test HealthCheckTest `yaml:"test,omitempty" json:"test,omitempty"`
535-
Timeout *Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"`
536-
Interval *Duration `yaml:"interval,omitempty" json:"interval,omitempty"`
537-
Retries *uint64 `yaml:"retries,omitempty" json:"retries,omitempty"`
538-
StartPeriod *Duration `yaml:"start_period,omitempty" json:"start_period,omitempty"`
539-
StartInterval *Duration `yaml:"start_interval,omitempty" json:"start_interval,omitempty"`
540-
Disable bool `yaml:"disable,omitempty" json:"disable,omitempty"`
541-
542-
Extensions Extensions `yaml:"#extensions,inline" json:"-"`
543-
}
544-
545-
// HealthCheckTest is the command run to test the health of a service
546-
type HealthCheckTest []string
547-
548532
// UpdateConfig the service update configuration
549533
type UpdateConfig struct {
550534
Parallelism *uint64 `yaml:"parallelism,omitempty" json:"parallelism,omitempty"`

0 commit comments

Comments
 (0)