Skip to content

Commit

Permalink
Merge pull request #36 from calyptia/fix-address-type
Browse files Browse the repository at this point in the history
Fix address type conversion to string.
  • Loading branch information
niedbalski authored Sep 28, 2022
2 parents 8156e24 + 9ef998f commit 2a0b405
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 24 deletions.
51 changes: 29 additions & 22 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,19 @@ func (a *Value) Equals(b *Value) bool {
return false
}
return *a.Float == *b.Float
} else if a.Address != nil {
if b.Address == nil {
return false
}
return *a.Address == *b.Address
}
return false
}

func (v *Value) Value() interface{} {
switch {
case v.Address != nil:
return *v.Address
case v.TemplateVariable != nil:
return *v.TemplateVariable
case v.JsonObject != nil:
Expand Down Expand Up @@ -411,8 +418,8 @@ func (value *Value) dumpValueINI(ini *bytes.Buffer) error {
return nil
}

func (values Values) dumpValueINI(ini *bytes.Buffer) error {
for _, value := range values {
func (vs Values) dumpValueINI(ini *bytes.Buffer) error {
for _, value := range vs {
if err := value.dumpValueINI(ini); err != nil {
return err
}
Expand Down Expand Up @@ -526,45 +533,45 @@ func ParseINI(data []byte) (*Config, error) {

type Fields []Field

func (fs *Fields) UnmarshalYAML(unmarshal func(v interface{}) error) error {
func (fields *Fields) UnmarshalYAML(unmarshal func(v interface{}) error) error {
kv := make(map[string]interface{})
if err := unmarshal(&kv); err != nil {
return err
}
fields := make([]Field, 0)
newFields := make([]Field, 0)
for k, v := range kv {
switch t := v.(type) {
case string:
fields = append(fields, Field{
newFields = append(newFields, Field{
Key: k,
Values: []Value{{
String: &t,
}},
})
case int:
i := int64(v.(int))
fields = append(fields, Field{
newFields = append(newFields, Field{
Key: k,
Values: []Value{{
Number: &i,
}},
})
case int64:
fields = append(fields, Field{
newFields = append(newFields, Field{
Key: k,
Values: []Value{{
Number: &t,
}},
})
case float64:
fields = append(fields, Field{
newFields = append(newFields, Field{
Key: k,
Values: []Value{{
Float: &t,
}},
})
case bool:
fields = append(fields, Field{
newFields = append(newFields, Field{
Key: k,
Values: []Value{{
Bool: &t,
Expand All @@ -574,14 +581,14 @@ func (fs *Fields) UnmarshalYAML(unmarshal func(v interface{}) error) error {
return fmt.Errorf("unknown type: %+v: %+v", t, v)
}
}
*fs = fields
*fields = newFields

return nil
}

func (fs Fields) MarshalYAML() (interface{}, error) {
func (fields Fields) MarshalYAML() (interface{}, error) {
fmap := make(map[string]interface{})
for _, field := range fs {
for _, field := range fields {
if len(field.Values) == 1 {
switch true {
case field.Values[0].String != nil:
Expand All @@ -608,11 +615,11 @@ func (fs Fields) MarshalYAML() (interface{}, error) {
return fmap, nil
}

func (fs Fields) MarshalJSON() ([]byte, error) {
func (fields Fields) MarshalJSON() ([]byte, error) {
bfields := bytes.NewBuffer([]byte(""))
bfields.Write([]byte("{"))

for fidx, field := range fs {
for fidx, field := range fields {
bfields.Write([]byte(fmt.Sprintf("\"%s\": ", field.Key)))
val := bytes.NewBuffer([]byte(""))
isstr := false
Expand Down Expand Up @@ -681,7 +688,7 @@ func (fs Fields) MarshalJSON() ([]byte, error) {
bfields.Write(val.Bytes())
}

if fidx < len(fs)-1 {
if fidx < len(fields)-1 {
bfields.Write([]byte(", "))
}
}
Expand All @@ -690,8 +697,8 @@ func (fs Fields) MarshalJSON() ([]byte, error) {
return bfields.Bytes(), nil
}

func (fs *Fields) UnmarshalJSON(b []byte) error {
fields := make(Fields, 0)
func (fields *Fields) UnmarshalJSON(b []byte) error {
newFields := make(Fields, 0)

dec := json.NewDecoder(bytes.NewReader(b))
dec.UseNumber()
Expand All @@ -718,7 +725,7 @@ func (fs *Fields) UnmarshalJSON(b []byte) error {

switch t := v.(type) {
case string:
fields = append(fields, Field{
newFields = append(newFields, Field{
Key: k.(string),
Values: []Value{{
String: &t,
Expand All @@ -731,22 +738,22 @@ func (fs *Fields) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}
fields = append(fields, Field{
newFields = append(newFields, Field{
Key: k.(string),
Values: []Value{{
Float: &f,
}},
})
} else {
fields = append(fields, Field{
newFields = append(newFields, Field{
Key: k.(string),
Values: []Value{{
Number: &i,
}},
})
}
case bool:
fields = append(fields, Field{
newFields = append(newFields, Field{
Key: k.(string),
Values: []Value{{
Bool: &t,
Expand All @@ -757,7 +764,7 @@ func (fs *Fields) UnmarshalJSON(b []byte) error {
}
}

*fs = fields
*fields = newFields
return nil
}

Expand Down
75 changes: 73 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ func TestNewConfigFromBytes(t *testing.T) {
[INPUT]
Name tcp
Port 5557
Tag foobat
Tag foobar
[OUTPUT]
Name stdout
Expand Down Expand Up @@ -809,7 +809,7 @@ func TestNewConfigFromBytes(t *testing.T) {
String: ptr("5557"),
}}},
{Key: "Tag", Values: []Value{{
String: ptr("foobat"),
String: ptr("foobar"),
}}},
},
}, {
Expand All @@ -826,6 +826,77 @@ func TestNewConfigFromBytes(t *testing.T) {
},
expectedError: false,
},
{
name: "test valid multiple outputs with hosts",
config: []byte(`[OUTPUT]
Name tcp
Host 172.168.0.1
Port 5556
Tag foobar
[OUTPUT]
Name opensearch
Host 127.0.0.1
Port 5550
Tag foobar
[OUTPUT]
Name gelf
Host api.gelf.test
Match *
`),
expected: Config{
Sections: []ConfigSection{{
Type: OutputSection,
Fields: []Field{
{Key: "Name", Values: []Value{{
String: ptr("tcp"),
}}},
{Key: "Host", Values: []Value{{
Address: ptr("172.168.0.1"),
}}},
{Key: "Port", Values: []Value{{
String: ptr("5556"),
List: nil},
}},
{Key: "Tag", Values: []Value{{
String: ptr("foobar"),
}}},
},
}, {
Type: OutputSection,
Fields: []Field{
{Key: "Name", Values: []Value{{
String: ptr("opensearch"),
}}},
{Key: "Host", Values: []Value{{
Address: ptr("127.0.0.1"),
}}},
{Key: "Port", Values: []Value{{
String: ptr("5557"),
}}},
{Key: "Tag", Values: []Value{{
String: ptr("foobar"),
}}},
},
}, {
Type: OutputSection,
Fields: []Field{
{Key: "name", Values: []Value{{
String: ptr("gelf"),
}}},
{Key: "Host", Values: []Value{{
Address: ptr("api.gelf.test"),
}}},
{Key: "Match", Values: []Value{{
String: ptr("*"),
}}},
},
}},
},
expectedError: false,
},

{
name: "test time formats",
config: []byte(`
Expand Down

0 comments on commit 2a0b405

Please sign in to comment.