Skip to content

Commit 684ec27

Browse files
authored
Merge pull request #7 from breml/fix-hashentry-name
Fix hashentry name
2 parents 4605d71 + f9cd348 commit 684ec27

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

ast/ast.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,15 +549,24 @@ func (ha HashAttribute) Value() []HashEntry {
549549
return ha.value
550550
}
551551

552+
type HashEntryKey interface {
553+
ValueString() string
554+
attributeNode()
555+
hashEntryKeyAttribute()
556+
}
557+
558+
func (NumberAttribute) hashEntryKeyAttribute() {}
559+
func (StringAttribute) hashEntryKeyAttribute() {}
560+
552561
// A HashEntry node defines a hash entry within a hash attribute.
553562
type HashEntry struct {
554-
name string
563+
name HashEntryKey
555564
value Attribute
556565
Comment CommentBlock
557566
}
558567

559568
// NewHashEntry creates a new hash entry for a hash attribute.
560-
func NewHashEntry(name string, value Attribute) HashEntry {
569+
func NewHashEntry(name HashEntryKey, value Attribute) HashEntry {
561570
return HashEntry{
562571
name: name,
563572
value: value,
@@ -566,7 +575,7 @@ func NewHashEntry(name string, value Attribute) HashEntry {
566575

567576
// Name returns the name of the attribute.
568577
func (he HashEntry) Name() string {
569-
return he.name
578+
return he.name.ValueString()
570579
}
571580

572581
// String returns a string representation of a hash entry.

ast/ast_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ func TestAst(t *testing.T) {
2727
),
2828
NewHashAttribute(
2929
"add_field",
30-
NewHashEntry("fieldname", NewStringAttribute("", "fieldvalue", DoubleQuoted)),
31-
NewHashEntry("number", NewNumberAttribute("", 3.1415)),
30+
NewHashEntry(NewStringAttribute("", "bareword", Bareword), NewStringAttribute("", "bareword", Bareword)),
31+
NewHashEntry(NewStringAttribute("", "single quoted", SingleQuoted), NewStringAttribute("", "single quoted", SingleQuoted)),
32+
NewHashEntry(NewStringAttribute("", "double quoted", DoubleQuoted), NewStringAttribute("", "double quoted", DoubleQuoted)),
33+
NewHashEntry(NewNumberAttribute("", 1), NewNumberAttribute("", 3.1415)),
3234
),
3335
NewNumberAttribute("pi", 3.1415),
3436
NewPluginAttribute("codec", NewPlugin("rubydebug", NewStringAttribute("string", "a value", DoubleQuoted))),
@@ -49,8 +51,10 @@ func TestAst(t *testing.T) {
4951
tag3
5052
]
5153
add_field => {
52-
fieldname => "fieldvalue"
53-
number => 3.1415
54+
bareword => bareword
55+
'single quoted' => 'single quoted'
56+
"double quoted" => "double quoted"
57+
1 => 3.1415
5458
}
5559
pi => 3.1415
5660
codec => rubydebug {
@@ -531,7 +535,7 @@ output {
531535
nil,
532536
NewStringAttribute("foo", "bar", Bareword),
533537
NewArrayAttribute("nil", nil),
534-
NewHashAttribute("nilHash", NewHashEntry("nilEntry", nil)),
538+
NewHashAttribute("nilHash", NewHashEntry(NewStringAttribute("", "nilEntry", Bareword), nil)),
535539
nil,
536540
),
537541
NewBranch(

logstash_config_helper.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,9 @@ func hashentries(attribute, attributes1 interface{}) ([]ast.HashEntry, error) {
335335
}
336336

337337
func hashentry(name, value, comment interface{}) (ast.HashEntry, error) {
338-
var key ast.StringAttribute
339-
340-
switch name := name.(type) {
341-
case ast.StringAttribute:
342-
key = name
343-
}
338+
key := name.(ast.HashEntryKey)
344339

345-
he := ast.NewHashEntry(key.ValueString(), value.(ast.Attribute))
340+
he := ast.NewHashEntry(key, value.(ast.Attribute))
346341
he.Comment = commentBlock(comment, true, false)
347342

348343
return he, nil

0 commit comments

Comments
 (0)