Skip to content

Commit

Permalink
decode Healthcheck.Test using DecodeMapstructure
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Oct 2, 2023
1 parent 74c59b5 commit 4fc0069
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 28 deletions.
12 changes: 0 additions & 12 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,6 @@ type Transformer struct {
func createTransformHook(additionalTransformers ...Transformer) mapstructure.DecodeHookFuncType {
transforms := map[reflect.Type]func(interface{}) (interface{}, error){
reflect.TypeOf(types.External{}): transformExternal,
reflect.TypeOf(types.HealthCheckTest{}): transformHealthCheckTest,
reflect.TypeOf(types.Options{}): transformMapStringString,
reflect.TypeOf(types.UlimitsConfig{}): transformUlimits,
reflect.TypeOf([]types.ServicePortConfig{}): transformServicePort,
Expand Down Expand Up @@ -1253,17 +1252,6 @@ func transformValueToMapEntry(value string, separator string, allowNil bool) (st
}
}

var transformHealthCheckTest TransformerFunc = func(data interface{}) (interface{}, error) {
switch value := data.(type) {
case string:
return append([]string{"CMD-SHELL"}, value), nil
case []interface{}:
return value, nil
default:
return value, errors.Errorf("invalid type %T for healthcheck.test", value)
}
}

func toMapStringString(value map[string]interface{}, allowNil bool) map[string]interface{} {
output := make(map[string]interface{})
for key, value := range value {
Expand Down
53 changes: 53 additions & 0 deletions types/healthcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package types

import (
"fmt"
)

// HealthCheckConfig the healthcheck configuration for a service
type HealthCheckConfig struct {
Test HealthCheckTest `yaml:"test,omitempty" json:"test,omitempty"`
Timeout *Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"`
Interval *Duration `yaml:"interval,omitempty" json:"interval,omitempty"`
Retries *uint64 `yaml:"retries,omitempty" json:"retries,omitempty"`
StartPeriod *Duration `yaml:"start_period,omitempty" json:"start_period,omitempty"`
StartInterval *Duration `yaml:"start_interval,omitempty" json:"start_interval,omitempty"`
Disable bool `yaml:"disable,omitempty" json:"disable,omitempty"`

Extensions Extensions `yaml:"#extensions,inline" json:"-"`
}

// HealthCheckTest is the command run to test the health of a service
type HealthCheckTest []string

func (l *HealthCheckTest) DecodeMapstructure(value interface{}) error {
switch v := value.(type) {
case string:
*l = []string{"CMD-SHELL", v}
case []interface{}:
seq := make([]string, len(v))
for i, e := range v {
seq[i] = e.(string)
}
*l = seq
default:
return fmt.Errorf("unexpected value type %T for healthcheck.test", value)
}
return nil
}
16 changes: 0 additions & 16 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,22 +529,6 @@ type DeployConfig struct {
Extensions Extensions `yaml:"#extensions,inline" json:"-"`
}

// HealthCheckConfig the healthcheck configuration for a service
type HealthCheckConfig struct {
Test HealthCheckTest `yaml:"test,omitempty" json:"test,omitempty"`
Timeout *Duration `yaml:"timeout,omitempty" json:"timeout,omitempty"`
Interval *Duration `yaml:"interval,omitempty" json:"interval,omitempty"`
Retries *uint64 `yaml:"retries,omitempty" json:"retries,omitempty"`
StartPeriod *Duration `yaml:"start_period,omitempty" json:"start_period,omitempty"`
StartInterval *Duration `yaml:"start_interval,omitempty" json:"start_interval,omitempty"`
Disable bool `yaml:"disable,omitempty" json:"disable,omitempty"`

Extensions Extensions `yaml:"#extensions,inline" json:"-"`
}

// HealthCheckTest is the command run to test the health of a service
type HealthCheckTest []string

// UpdateConfig the service update configuration
type UpdateConfig struct {
Parallelism *uint64 `yaml:"parallelism,omitempty" json:"parallelism,omitempty"`
Expand Down

0 comments on commit 4fc0069

Please sign in to comment.