-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Merge template functions "dict/Dict/mergeinto" #23932
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0ccc3ae
fix
wxiaoguang e64f343
Update modules/templates/util.go
wxiaoguang 4e6f99f
Merge branch 'main' into refactor-dict
KN4CK3R 5f6cb3b
Merge branch 'main' into refactor-dict
wxiaoguang 3783d53
Merge branch 'main' into refactor-dict
wxiaoguang bd37277
fix merge
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright 2023 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package templates | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "reflect" | ||
| ) | ||
|
|
||
| func dictMerge(base map[string]any, arg any) bool { | ||
| if arg == nil { | ||
| return true | ||
| } | ||
| rv := reflect.ValueOf(arg) | ||
| if rv.Kind() == reflect.Map { | ||
| for _, k := range rv.MapKeys() { | ||
| base[k.String()] = rv.MapIndex(k).Interface() | ||
| } | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // dict is a helper function for creating a map[string]any from a list of key-value pairs. | ||
| // If the key is dot ".", the value is merged into the base map, just like Golang template's dot syntax: dot means current | ||
| // The dot syntax is highly discouraged, because it might case unclear key conflicts. It's always good use explicit keys. | ||
| func dict(args ...any) (map[string]any, error) { | ||
| if len(args)%2 != 0 { | ||
| return nil, fmt.Errorf("invalid dict constructor syntax: must have key-value pairs") | ||
| } | ||
| m := make(map[string]any, len(args)/2) | ||
| for i := 0; i < len(args); i += 2 { | ||
| key, ok := args[i].(string) | ||
| if !ok { | ||
| return nil, fmt.Errorf("invalid dict constructor syntax: unable to merge args[%d]", i) | ||
| } | ||
| if key == "." { | ||
| if ok = dictMerge(m, args[i+1]); !ok { | ||
| return nil, fmt.Errorf("invalid dict constructor syntax: dot arg[%d] must be followed by a dict", i) | ||
| } | ||
| } else { | ||
| m[key] = args[i+1] | ||
| } | ||
| } | ||
| return m, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright 2023 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package templates | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestDict(t *testing.T) { | ||
| type M map[string]any | ||
| cases := []struct { | ||
| args []any | ||
| want map[string]any | ||
| }{ | ||
| {[]any{"a", 1, "b", 2}, M{"a": 1, "b": 2}}, | ||
| {[]any{".", M{"base": 1}, "b", 2}, M{"base": 1, "b": 2}}, | ||
| {[]any{"a", 1, ".", M{"extra": 2}}, M{"a": 1, "extra": 2}}, | ||
| {[]any{"a", 1, ".", map[string]int{"int": 2}}, M{"a": 1, "int": 2}}, | ||
| {[]any{".", nil, "b", 2}, M{"b": 2}}, | ||
| } | ||
|
|
||
| for _, c := range cases { | ||
| got, err := dict(c.args...) | ||
| if assert.NoError(t, err) { | ||
| assert.EqualValues(t, c.want, got) | ||
| } | ||
| } | ||
|
|
||
| bads := []struct { | ||
| args []any | ||
| }{ | ||
| {[]any{"a", 1, "b"}}, | ||
| {[]any{1}}, | ||
| {[]any{struct{}{}}}, | ||
| } | ||
| for _, c := range bads { | ||
| _, err := dict(c.args...) | ||
| assert.Error(t, err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.