From 4b3a326e9ba0501a8f895e2f3d8be63c29feaa40 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Tue, 22 Mar 2022 17:50:54 +0100 Subject: [PATCH 1/3] Handle string ports in docker compose v2 config yaml --- internal/compose/compose.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 3cd329662c..79b8daeccf 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -54,6 +54,20 @@ type portMapping struct { Protocol string } +type intOrStringYaml int + +func (p *intOrStringYaml) UnmarshalYAML(node *yaml.Node) error { + var s string + err := node.Decode(&s) + if err == nil { + i, err := strconv.Atoi(s) + *p = intOrStringYaml(i) + return err + } + + return node.Decode(p) +} + // UnmarshalYAML unmarshals a Docker Compose port mapping in YAML to // a portMapping. func (p *portMapping) UnmarshalYAML(node *yaml.Node) error { @@ -68,8 +82,8 @@ func (p *portMapping) UnmarshalYAML(node *yaml.Node) error { var s struct { HostIP string `yaml:"host_ip"` - Target int - Published int + Target intOrStringYaml + Published intOrStringYaml Protocol string } @@ -77,8 +91,8 @@ func (p *portMapping) UnmarshalYAML(node *yaml.Node) error { return errors.Wrap(err, "could not unmarshal YAML map node") } - p.InternalPort = s.Target - p.ExternalPort = s.Published + p.InternalPort = int(s.Target) + p.ExternalPort = int(s.Published) p.Protocol = s.Protocol p.ExternalIP = s.HostIP return nil From fad36148b9b48967b1018b05dbe885958b6f63f1 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Tue, 22 Mar 2022 18:03:05 +0100 Subject: [PATCH 2/3] Add unit tests --- internal/compose/compose_test.go | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 internal/compose/compose_test.go diff --git a/internal/compose/compose_test.go b/internal/compose/compose_test.go new file mode 100644 index 0000000000..4d893a1d67 --- /dev/null +++ b/internal/compose/compose_test.go @@ -0,0 +1,33 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package compose + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" +) + +func TestIntOrStringYaml(t *testing.T) { + cases := []struct { + yaml string + expected int + }{ + {`"9200"`, 9200}, + {`'9200'`, 9200}, + {`9200`, 9200}, + } + + for _, c := range cases { + t.Run(c.yaml, func(t *testing.T) { + var n intOrStringYaml + err := yaml.Unmarshal([]byte(c.yaml), &n) + require.NoError(t, err) + assert.Equal(t, c.expected, int(n)) + }) + } +} From 0a42d729516fefc8efaf8859ef779842f904ce93 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Wed, 23 Mar 2022 09:31:11 +0100 Subject: [PATCH 3/3] Add comments --- internal/compose/compose.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/compose/compose.go b/internal/compose/compose.go index 79b8daeccf..07e61b0948 100644 --- a/internal/compose/compose.go +++ b/internal/compose/compose.go @@ -81,9 +81,9 @@ func (p *portMapping) UnmarshalYAML(node *yaml.Node) error { } var s struct { - HostIP string `yaml:"host_ip"` - Target intOrStringYaml - Published intOrStringYaml + HostIP string `yaml:"host_ip"` + Target intOrStringYaml // Docker compose v2 can define ports as strings. + Published intOrStringYaml // Docker compose v2 can define ports as strings. Protocol string }