Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
New: UnmarshalTOML for trailer type
Browse files Browse the repository at this point in the history
  • Loading branch information
leodido committed Dec 18, 2018
1 parent b7833e8 commit 0cd00a9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
18 changes: 16 additions & 2 deletions nontransparent/trailer_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
31 changes: 31 additions & 0 deletions nontransparent/trailer_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`)
Expand Down
8 changes: 8 additions & 0 deletions rfc5424/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 0cd00a9

Please sign in to comment.