Skip to content

Commit

Permalink
Merge pull request #53 from calyptia/equal
Browse files Browse the repository at this point in the history
add equal method
  • Loading branch information
niedbalski authored Jan 10, 2023
2 parents 3dfb7b0 + 5d7602a commit dad49f5
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 6 deletions.
49 changes: 49 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"strings"

"golang.org/x/exp/maps"
"golang.org/x/exp/slices"

"github.com/calyptia/go-fluentbit-config/property"
)

Expand Down Expand Up @@ -69,6 +72,42 @@ func (c *Config) AddSection(kind SectionKind, props property.Properties) {
}
}

func (c Config) Equal(target Config) bool {
if !c.Env.Equal(target.Env) {
return false
}

if !slices.Equal(c.Includes, target.Includes) {
return false
}

if !c.Service.Equal(target.Service) {
return false
}

if !equalByNames(c.Customs, target.Customs) {
return false
}

if !equalByNames(c.Pipeline.Inputs, target.Pipeline.Inputs) {
return false
}

if !equalByNames(c.Pipeline.Parsers, target.Pipeline.Parsers) {
return false
}

if !equalByNames(c.Pipeline.Filters, target.Pipeline.Filters) {
return false
}

if !equalByNames(c.Pipeline.Outputs, target.Pipeline.Outputs) {
return false
}

return true
}

// Name from properties.
func Name(props property.Properties) string {
nameVal, ok := props.Get("name")
Expand All @@ -82,3 +121,13 @@ func Name(props property.Properties) string {

return strings.TrimSpace(strings.ToValidUTF8(fmt.Sprintf("%v", nameVal), ""))
}

func equalByNames(a, b []ByName) bool {
return slices.EqualFunc(a, b, equalByName)
}

func equalByName(a, b ByName) bool {
return maps.EqualFunc(a, b, func(v1, v2 property.Properties) bool {
return v1.Equal(v2)
})
}
151 changes: 151 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"gopkg.in/yaml.v3"

fluentbitconfig "github.com/calyptia/go-fluentbit-config"
"github.com/calyptia/go-fluentbit-config/property"
)

func Test_Config(t *testing.T) {
Expand Down Expand Up @@ -69,3 +70,153 @@ func makeTestName(fp string) string {
s = strings.ReplaceAll(s, "-", "_")
return strings.ReplaceAll(s, " ", "_")
}

func TestConfig_Equal(t *testing.T) {
t.Run("equal_env", func(t *testing.T) {
a := fluentbitconfig.Config{
Env: property.Properties{
{Key: "foo", Value: "bar"},
},
}
b := fluentbitconfig.Config{
Env: property.Properties{
{Key: "foo", Value: "bar"},
},
}
assert.True(t, a.Equal(b))
})

t.Run("not_equal_env", func(t *testing.T) {
a := fluentbitconfig.Config{
Env: property.Properties{
{Key: "a", Value: "b"},
},
}
b := fluentbitconfig.Config{
Env: property.Properties{
{Key: "b", Value: "c"},
},
}
assert.False(t, a.Equal(b))
})

t.Run("equal_includes", func(t *testing.T) {
a := fluentbitconfig.Config{
Includes: []string{"foo"},
}
b := fluentbitconfig.Config{
Includes: []string{"foo"},
}
assert.True(t, a.Equal(b))
})

t.Run("not_equal_includes", func(t *testing.T) {
a := fluentbitconfig.Config{
Includes: []string{"foo"},
}
b := fluentbitconfig.Config{
Includes: []string{"bar"},
}
assert.False(t, a.Equal(b))
})

t.Run("equal_input", func(t *testing.T) {
a := fluentbitconfig.Config{
Pipeline: fluentbitconfig.Pipeline{
Inputs: []fluentbitconfig.ByName{{
"dummy": property.Properties{
{Key: "name", Value: "dummy"},
{Key: "rate", Value: 10},
}},
},
},
}
b := fluentbitconfig.Config{
Pipeline: fluentbitconfig.Pipeline{
Inputs: []fluentbitconfig.ByName{{
"dummy": property.Properties{
{Key: "name", Value: "dummy"},
{Key: "rate", Value: 10},
}},
},
},
}
assert.True(t, a.Equal(b))
})

t.Run("not_equal_input", func(t *testing.T) {
a := fluentbitconfig.Config{
Pipeline: fluentbitconfig.Pipeline{
Inputs: []fluentbitconfig.ByName{{
"dummy": property.Properties{
{Key: "name", Value: "dummy"},
{Key: "rate", Value: 10},
}},
},
},
}
b := fluentbitconfig.Config{
Pipeline: fluentbitconfig.Pipeline{
Inputs: []fluentbitconfig.ByName{{
"dummy": property.Properties{
{Key: "name", Value: "dummy"},
{Key: "rate", Value: 22},
}},
},
},
}
assert.False(t, a.Equal(b))
})

t.Run("not_equal_properties_out_of_order", func(t *testing.T) {
a := fluentbitconfig.Config{
Pipeline: fluentbitconfig.Pipeline{
Inputs: []fluentbitconfig.ByName{{
"foo": property.Properties{
{Key: "name", Value: "foo"},
{Key: "test_key", Value: "test_value"},
}},
},
},
}
b := fluentbitconfig.Config{
Pipeline: fluentbitconfig.Pipeline{
Inputs: []fluentbitconfig.ByName{{
"foo": property.Properties{
{Key: "test_key", Value: "test_value"},
{Key: "name", Value: "foo"},
}},
},
},
}
assert.False(t, a.Equal(b))
})

t.Run("not_equal_by_names_out_of_order", func(t *testing.T) {
a := fluentbitconfig.Config{
Pipeline: fluentbitconfig.Pipeline{
Inputs: []fluentbitconfig.ByName{{
"one": property.Properties{
{Key: "name", Value: "one"},
}}, {
"two": property.Properties{
{Key: "name", Value: "two"},
}},
},
},
}
b := fluentbitconfig.Config{
Pipeline: fluentbitconfig.Pipeline{
Inputs: []fluentbitconfig.ByName{{
"two": property.Properties{
{Key: "name", Value: "two"},
},
"one": property.Properties{
{Key: "name", Value: "one"},
}},
},
},
}
assert.False(t, a.Equal(b))
})
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module github.com/calyptia/go-fluentbit-config
go 1.19

require (
github.com/alecthomas/assert/v2 v2.2.0
github.com/alecthomas/assert/v2 v2.2.1
golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/alecthomas/repr v0.1.1 // indirect
github.com/alecthomas/repr v0.2.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
github.com/alecthomas/assert/v2 v2.2.0 h1:f6L/b7KE2bfA+9O4FL3CM/xJccDEwPVYd5fALBiuwvw=
github.com/alecthomas/assert/v2 v2.2.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA=
github.com/alecthomas/repr v0.1.1 h1:87P60cSmareLAxMc4Hro0r2RBY4ROm0dYwkJNpS4pPs=
github.com/alecthomas/repr v0.1.1/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink=
github.com/alecthomas/assert/v2 v2.2.1/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a h1:tlXy25amD5A7gOfbXdqCGN5k8ESEed/Ee1E5RcrYnqU=
golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
8 changes: 8 additions & 0 deletions property/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package property

import (
"strings"

"golang.org/x/exp/slices"
)

// Properties list.
Expand Down Expand Up @@ -88,6 +90,12 @@ func (pp *Properties) Add(key string, value any) {
*pp = append(*pp, Property{Key: key, Value: value})
}

func (pp Properties) Equal(target Properties) bool {
return slices.EqualFunc(pp, target, func(a, b Property) bool {
return a.Key == b.Key && a.Value == b.Value
})
}

// Property key-value pair.
type Property struct {
Key string
Expand Down

0 comments on commit dad49f5

Please sign in to comment.