Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support multiple roles on blocks #807

Merged
merged 1 commit into from
Dec 16, 2020
Merged
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
5 changes: 5 additions & 0 deletions pkg/parser/attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ var _ = DescribeTable("valid block attributes",
types.AttrPositional2: `go.not_a_role`,
},
),
Entry("multiple roles", "[.role1]\n[.role2]",
types.Attributes{
types.AttrRoles: []interface{}{`role1`, `role2`},
},
),

// option shorthand
Entry(`[%hardbreaks]`, `[%hardbreaks]`,
Expand Down
32 changes: 31 additions & 1 deletion pkg/parser/paragraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ baz`
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("with paragraph roles and attribute", func() {
It("with paragraph roles and attribute - case 1", func() {
source := `[.role1%hardbreaks.role2]
foo
bar
Expand Down Expand Up @@ -326,6 +326,36 @@ baz`
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("with paragraph roles - case 2", func() {
source := `[.role1%hardbreaks]
[.role2]
foo
bar
baz`
expected := types.DraftDocument{
Elements: []interface{}{
types.Paragraph{
Attributes: types.Attributes{
types.AttrOptions: []interface{}{"hardbreaks"},
types.AttrRoles: []interface{}{"role1", "role2"},
},
Lines: [][]interface{}{
{
types.StringElement{Content: "foo"},
},
{
types.StringElement{Content: "bar"},
},
{
types.StringElement{Content: "baz"},
},
},
},
},
}
Expect(ParseDraftDocument(source)).To(MatchDraftDocument(expected))
})

It("not treat plusplus as line break", func() {
source := `C++
foo`
Expand Down
23 changes: 22 additions & 1 deletion pkg/types/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ func NewAttributes(attributes ...interface{}) (Attributes, error) {
return nil, fmt.Errorf("unexpected type of attribute: '%[1]T' (%[1]v)", attr)
}
}
if log.IsLevelEnabled(log.DebugLevel) {
log.Debug("initialized attributes:")
spew.Fdump(log.StandardLogger().Out, result)
}
return result, nil
}

Expand Down Expand Up @@ -539,10 +543,22 @@ func (a Attributes) Set(key string, value interface{}) Attributes {
switch key {
case AttrRole:
if roles, ok := a[AttrRoles].([]interface{}); ok {
log.Debugf("appending role to existin one(s): %v", value)
a[AttrRoles] = append(roles, value)
} else {
log.Debugf("setting first role: %v (%T)", value, a[AttrRoles])
a[AttrRoles] = []interface{}{value}
}
case AttrRoles:
if r, ok := value.([]interface{}); ok { // value should be an []interface{}
if roles, ok := a[AttrRoles].([]interface{}); ok {
log.Debugf("appending role to existin one(s): %v", value)
a[AttrRoles] = append(roles, r...)
} else {
log.Debugf("setting first role: %v (%T)", value, a[AttrRoles])
a[AttrRoles] = r
}
}
case AttrOption:
if options, ok := a[AttrOptions].([]interface{}); ok {
a[AttrOptions] = append(options, value)
Expand All @@ -552,11 +568,16 @@ func (a Attributes) Set(key string, value interface{}) Attributes {
default:
a[key] = value
}
if log.IsLevelEnabled(log.DebugLevel) {
spew.Fdump(log.StandardLogger().Out, a)

}
return a
}

// SetAll adds the given attributes to the current ones
func (a Attributes) SetAll(attr interface{}) Attributes {
log.Debug("setting attributes")
switch attr := attr.(type) {
case Attribute:
if a == nil {
Expand All @@ -571,7 +592,7 @@ func (a Attributes) SetAll(attr interface{}) Attributes {
a = Attributes{}
}
for k, v := range attr {
a[k] = v
a.Set(k, v)
}
case []interface{}:
for i := range attr {
Expand Down