snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
This commit is contained in:
158
server/pkg/payload/util.go
Normal file
158
server/pkg/payload/util.go
Normal file
@@ -0,0 +1,158 @@
|
||||
package payload
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/pkg/filter"
|
||||
"github.com/cortezaproject/corteza/server/pkg/handle"
|
||||
"github.com/jmoiron/sqlx/types"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
func Uint64toa(i uint64) string {
|
||||
return strconv.FormatUint(i, 10)
|
||||
}
|
||||
|
||||
func Uint64stoa(uu []uint64) []string {
|
||||
ss := make([]string, len(uu))
|
||||
for i, u := range uu {
|
||||
ss[i] = Uint64toa(u)
|
||||
}
|
||||
|
||||
return ss
|
||||
}
|
||||
|
||||
// ParseUint64 parses an string to uint64
|
||||
func ParseUint64(s string) uint64 {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
i, _ := strconv.ParseUint(s, 10, 64)
|
||||
return i
|
||||
}
|
||||
|
||||
// ParseUint64s parses a slice of strings into a slice of uint64s
|
||||
func ParseUint64s(ss []string) []uint64 {
|
||||
uu := make([]uint64, len(ss))
|
||||
for i, s := range ss {
|
||||
uu[i] = ParseUint64(s)
|
||||
}
|
||||
|
||||
return uu
|
||||
}
|
||||
|
||||
func ParseJSONTextWithErr(s string) (types.JSONText, error) {
|
||||
result := &types.JSONText{}
|
||||
err := fmt.Errorf("error parsing JSONText: %w", result.Scan(s))
|
||||
return *result, err
|
||||
}
|
||||
|
||||
func ParseISODateWithErr(s string) (time.Time, error) {
|
||||
return time.Parse(time.RFC3339, s)
|
||||
}
|
||||
|
||||
func ParseISODatePtrWithErr(s string) (*time.Time, error) {
|
||||
t, err := ParseISODateWithErr(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
// ParseInt parses a string to int
|
||||
func ParseInt(s string) int {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
i, _ := strconv.Atoi(s)
|
||||
return i
|
||||
}
|
||||
|
||||
// ParseUInt parses a string to uint64
|
||||
func ParseUint(s string) uint {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
i, _ := strconv.ParseUint(s, 10, 32)
|
||||
return uint(i)
|
||||
}
|
||||
|
||||
// ParseUint64s parses a slice of strings into a slice of uint64s
|
||||
func ParseUints(ss []string) []uint {
|
||||
uu := make([]uint, len(ss))
|
||||
for i, s := range ss {
|
||||
uu[i] = ParseUint(s)
|
||||
}
|
||||
|
||||
return uu
|
||||
}
|
||||
|
||||
// ParseInt64 parses a string to int64
|
||||
func ParseInt64(s string) int64 {
|
||||
if s == "" {
|
||||
return 0
|
||||
}
|
||||
i, _ := strconv.ParseInt(s, 10, 64)
|
||||
return i
|
||||
}
|
||||
|
||||
// parseUInt64 parses a string to uint64
|
||||
func ParseBool(s string) bool {
|
||||
return cast.ToBool(s)
|
||||
}
|
||||
|
||||
func ParseFilterState(s string) filter.State {
|
||||
return filter.State(cast.ToUint(s))
|
||||
}
|
||||
|
||||
// similar to labels.ParseStrings but for map[string]any
|
||||
func ParseMeta(ss []string) (m map[string]any, err error) {
|
||||
if len(ss) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
m = make(map[string]any)
|
||||
|
||||
for _, s := range ss {
|
||||
if strings.HasPrefix(s, "{") && strings.HasSuffix(s, "}") {
|
||||
// assume json
|
||||
if err = json.Unmarshal([]byte(s), &m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for k := range m {
|
||||
if !isValidMetaKey(k) {
|
||||
return nil, fmt.Errorf("invalid metadata key format")
|
||||
}
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
kv := strings.SplitN(s, "=", 2)
|
||||
if !handle.IsValid(kv[0]) {
|
||||
return nil, fmt.Errorf("invalid metadata key format")
|
||||
}
|
||||
|
||||
if len(kv) == 2 {
|
||||
m[kv[0]] = kv[1]
|
||||
} else {
|
||||
m[kv[0]] = nil
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// isValidMetaKey rejects keys that can not be safely used as part of a JSON path
|
||||
//
|
||||
// Store layer interpolates metadata keys into JSON path expressions where an
|
||||
// unescaped quote would terminate the surrounding string literal
|
||||
func isValidMetaKey(k string) bool {
|
||||
return !strings.Contains(k, "'")
|
||||
}
|
||||
120
server/pkg/payload/util_test.go
Normal file
120
server/pkg/payload/util_test.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package payload
|
||||
|
||||
import (
|
||||
"github.com/cortezaproject/corteza/server/pkg/filter"
|
||||
"github.com/stretchr/testify/require"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseFilterState(t *testing.T) {
|
||||
var (
|
||||
req = require.New(t)
|
||||
)
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
expected filter.State
|
||||
}{
|
||||
{
|
||||
"invalid string should be Excluded",
|
||||
"zero",
|
||||
filter.StateExcluded,
|
||||
},
|
||||
{
|
||||
"empty string should be Excluded",
|
||||
"0",
|
||||
filter.StateExcluded,
|
||||
},
|
||||
{
|
||||
"Excluded",
|
||||
"0",
|
||||
filter.StateExcluded,
|
||||
},
|
||||
{
|
||||
"Excluded",
|
||||
"1",
|
||||
filter.StateInclusive,
|
||||
},
|
||||
{
|
||||
"Excluded",
|
||||
"2",
|
||||
filter.StateExclusive,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req.Equal(tt.expected, ParseFilterState(tt.input))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseMeta(t *testing.T) {
|
||||
var (
|
||||
req = require.New(t)
|
||||
)
|
||||
tests := []struct {
|
||||
name string
|
||||
input []string
|
||||
expected map[string]any
|
||||
hasError bool
|
||||
}{
|
||||
{
|
||||
"key-value pair",
|
||||
[]string{"foo=bar"},
|
||||
map[string]any{"foo": "bar"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"json object",
|
||||
[]string{`{"foo":"bar"}`},
|
||||
map[string]any{"foo": "bar"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"json object with a key the handle validation would reject",
|
||||
[]string{`{"a":"bar"}`},
|
||||
map[string]any{"a": "bar"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"json object with a quoted key",
|
||||
[]string{`{"fo'o":"bar"}`},
|
||||
nil,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"json object with a backslash in the key",
|
||||
[]string{`{"fo\\o":"bar"}`},
|
||||
map[string]any{`fo\o`: "bar"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"json object with a double quote in the key",
|
||||
[]string{`{"fo\"o":"bar"}`},
|
||||
map[string]any{`fo"o`: "bar"},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"json object with a control character in the key",
|
||||
[]string{"{\"fo\\u0000o\":\"bar\"}"},
|
||||
map[string]any{"fo\x00o": "bar"},
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
m, err := ParseMeta(tt.input)
|
||||
|
||||
if tt.hasError {
|
||||
req.Error(err)
|
||||
req.Nil(m)
|
||||
return
|
||||
}
|
||||
|
||||
req.NoError(err)
|
||||
req.Equal(tt.expected, m)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user