Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
yaml "github.com/goccy/go-yaml"

"github.com/calyptia/go-fluentbit-config/v2/property"
)
Expand Down Expand Up @@ -495,11 +495,11 @@ func Example_configWithProcessors() {
// inputs:
// - name: dummy
// processors:
// logs:
// - name: calyptia
// actions:
// type: block_keys
// opts:
// regex: star
// regexEngine: pcre2
// logs:
// - name: calyptia
// actions:
// type: block_keys
// opts:
// regex: star
// regexEngine: pcre2
}
8 changes: 4 additions & 4 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"io"
"strings"

"gopkg.in/yaml.v3"
"github.com/goccy/go-yaml"
)

var ErrFormatUnknown = errors.New("format unknown")
Expand Down Expand Up @@ -40,8 +40,7 @@ func ParseAsClassic(raw string) (Config, error) {

func ParseAsYAML(raw string) (Config, error) {
var out Config
dec := yaml.NewDecoder(strings.NewReader(raw))
dec.KnownFields(true)
dec := yaml.NewDecoder(strings.NewReader(raw), yaml.Strict())
err := dec.Decode(&out)
if errors.Is(err, io.EOF) {
return out, nil
Expand Down Expand Up @@ -84,7 +83,8 @@ func (c Config) DumpAsClassic() (string, error) {
}

func (c Config) DumpAsYAML() (string, error) {
b, err := yaml.Marshal(c)
b, err := yaml.MarshalWithOptions(c,
yaml.Indent(4), yaml.IndentSequence(true), yaml.UseSingleQuote(true))
if err != nil {
return "", err
}
Expand Down
6 changes: 3 additions & 3 deletions dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ func TestParseAsYAML(t *testing.T) {
inputs:
- name: dummy
`))
require.EqualError(t, err, "yaml: unmarshal errors:\n line 1: field pipelines not found in type fluentbitconfig.Config")
require.EqualError(t, err, "[1:1] unknown field \"pipelines\"\n> 1 | pipelines:\n ^\n 2 | inputs:\n 3 | - name: dummy")
})

t.Run("unknown_section", func(t *testing.T) {
_, err := ParseAsYAML(configLiteral(`
unknown:
- name: dummy
`))
require.EqualError(t, err, "yaml: unmarshal errors:\n line 1: field unknown not found in type fluentbitconfig.Config")
require.EqualError(t, err, "[1:1] unknown field \"unknown\"\n> 1 | unknown:\n ^\n 2 | - name: dummy")
})

t.Run("unknown_pipeline_plugins_kind", func(t *testing.T) {
Expand All @@ -38,7 +38,7 @@ func TestParseAsYAML(t *testing.T) {
foos:
- name: bar
`))
require.EqualError(t, err, "yaml: unmarshal errors:\n line 2: field foos not found in type fluentbitconfig.Pipeline")
require.EqualError(t, err, "[2:5] unknown field \"foos\"\n 1 | pipeline:\n> 2 | foos:\n ^\n 3 | - name: bar")
})

t.Run("empty", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.23.0
toolchain go1.24.2

require (
github.com/goccy/go-yaml v1.17.1
github.com/muesli/reflow v0.3.0
github.com/spf13/pflag v1.0.6
github.com/stretchr/testify v1.10.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goccy/go-yaml v1.17.1 h1:LI34wktB2xEE3ONG/2Ar54+/HJVBriAGJ55PHls4YuY=
github.com/goccy/go-yaml v1.17.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
Expand Down
8 changes: 4 additions & 4 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"fmt"

yaml "github.com/goccy/go-yaml"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"

"github.com/calyptia/go-fluentbit-config/v2/property"
)
Expand Down Expand Up @@ -50,9 +50,9 @@ func (plugins *Plugins) UnmarshalJSON(data []byte) error {
return nil
}

func (plugins *Plugins) UnmarshalYAML(node *yaml.Node) error {
func (plugins *Plugins) UnmarshalYAML(node []byte) error {
var dest []Plugin
err := node.Decode(&dest)
err := yaml.Unmarshal(node, &dest)
if err != nil {
return err
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (p *Plugin) UnmarshalJSON(data []byte) error {
return nil
}

func (p *Plugin) UnmarshalYAML(node *yaml.Node) error {
func (p *Plugin) UnmarshalYAML(node []byte) error {
err := p.Properties.UnmarshalYAML(node)
if err != nil {
return err
Expand Down
44 changes: 16 additions & 28 deletions property/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package property
import (
"fmt"

"gopkg.in/yaml.v3"
"github.com/goccy/go-yaml"
)

// MarshalYAML implements yaml.Marshaler interface
Expand All @@ -17,45 +17,33 @@ func (pp Properties) MarshalYAML() (any, error) {
return []any{}, nil
}

node := &yaml.Node{
Kind: yaml.MappingNode,
}

for _, p := range pp {
valueNode := &yaml.Node{}
if err := valueNode.Encode(p.Value); err != nil {
return nil, fmt.Errorf("yaml encode property value: %w", err)
}
properties := make(yaml.MapSlice, len(pp))

node.Content = append(node.Content, &yaml.Node{
Kind: yaml.ScalarNode,
Value: p.Key,
}, valueNode)
for pindex, prop := range pp {
properties[pindex] = yaml.MapItem{Key: prop.Key, Value: prop.Value}
}

return node, nil
return properties, nil
}

// UnmarshalYAML implements yaml.Unmarshaler interface
// to unmarshal an object into a sorted list of properties.
func (pp *Properties) UnmarshalYAML(node *yaml.Node) error {
d := len(node.Content)
if d%2 != 0 {
return fmt.Errorf("expected even items for key-value")
func (pp *Properties) UnmarshalYAML(node []byte) error {
var mprops yaml.MapSlice

if err := yaml.Unmarshal(node, &mprops); err != nil {
return err
}

for i := 0; i < d; i += 2 {
for _, mprop := range mprops {
var prop Property
var ok bool

keyNode := node.Content[i]
if err := keyNode.Decode(&prop.Key); err != nil {
return fmt.Errorf("yaml decode property key: %w", err)
}

valueNode := node.Content[i+1]
if err := valueNode.Decode(&prop.Value); err != nil {
return fmt.Errorf("yaml decode property value: %w", err)
prop.Key, ok = mprop.Key.(string)
if !ok {
return fmt.Errorf("unable to unmarshal key")
}
prop.Value = mprop.Value

*pp = append(*pp, prop)
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/array-value.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pipeline:
filters:
- name: record_modifier
record:
- key1 value1
- key2 value2
- key1 value1
- key2 value2
2 changes: 1 addition & 1 deletion testdata/empty-string.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
customs:
- name: empty_string
value: ""
value: ''
2 changes: 1 addition & 1 deletion testdata/example-multiline-parser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pipeline:
Path: /var/log/containers/*.log
Tag: containers
multiline.parser: docker , cri
Skip_Long_Lines: "On"
Skip_Long_Lines: 'On'
8 changes: 4 additions & 4 deletions testdata/full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ env:
BAZ: qux
service:
flush: 1
daemon: "on"
daemon: 'on'
log_level: error
http_server: "on"
http_server: 'on'
customs:
- name: calyptia
api_key: secret
Expand All @@ -25,8 +25,8 @@ pipeline:
- name: record_modifier
match: dummy
record:
- key1 value1
- key2 value2
- key1 value1
- key2 value2
outputs:
- name: stdout
match: '*'
4 changes: 2 additions & 2 deletions testdata/multiline-value.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
customs:
- name: custom
multiline: |-
FOO
BAR
FOO
BAR
42 changes: 21 additions & 21 deletions testdata/processors/processors.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
service:
log_level: info
http_server: "on"
http_server: 'on'
http_listen: 0.0.0.0
http_port: 2021
pipeline:
inputs:
- name: dummy
tag: test-tag
processors:
logs:
- add: hostname monox
name: modify
- call: append_tag
code: |
function append_tag(tag, timestamp, record)
new_record = record
new_record["tag"] = tag
return 1, timestamp, new_record
end
name: lua
logs:
- add: hostname monox
name: modify
- call: append_tag
code: |
function append_tag(tag, timestamp, record)
new_record = record
new_record["tag"] = tag
return 1, timestamp, new_record
end
name: lua
outputs:
- name: stdout
format: json_lines
match: '*'
processors:
logs:
- call: add_field
code: |
function add_field(tag, timestamp, record)
new_record = record
new_record["output"] = "new data"
return 1, timestamp, new_record
end
name: lua
logs:
- call: add_field
code: |
function add_field(tag, timestamp, record)
new_record = record
new_record["output"] = "new data"
return 1, timestamp, new_record
end
name: lua
4 changes: 2 additions & 2 deletions testdata/service.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
service:
flush: 1
daemon: "on"
daemon: 'on'
log_level: error
http_server: "on"
http_server: 'on'
3 changes: 2 additions & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ func (c Config) ValidateWithSchema(schema Schema) error {
switch property.Value.(type) {
case bool:
case int:
case uint64:
case int64:
case float64:
case string:
default:
return fmt.Errorf("invalid type (%T) for service setting: %s",
return fmt.Errorf("invalid type (%T) for service setting: %s",
property.Value,
property.Key)
}
Expand Down
2 changes: 1 addition & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
yaml "github.com/goccy/go-yaml"
)

func TestConfig_Validate(t *testing.T) {
Expand Down