snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
26
oryx/stringsx/case.go
Normal file
26
oryx/stringsx/case.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package stringsx
|
||||
|
||||
import "unicode"
|
||||
|
||||
// ToLowerInitial converts a string's first character to lower case.
|
||||
func ToLowerInitial(s string) string {
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
a := []rune(s)
|
||||
a[0] = unicode.ToLower(a[0])
|
||||
return string(a)
|
||||
}
|
||||
|
||||
// ToUpperInitial converts a string's first character to upper case.
|
||||
func ToUpperInitial(s string) string {
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
a := []rune(s)
|
||||
a[0] = unicode.ToUpper(a[0])
|
||||
return string(a)
|
||||
}
|
||||
16
oryx/stringsx/split.go
Normal file
16
oryx/stringsx/split.go
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package stringsx
|
||||
|
||||
import "strings"
|
||||
|
||||
// Splitx is a special case of strings.Split
|
||||
// which returns an empty slice if the string is empty
|
||||
func Splitx(s, sep string) []string {
|
||||
if s == "" {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
return strings.Split(s, sep)
|
||||
}
|
||||
90
oryx/stringsx/switch_case.go
Normal file
90
oryx/stringsx/switch_case.go
Normal file
@@ -0,0 +1,90 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package stringsx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
RegisteredCases struct {
|
||||
cases []string
|
||||
actual string
|
||||
}
|
||||
errUnknownCase struct {
|
||||
*RegisteredCases
|
||||
}
|
||||
RegisteredPrefixes struct {
|
||||
prefixes []string
|
||||
actual string
|
||||
}
|
||||
errUnknownPrefix struct {
|
||||
*RegisteredPrefixes
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnknownCase = errUnknownCase{}
|
||||
ErrUnknownPrefix = errUnknownPrefix{}
|
||||
)
|
||||
|
||||
func SwitchExact(actual string) *RegisteredCases {
|
||||
return &RegisteredCases{
|
||||
actual: actual,
|
||||
}
|
||||
}
|
||||
|
||||
func SwitchPrefix(actual string) *RegisteredPrefixes {
|
||||
return &RegisteredPrefixes{
|
||||
actual: actual,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RegisteredCases) AddCase(cases ...string) bool {
|
||||
r.cases = append(r.cases, cases...)
|
||||
return slices.Contains(cases, r.actual)
|
||||
}
|
||||
|
||||
func (r *RegisteredPrefixes) HasPrefix(prefixes ...string) bool {
|
||||
r.prefixes = append(r.prefixes, prefixes...)
|
||||
return slices.ContainsFunc(prefixes, func(s string) bool {
|
||||
return strings.HasPrefix(r.actual, s)
|
||||
})
|
||||
}
|
||||
|
||||
func (r *RegisteredCases) String() string {
|
||||
return "[" + strings.Join(r.cases, ", ") + "]"
|
||||
}
|
||||
|
||||
func (r *RegisteredPrefixes) String() string {
|
||||
return "[" + strings.Join(r.prefixes, ", ") + "]"
|
||||
}
|
||||
|
||||
func (r *RegisteredCases) ToUnknownCaseErr() error {
|
||||
return errUnknownCase{r}
|
||||
}
|
||||
|
||||
func (r *RegisteredPrefixes) ToUnknownPrefixErr() error {
|
||||
return errUnknownPrefix{r}
|
||||
}
|
||||
|
||||
func (e errUnknownCase) Error() string {
|
||||
return fmt.Sprintf("expected one of %s but got %s", e.String(), e.actual)
|
||||
}
|
||||
|
||||
func (e errUnknownCase) Is(err error) bool {
|
||||
_, ok := err.(errUnknownCase)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (e errUnknownPrefix) Error() string {
|
||||
return fmt.Sprintf("expected %s to have one of the prefixes %s", e.actual, e.String())
|
||||
}
|
||||
|
||||
func (e errUnknownPrefix) Is(err error) bool {
|
||||
_, ok := err.(errUnknownPrefix)
|
||||
return ok
|
||||
}
|
||||
21
oryx/stringsx/truncate.go
Normal file
21
oryx/stringsx/truncate.go
Normal file
@@ -0,0 +1,21 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package stringsx
|
||||
|
||||
import "unicode/utf8"
|
||||
|
||||
// TruncateByteLen returns string truncated at the end with the length specified
|
||||
func TruncateByteLen(s string, length int) string {
|
||||
if length <= 0 || len(s) <= length {
|
||||
return s
|
||||
}
|
||||
|
||||
res := s[:length]
|
||||
|
||||
// in case we cut in the middle of an utf8 rune, we have to remove the last byte as well until it fits
|
||||
for !utf8.ValidString(res) {
|
||||
res = res[:len(res)-1]
|
||||
}
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user