Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
6acc73c4
Commit
6acc73c4
authored
2 months ago
by
Semyon Krestyaninov
Committed by
Pavel Antonov
2 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Исправлена ошибка с потерей контекста при использовании шаблонных функций в template.Builder
parent
2c47720b
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
template/builder.go
+9
-4
9 additions, 4 deletions
template/builder.go
template/builder_test.go
+35
-0
35 additions, 0 deletions
template/builder_test.go
template/funcs.go
+1
-1
1 addition, 1 deletion
template/funcs.go
template/system.go
+3
-2
3 additions, 2 deletions
template/system.go
with
48 additions
and
7 deletions
template/builder.go
+
9
−
4
View file @
6acc73c4
...
@@ -56,10 +56,7 @@ func NewBuilder(conf *BuilderConfig) *Builder {
...
@@ -56,10 +56,7 @@ func NewBuilder(conf *BuilderConfig) *Builder {
ctx
:
context
.
Background
(),
ctx
:
context
.
Background
(),
data
:
make
(
map
[
string
]
any
),
data
:
make
(
map
[
string
]
any
),
}
}
builder
.
funcMap
=
map
[
string
]
any
{
setFuncMap
(
builder
)
"lookup"
:
getLookup
(
builder
),
"system"
:
getSystem
(
builder
),
}
return
builder
return
builder
}
}
...
@@ -83,6 +80,7 @@ func (b *Builder) Context() context.Context {
...
@@ -83,6 +80,7 @@ func (b *Builder) Context() context.Context {
func
(
b
*
Builder
)
WithContext
(
ctx
context
.
Context
)
*
Builder
{
func
(
b
*
Builder
)
WithContext
(
ctx
context
.
Context
)
*
Builder
{
clone
:=
*
b
clone
:=
*
b
clone
.
ctx
=
ctx
clone
.
ctx
=
ctx
setFuncMap
(
&
clone
)
return
&
clone
return
&
clone
}
}
...
@@ -145,3 +143,10 @@ func (b *Builder) ExecuteMap(patternMap map[string]any, data ...any) (map[string
...
@@ -145,3 +143,10 @@ func (b *Builder) ExecuteMap(patternMap map[string]any, data ...any) (map[string
}
}
return
b
.
TextTemplate
()
.
ExecuteMap
(
patternMap
,
data
...
)
return
b
.
TextTemplate
()
.
ExecuteMap
(
patternMap
,
data
...
)
}
}
func
setFuncMap
(
b
*
Builder
)
{
b
.
funcMap
=
map
[
string
]
any
{
"lookup"
:
getLookup
(
b
),
"system"
:
getSystem
(
b
),
}
}
This diff is collapsed.
Click to expand it.
template/builder_test.go
+
35
−
0
View file @
6acc73c4
package
template
package
template
import
(
import
(
"context"
"testing"
"testing"
"git.perx.ru/perxis/perxis-go/pkg/collections"
"git.perx.ru/perxis/perxis-go/pkg/collections"
...
@@ -17,6 +18,8 @@ import (
...
@@ -17,6 +18,8 @@ import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/require"
)
)
type
testCtxKey
struct
{}
func
TestBuilder
(
t
*
testing
.
T
)
{
func
TestBuilder
(
t
*
testing
.
T
)
{
t
.
Run
(
"using default HTML template"
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
"using default HTML template"
,
func
(
t
*
testing
.
T
)
{
builder
:=
NewBuilder
(
&
BuilderConfig
{
builder
:=
NewBuilder
(
&
BuilderConfig
{
...
@@ -36,6 +39,7 @@ func TestBuilder_Funcs(t *testing.T) {
...
@@ -36,6 +39,7 @@ func TestBuilder_Funcs(t *testing.T) {
name
string
name
string
pattern
string
pattern
string
setupContent
func
(
t
*
testing
.
T
)
*
content
.
Content
setupContent
func
(
t
*
testing
.
T
)
*
content
.
Content
with
func
(
b
*
Builder
)
*
Builder
spaceID
string
spaceID
string
environmentID
string
environmentID
string
collectionID
string
collectionID
string
...
@@ -177,6 +181,34 @@ func TestBuilder_Funcs(t *testing.T) {
...
@@ -177,6 +181,34 @@ func TestBuilder_Funcs(t *testing.T) {
want
:
"coll_id"
,
want
:
"coll_id"
,
assertError
:
assert
.
NoError
,
assertError
:
assert
.
NoError
,
},
},
{
name
:
"keep builder context"
,
pattern
:
"{{ lookup `coll_id.item_id.field` }}"
,
setupContent
:
func
(
t
*
testing
.
T
)
*
content
.
Content
{
itemsService
:=
itemsmocks
.
NewItems
(
t
)
itemsService
.
On
(
"Get"
,
mock
.
Anything
,
"space_id"
,
"env_id"
,
"coll_id"
,
"item_id"
)
.
Return
(
&
items
.
Item
{
Data
:
map
[
string
]
any
{
"field"
:
"value"
,
},
},
nil
)
.
Run
(
func
(
args
mock
.
Arguments
)
{
ctx
:=
args
.
Get
(
0
)
.
(
context
.
Context
)
assert
.
Equal
(
t
,
"bar"
,
ctx
.
Value
(
testCtxKey
{}))
})
.
Once
()
return
&
content
.
Content
{
Items
:
itemsService
,
}
},
with
:
func
(
b
*
Builder
)
*
Builder
{
return
b
.
WithContext
(
context
.
WithValue
(
context
.
Background
(),
testCtxKey
{},
"bar"
))
},
spaceID
:
"space_id"
,
environmentID
:
"env_id"
,
want
:
"value"
,
assertError
:
assert
.
NoError
,
},
}
{
}
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing
.
T
)
{
conf
:=
&
BuilderConfig
{
conf
:=
&
BuilderConfig
{
...
@@ -188,6 +220,9 @@ func TestBuilder_Funcs(t *testing.T) {
...
@@ -188,6 +220,9 @@ func TestBuilder_Funcs(t *testing.T) {
conf
.
Content
=
tc
.
setupContent
(
t
)
conf
.
Content
=
tc
.
setupContent
(
t
)
}
}
builder
:=
NewBuilder
(
conf
)
builder
:=
NewBuilder
(
conf
)
if
tc
.
with
!=
nil
{
builder
=
tc
.
with
(
builder
)
}
got
,
err
:=
builder
.
TextTemplate
()
.
Execute
(
tc
.
pattern
)
got
,
err
:=
builder
.
TextTemplate
()
.
Execute
(
tc
.
pattern
)
tc
.
assertError
(
t
,
err
)
tc
.
assertError
(
t
,
err
)
assert
.
Equal
(
t
,
tc
.
want
,
got
)
assert
.
Equal
(
t
,
tc
.
want
,
got
)
...
...
This diff is collapsed.
Click to expand it.
template/funcs.go
+
1
−
1
View file @
6acc73c4
...
@@ -21,7 +21,7 @@ func getLookup(b *Builder) func(string) (any, error) {
...
@@ -21,7 +21,7 @@ func getLookup(b *Builder) func(string) (any, error) {
field
:=
parsedName
[
2
]
field
:=
parsedName
[
2
]
item
,
err
:=
b
.
Content
()
.
Items
.
Get
(
b
.
Context
(),
b
.
SpaceID
(),
b
.
EnvironmentID
(),
collectionID
,
itemID
)
item
,
err
:=
b
.
Content
()
.
Items
.
Get
(
b
.
Context
(),
b
.
SpaceID
(),
b
.
EnvironmentID
(),
collectionID
,
itemID
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
errors
.
Wrapf
(
err
,
"
failed to get
\"
%s
\"
"
)
return
nil
,
errors
.
Wrapf
(
err
,
"
get item %q"
,
itemID
)
}
}
if
len
(
item
.
Data
)
>
0
{
if
len
(
item
.
Data
)
>
0
{
...
...
This diff is collapsed.
Click to expand it.
template/system.go
+
3
−
2
View file @
6acc73c4
...
@@ -29,7 +29,8 @@ func (s *System) Environment() (*environments.Environment, error) {
...
@@ -29,7 +29,8 @@ func (s *System) Environment() (*environments.Environment, error) {
return
s
.
builder
.
environment
,
nil
return
s
.
builder
.
environment
,
nil
}
}
env
,
err
:=
s
.
builder
.
Content
()
.
Environments
.
Get
(
s
.
builder
.
ctx
,
s
.
builder
.
SpaceID
(),
s
.
builder
.
EnvironmentID
())
env
,
err
:=
s
.
builder
.
Content
()
.
Environments
.
Get
(
s
.
builder
.
Context
(),
s
.
builder
.
SpaceID
(),
s
.
builder
.
EnvironmentID
())
s
.
builder
.
environment
=
env
s
.
builder
.
environment
=
env
return
s
.
builder
.
environment
,
err
return
s
.
builder
.
environment
,
err
}
}
...
@@ -43,7 +44,7 @@ func (s *System) Collection() (*collections.Collection, error) {
...
@@ -43,7 +44,7 @@ func (s *System) Collection() (*collections.Collection, error) {
return
s
.
builder
.
collection
,
nil
return
s
.
builder
.
collection
,
nil
}
}
coll
,
err
:=
s
.
builder
.
Content
()
.
Collections
.
Get
(
s
.
builder
.
ctx
,
s
.
builder
.
SpaceID
(),
coll
,
err
:=
s
.
builder
.
Content
()
.
Collections
.
Get
(
s
.
builder
.
Context
()
,
s
.
builder
.
SpaceID
(),
s
.
builder
.
EnvironmentID
(),
s
.
builder
.
CollectionID
())
s
.
builder
.
EnvironmentID
(),
s
.
builder
.
CollectionID
())
s
.
builder
.
collection
=
coll
s
.
builder
.
collection
=
coll
return
s
.
builder
.
collection
,
err
return
s
.
builder
.
collection
,
err
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment