Skip to content

Commit

Permalink
GH-26: Added group tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Apr 30, 2019
1 parent e26d215 commit 86ad409
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 15 deletions.
121 changes: 121 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package res

import (
"fmt"
"strings"
)

type group []gpart

type gpart struct {
str string
idx int
}

// parseGroup takes a group name and parses it for ${tag} sequences,
// verifying the tags exists as parameter tags in the pattern as well.
// Panics if an error is encountered.
func parseGroup(g, pattern string) group {
if g == "" {
return nil
}

tokens := splitPattern(pattern)

var gr group
var c byte
l := len(g)
i := 0
start := 0

StateDefault:
if i == l {
if i > start {
gr = append(gr, gpart{str: g[start:i]})
}
return gr
}
if g[i] == '$' {
if i > start {
gr = append(gr, gpart{str: g[start:i]})
}
i++
if i == l {
goto UnexpectedEnd
}
if g[i] != '{' {
panic(fmt.Sprintf("expected character \"{\" at pos %d", i))
}
i++
start = i
goto StateTag
}
i++
goto StateDefault

StateTag:
if i == l {
goto UnexpectedEnd
}
c = g[i]
if c == '}' {
if i == start {
panic(fmt.Sprintf("empty group tag at pos %d", i))
}
tag := "$" + g[start:i]
for j, t := range tokens {
if t == tag {
gr = append(gr, gpart{idx: j})
goto TokenFound
}
}
panic(fmt.Sprintf("group tag %s not found in pattern", tag))
TokenFound:
i++
start = i
goto StateDefault
}
if (c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '_' && c != '-' {
panic(fmt.Sprintf("non alpha-numeric (a-z0-9_-) character in group tag at pos %d", i))
}
i++
goto StateTag

UnexpectedEnd:
panic("unexpected end of group tag")
}

func (g group) toString(rname string) string {
l := len(g)
if l == 0 {
return rname
}

if l == 1 && g[0].str != "" {
return g[0].str
}

var tokens []string
if len(rname) > 0 {
tokens = make([]string, 0, 32)
start := 0
for i := 0; i < len(rname); i++ {
if rname[i] == btsep {
tokens = append(tokens, rname[start:i])
start = i + 1
}
}
tokens = append(tokens, rname[start:])
}

var b strings.Builder
for _, gp := range g {
if gp.str == "" {
b.WriteString(tokens[gp.idx])
} else {
b.WriteString(gp.str)
}
}

return b.String()
}
86 changes: 86 additions & 0 deletions group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package res

import (
"testing"
)

// Test parseGroup panics when expected
func TestParseGroup(t *testing.T) {
tbl := []struct {
Group string
Pattern string
Panic bool
}{
// Valid groups
{"", "test", false},
{"test", "test", false},
{"test", "test.$foo", false},
{"test.${foo}", "test.$foo", false},
{"${foo}", "test.$foo", false},
{"${foo}.test", "test.$foo", false},
{"${foo}${bar}", "test.$foo.$bar", false},
{"${bar}${foo}", "test.$foo.$bar", false},
{"${foo}.${bar}", "test.$foo.$bar.>", false},
{"${foo}${foo}", "test.$foo.$bar", false},

// Invalid groups
{"$", "test.$foo", true},
{"${", "test.$foo", true},
{"${foo", "test.$foo", true},
{"${}", "test.$foo", true},
{"${$foo}", "test.$foo", true},
{"${bar}", "test.$foo", true},
}

for _, l := range tbl {
func() {
defer func() {
if r := recover(); r != nil {
if !l.Panic {
t.Errorf("expected parseGroup not to panic, but it did:\n\tpanic : %s\n\tgroup : %s\n\tpattern : %s", r, l.Group, l.Pattern)
}
} else {
if l.Panic {
t.Errorf("expected parseGroup to panic, but it didn't\n\tgroup : %s\n\tpattern : %s", l.Group, l.Pattern)
}
}
}()

parseGroup(l.Group, l.Pattern)
}()
}
}

// Test group toString
func TestGroupToString(t *testing.T) {
tbl := []struct {
Group string
Pattern string
RName string
Expected string
}{
{"", "test", "test", "test"},
{"test", "test", "test", "test"},
{"foo", "test", "test", "foo"},
{"test", "test.$foo", "test.42", "test"},
{"test.${foo}", "test.$foo", "test.42", "test.42"},
{"bar.${foo}", "test.$foo", "test.42", "bar.42"},
{"${foo}", "test.$foo", "test.42", "42"},
{"${foo}.test", "test.$foo", "test.42", "42.test"},
{"${foo}${bar}", "test.$foo.$bar", "test.42.baz", "42baz"},
{"${bar}${foo}", "test.$foo.$bar", "test.42.baz", "baz42"},
{"${foo}.${bar}", "test.$foo.$bar.>", "test.42.baz.extra.all", "42.baz"},
{"${foo}${foo}", "test.$foo.$bar", "test.42.baz", "4242"},
{"${foo}.test.this.${bar}", "test.$foo.$bar", "test.42.baz", "42.test.this.baz"},
}

for _, l := range tbl {
func() {
gr := parseGroup(l.Group, l.Pattern)
wid := gr.toString(l.RName)
if wid != l.Expected {
t.Errorf("expected parseGroup(%#v, %#v).toString(%#v) to return:\n\t%#v\nbut got:\n\t%#v", l.Group, l.Pattern, l.RName, l.Expected, wid)
}
}()
}
}
30 changes: 18 additions & 12 deletions patterns.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,8 @@ type nodeMatch struct {
// add inserts new handlers to the pattern store.
// An invalid pattern, or a pattern already registered will cause panic.
func (ls *patterns) add(pattern string, hs *regHandler) {
var tokens []string
if len(pattern) > 0 {
tokens = make([]string, 0, 32)
start := 0
for i := 0; i < len(pattern); i++ {
if pattern[i] == btsep {
tokens = append(tokens, pattern[start:i])
start = i + 1
}
}
tokens = append(tokens, pattern[start:])
}
tokens := splitPattern(pattern)

var params []pathParam

l := ls.root
Expand Down Expand Up @@ -192,3 +182,19 @@ func matchNode(l *node, toks []string, i int, m *nodeMatch) bool {

return false
}

func splitPattern(p string) []string {
if len(p) == 0 {
return nil
}
tokens := make([]string, 0, 32)
start := 0
for i := 0; i < len(p); i++ {
if p[i] == btsep {
tokens = append(tokens, p[start:i])
start = i + 1
}
}
tokens = append(tokens, p[start:])
return tokens
}
21 changes: 18 additions & 3 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,16 @@ type Handler struct {
// Group is the identifier of the group the resource belongs to.
// All resources of the same group will be handled on the same
// goroutine.
// The group may contain tags, ${tagName}, where the tag name matches
// a parameter placeholder name in the resource pattern.
// If empty, the resource name will be used as identifier.
Group string
}

type regHandler struct {
Handler
typ rtype
group group
typ rtype
}

const (
Expand Down Expand Up @@ -206,8 +209,10 @@ func (s *Service) AddHandler(pattern string, hs Handler) {
if hs.Access != nil {
s.withAccess = true
}

h := regHandler{
Handler: hs,
group: parseGroup(hs.Group, pattern),
typ: validateGetHandlers(hs),
}
s.patterns.add(s.Name+"."+pattern, &h)
Expand Down Expand Up @@ -295,6 +300,16 @@ func Auth(method string, h AuthHandler) HandlerOption {
}
}

// Group sets a group ID. All resources of the same group will be handled
// on the same goroutine.
// The group may contain tags, ${tagName}, where the tag name matches
// a parameter placeholder name in the resource pattern.
func Group(group string) HandlerOption {
return func(hs *Handler) {
hs.Group = group
}
}

// SetReset sets the patterns used for resources and access when a reset is made.¨
// For more details on system reset, see:
// https://github.com/jirenius/resgate/blob/master/docs/res-service-protocol.md#system-reset-event
Expand Down Expand Up @@ -558,8 +573,8 @@ func (s *Service) runWith(hs *regHandler, rname string, cb func()) {
}

wid := rname
if hs != nil && hs.Group != "" {
wid = hs.Group
if hs != nil {
wid = hs.group.toString(rname)
}

s.mu.Lock()
Expand Down

0 comments on commit 86ad409

Please sign in to comment.