1
0
Fork 0
forked from forgejo/forgejo

Merge template functions "dict/Dict/mergeinto" (#23932)

One of the steps in #23328


Before there were 3 different but similar functions: dict/Dict/mergeinto

The code was just copied & pasted, no test.

This PR defines a new stable `dict` function, it covers all the 3 old
functions behaviors, only +160 -171


Future developers do not need to think about or guess the different dict
functions, just use one: `dict`

Why use `dict` but not `Dict`? Because there are far more `dict` than
`Dict` in code already ......
This commit is contained in:
wxiaoguang 2023-04-07 22:39:08 +08:00 committed by GitHub
parent 5b89670a31
commit 36c0840cf1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 162 additions and 178 deletions

View file

@ -6,7 +6,6 @@ package util
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"math/big"
"strconv"
@ -117,29 +116,6 @@ func NormalizeEOL(input []byte) []byte {
return tmp[:pos]
}
// MergeInto merges pairs of values into a "dict"
func MergeInto(dict map[string]interface{}, values ...interface{}) (map[string]interface{}, error) {
for i := 0; i < len(values); i++ {
switch key := values[i].(type) {
case string:
i++
if i == len(values) {
return nil, errors.New("specify the key for non array values")
}
dict[key] = values[i]
case map[string]interface{}:
m := values[i].(map[string]interface{})
for i, v := range m {
dict[i] = v
}
default:
return nil, errors.New("dict values must be maps")
}
}
return dict, nil
}
// CryptoRandomInt returns a crypto random integer between 0 and limit, inclusive
func CryptoRandomInt(limit int64) (int64, error) {
rInt, err := rand.Int(rand.Reader, big.NewInt(limit))