Skip to content

Commit

Permalink
Merge pull request #12 from mailgun/cweid
Browse files Browse the repository at this point in the history
[PIP-1836] - Create new endpoint in temple to return template variables detected from provided template
  • Loading branch information
Conrad Weidenkeller authored Oct 19, 2022
2 parents 6e8e156 + 3a85ad1 commit aa083fa
Show file tree
Hide file tree
Showing 7 changed files with 715 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/
*.swp
.vscode/
__pycache__
*.pyc
Expand Down
165 changes: 165 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package raymond

import (
"strings"
)

type contextMember struct {
path string
asMapping string
}

type handlebarsContext struct {
contextMembers []contextMember
}

func newHandlebarsContext() *handlebarsContext {
var cm []contextMember
return &handlebarsContext{contextMembers: cm}
}

func (h *handlebarsContext) AddMemberContext(path, asMapping string) {
cmp := contextMember{path: path, asMapping: asMapping}
h.contextMembers = append(h.contextMembers, cmp)
}

func (h *handlebarsContext) GetCurrentContext() []string {
return h.GetParentContext(0)
}

func (h *handlebarsContext) GetCurrentContextString() string {
return h.GetParentContextString(0)
}

func (h *handlebarsContext) GetParentContext(num_ancestors int) []string {
if len(h.contextMembers) == 0 {
return []string{}
}
return strings.Split(h.GetParentContextString(num_ancestors), ".")
}

func (h *handlebarsContext) GetParentContextString(num_ancestors int) string {
if len(h.contextMembers) == 0 {
return ""
}
if num_ancestors > len(h.contextMembers) {
num_ancestors = 0
}
var res string
for _, val := range h.contextMembers[:len(h.contextMembers)-num_ancestors] {
if len(res) == 0 {
res = val.path
} else {
res = res + "." + val.path
}
}
return res
}

func (h *handlebarsContext) MoveUpContext() {
if len(h.contextMembers) > 0 {
h.contextMembers = h.contextMembers[:len(h.contextMembers)-1]
}
}

func (h *handlebarsContext) HaveAsContexts(num_ancestors int) bool {
if num_ancestors > len(h.contextMembers) {
num_ancestors = 0
}
for val := range h.contextMembers[:len(h.contextMembers)-num_ancestors] {
if h.contextMembers[val].asMapping != "" {
return true
}
}
return false
}

func (h *handlebarsContext) GetMappedContext(path []string, num_ancestors int) []string {
if len(path) == 0 {
return []string{}
}
return strings.Split(h.GetMappedContextString(path, num_ancestors), ".")
}

func (h *handlebarsContext) GetMappedContextString(path []string, num_ancestors int) string {
if len(h.contextMembers) == 0 {
return strings.Join(path, ".")
}
if num_ancestors > len(h.contextMembers) {
num_ancestors = 0
}
if !h.HaveAsContexts(num_ancestors) {
var res string
if path[0] == "" {
res = h.GetParentContextString(num_ancestors)
} else {
res = h.GetParentContextString(num_ancestors) + "." + strings.Join(path, ".")
}
return strings.Trim(res, ".")
}
var res string
copiedMembers := make([]contextMember, 0)
if num_ancestors > 0 {
copiedMembers = append(copiedMembers, h.contextMembers[:len(h.contextMembers)-num_ancestors]...)
} else {
copiedMembers = append(copiedMembers, h.contextMembers...)
}
for p := len(path) - 1; p >= 0; p-- {
var val contextMember
var found string
if len(copiedMembers) == 0 {
found = path[p]
} else {
val = copiedMembers[len(copiedMembers)-1]
if val.asMapping == path[p] {
found = val.path
if len(copiedMembers) > 1 {
val2 := copiedMembers[len(copiedMembers)-2]
tmp := strings.Split(val.path, ".")
if tmp[0] == val2.asMapping {
found = strings.Join(tmp[1:], ".")
}
}
copiedMembers = copiedMembers[:len(copiedMembers)-1]
} else {
if len(val.asMapping) == 0 {
found = val.path + "." + path[p]
copiedMembers = copiedMembers[:len(copiedMembers)-1]
} else {
if len(copiedMembers) == 0 {
ss := strings.Split(val.asMapping, ".")
if ss[0] == path[p] {
found = val.path
} else {
}
} else {
if len(copiedMembers) > 1 {
cv := copiedMembers[len(copiedMembers)-2]
mappedPath := strings.Split(cv.path, ".")
if len(mappedPath) > 1 {
tmp := strings.Join(mappedPath[1:], ".")
if tmp == val.asMapping {
found = val.path
copiedMembers = copiedMembers[:len(copiedMembers)-1]
} else {
found = path[p]
}
} else {
found = path[p]
}
} else {
found = path[p]
}
}
}
}
}
res = found + "." + res
}
if len(copiedMembers) > 0 {
for p := len(copiedMembers) - 1; p >= 0; p-- {
res = copiedMembers[p].path + "." + res
}
}
return strings.Trim(res, ".")
}
64 changes: 64 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package raymond

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func TestHandlebarsContext(t *testing.T) {
suite.Run(t, new(HandlebarsContextSuite))
}

type HandlebarsContextSuite struct {
suite.Suite
c *handlebarsContext
}

func (s *HandlebarsContextSuite) SetupTest() {
s.c = newHandlebarsContext()
}

func (s *HandlebarsContextSuite) TestHandlebarsContextAddMemberContext() {
assert.Equal(s.T(), 0, len(s.c.GetCurrentContext()), "Len expected to be zero.")
s.c.AddMemberContext("foo", "bar")
assert.Equal(s.T(), 1, len(s.c.GetCurrentContext()), "Len expected to be one.")
s.c.AddMemberContext("baz", "bar")
assert.Equal(s.T(), 2, len(s.c.GetCurrentContext()), "Len expected to be two.")
s.c.AddMemberContext("bean", "bar")
assert.Equal(s.T(), 3, len(s.c.GetCurrentContext()), "Len expected to be three.")
assert.Equal(s.T(), "foo.baz.bean", s.c.GetCurrentContextString(), "Should be all three scopes.")
assert.Equal(s.T(), 2, len(s.c.GetParentContext(1)), "Len expected to be two.")
assert.Equal(s.T(), "foo.baz", s.c.GetParentContextString(1), "Should be two scopes.")
assert.Equal(s.T(), "", s.c.GetParentContextString(3), "Should be empty string.")
assert.Equal(s.T(), "foo.baz.bean", s.c.GetParentContextString(4), "Parent context exceeded use default ancestor.")
assert.Equal(s.T(), 3, len(s.c.GetCurrentContext()), "Len expected to be three.")
s.c.MoveUpContext()
assert.Equal(s.T(), 2, len(s.c.GetCurrentContext()), "Len expected to be two.")
assert.Equal(s.T(), "foo.baz", s.c.GetCurrentContextString(), "Should be two scopes.")
}

func (s *HandlebarsContextSuite) TestHandlebarsContextMappedContextAllTheSameMapping() {
assert.Equal(s.T(), 0, len(s.c.GetCurrentContext()), "Len expected to be zero.")
s.c.AddMemberContext("foo", "bar")
s.c.AddMemberContext("baz", "bar")
s.c.AddMemberContext("bean", "bar")
assert.Equal(s.T(), "foo.blah.baz.bing.bean.bong", s.c.GetMappedContextString([]string{"bar", "blah", "bar", "bing", "bar", "bong"}, 0), "Should be all three scopes.")
}

func (s *HandlebarsContextSuite) TestHandlebarsContextMappedContextLongNamesSameMapping() {
assert.Equal(s.T(), 0, len(s.c.GetCurrentContext()), "Len expected to be zero.")
s.c.AddMemberContext("foo.foo.foo", "bar")
s.c.AddMemberContext("baz.baz.baz", "bar")
s.c.AddMemberContext("bean.bean.bean", "bar")
assert.Equal(s.T(), "foo.foo.foo.baz.baz.baz.bean.bean.bean", s.c.GetMappedContextString([]string{"bar", "bar", "bar"}, 0), "Should be all three scopes.")
}

func (s *HandlebarsContextSuite) TestHandlebarsContextMappedContextLongNamesSameMappingNoMapping() {
assert.Equal(s.T(), 0, len(s.c.GetCurrentContext()), "Len expected to be zero.")
s.c.AddMemberContext("foo.foo.foo", "bar")
s.c.AddMemberContext("baz.baz.baz", "")
s.c.AddMemberContext("bean.bean.bean", "bar")
assert.Equal(s.T(), "foo.foo.foo.baz.baz.baz.bleep.bean.bean.bean.bop", s.c.GetMappedContextString([]string{"bar", "bleep", "bar", "bop"}, 0), "Should be all three scopes.")
}
Loading

0 comments on commit aa083fa

Please sign in to comment.