diff --git a/nontransparent/trailer_type.go b/nontransparent/trailer_type.go index 4dc4e77..e377c9b 100644 --- a/nontransparent/trailer_type.go +++ b/nontransparent/trailer_type.go @@ -38,14 +38,28 @@ func (t TrailerType) Value() (int, error) { // TrailerTypeFromString returns a TrailerType given a string. func TrailerTypeFromString(s string) (TrailerType, error) { switch strings.ToUpper(s) { - case "LF": + case `"LF"`: + fallthrough + case `'LF'`: + fallthrough + case `LF`: return LF, nil - case "NUL": + + case `"NUL"`: + fallthrough + case `'NUL'`: + fallthrough + case `NUL`: return NUL, nil } return -1, fmt.Errorf("unknown TrailerType") } +// UnmarshalTOML decodes trailer type from TOML data. +func (t *TrailerType) UnmarshalTOML(data []byte) (err error) { + return t.UnmarshalText(data) +} + // UnmarshalText implements encoding.TextUnmarshaler func (t *TrailerType) UnmarshalText(data []byte) (err error) { *t, err = TrailerTypeFromString(string(data)) diff --git a/nontransparent/trailer_type_test.go b/nontransparent/trailer_type_test.go index c68f0d7..af56e5b 100644 --- a/nontransparent/trailer_type_test.go +++ b/nontransparent/trailer_type_test.go @@ -12,6 +12,37 @@ type trailerWrapper struct { Trailer TrailerType `json:"trailer"` } +func TestUnmarshalTOML(t *testing.T) { + var t1 TrailerType + t1.UnmarshalTOML([]byte(`"LF"`)) + assert.Equal(t, LF, t1) + + var t2 TrailerType + t2.UnmarshalTOML([]byte(`LF`)) + assert.Equal(t, LF, t2) + + var t3 TrailerType + t3.UnmarshalTOML([]byte(`'LF'`)) + assert.Equal(t, LF, t3) + + var t4 TrailerType + t4.UnmarshalTOML([]byte(`"NUL"`)) + assert.Equal(t, NUL, t4) + + var t5 TrailerType + t5.UnmarshalTOML([]byte(`NUL`)) + assert.Equal(t, NUL, t5) + + var t6 TrailerType + t6.UnmarshalTOML([]byte(`'NUL'`)) + assert.Equal(t, NUL, t6) + + var t7 TrailerType + err := t7.UnmarshalTOML([]byte(`wrong`)) + assert.Equal(t, TrailerType(-1), t7) + assert.Error(t, err) +} + func TestUnmarshalLowercase(t *testing.T) { x := &trailerWrapper{} in := []byte(`{"trailer": "lf"}`) diff --git a/rfc5424/machine_test.go b/rfc5424/machine_test.go index 3184e8a..3760e41 100644 --- a/rfc5424/machine_test.go +++ b/rfc5424/machine_test.go @@ -281,6 +281,14 @@ var testCases = []testCase{ version: 1, }, }, + // fixme(leodido) > when space after multi-digit version is missing, the version error handler launches (should not) + // { + // []byte("<3>22"), + // false, + // nil, + // "parsing error [col 6]", + // (&SyslogMessage{}).SetPriority(3).SetVersion(22), + // }, // Invalid, non numeric (also partially) version { []byte("<1>3a"),