github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/compose/loader/interpolate.go (about)

     1  package loader
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  
     7  	interp "github.com/docker/cli/cli/compose/interpolation"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  var interpolateTypeCastMapping = map[interp.Path]interp.Cast{
    12  	servicePath("configs", interp.PathMatchList, "mode"):             toInt,
    13  	servicePath("secrets", interp.PathMatchList, "mode"):             toInt,
    14  	servicePath("healthcheck", "retries"):                            toInt,
    15  	servicePath("healthcheck", "disable"):                            toBoolean,
    16  	servicePath("deploy", "replicas"):                                toInt,
    17  	servicePath("deploy", "update_config", "parallelism"):            toInt,
    18  	servicePath("deploy", "update_config", "max_failure_ratio"):      toFloat,
    19  	servicePath("deploy", "rollback_config", "parallelism"):          toInt,
    20  	servicePath("deploy", "rollback_config", "max_failure_ratio"):    toFloat,
    21  	servicePath("deploy", "restart_policy", "max_attempts"):          toInt,
    22  	servicePath("deploy", "placement", "max_replicas_per_node"):      toInt,
    23  	servicePath("ports", interp.PathMatchList, "target"):             toInt,
    24  	servicePath("ports", interp.PathMatchList, "published"):          toInt,
    25  	servicePath("ulimits", interp.PathMatchAll):                      toInt,
    26  	servicePath("ulimits", interp.PathMatchAll, "hard"):              toInt,
    27  	servicePath("ulimits", interp.PathMatchAll, "soft"):              toInt,
    28  	servicePath("privileged"):                                        toBoolean,
    29  	servicePath("read_only"):                                         toBoolean,
    30  	servicePath("stdin_open"):                                        toBoolean,
    31  	servicePath("tty"):                                               toBoolean,
    32  	servicePath("volumes", interp.PathMatchList, "read_only"):        toBoolean,
    33  	servicePath("volumes", interp.PathMatchList, "volume", "nocopy"): toBoolean,
    34  	iPath("networks", interp.PathMatchAll, "external"):               toBoolean,
    35  	iPath("networks", interp.PathMatchAll, "internal"):               toBoolean,
    36  	iPath("networks", interp.PathMatchAll, "attachable"):             toBoolean,
    37  	iPath("volumes", interp.PathMatchAll, "external"):                toBoolean,
    38  	iPath("secrets", interp.PathMatchAll, "external"):                toBoolean,
    39  	iPath("configs", interp.PathMatchAll, "external"):                toBoolean,
    40  }
    41  
    42  func iPath(parts ...string) interp.Path {
    43  	return interp.NewPath(parts...)
    44  }
    45  
    46  func servicePath(parts ...string) interp.Path {
    47  	return iPath(append([]string{"services", interp.PathMatchAll}, parts...)...)
    48  }
    49  
    50  func toInt(value string) (interface{}, error) {
    51  	return strconv.Atoi(value)
    52  }
    53  
    54  func toFloat(value string) (interface{}, error) {
    55  	return strconv.ParseFloat(value, 64)
    56  }
    57  
    58  // should match http://yaml.org/type/bool.html
    59  func toBoolean(value string) (interface{}, error) {
    60  	switch strings.ToLower(value) {
    61  	case "y", "yes", "true", "on":
    62  		return true, nil
    63  	case "n", "no", "false", "off":
    64  		return false, nil
    65  	default:
    66  		return nil, errors.Errorf("invalid boolean: %s", value)
    67  	}
    68  }
    69  
    70  func interpolateConfig(configDict map[string]interface{}, opts interp.Options) (map[string]interface{}, error) {
    71  	return interp.Interpolate(configDict, opts)
    72  }