Skip to content

Commit

Permalink
more funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed Jul 29, 2023
1 parent c37f1ab commit 3a90441
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/arrayx/append.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ func RemoveAt[T any](arr []T, i int) []T {
}
return append(arr[:i], arr[i+1:]...)
}

func PreAppend[T any](item T, slice []T) []T {
return append([]T{item}, slice...)
}
21 changes: 21 additions & 0 deletions pkg/cryptox/hash_str.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cryptox

func SHA256Str(s string) string {
return SHA256([]byte(s))
}

func SHA512Str(s string) string {
return SHA512([]byte(s))
}

func SHA384Str(s string) string {
return SHA384([]byte(s))
}

func SHA512_224Str(s string) string {
return SHA512_224([]byte(s))
}

func SHA512_256Str(s string) string {
return SHA512_256([]byte(s))
}
18 changes: 18 additions & 0 deletions pkg/ds/dic/concurrent_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,21 @@ func (d *ConcurrentDictionary[K, V]) UnmarshalJSON(b []byte) error {
defer d.l.Unlock()
return d.m.UnmarshalJSON(b)
}

func (d *ConcurrentDictionary[K, V]) String() string {
d.l.RLock()
defer d.l.RUnlock()
return d.m.String()
}

func (d *ConcurrentDictionary[K, V]) ROper(f func(m map[K]V)) {
d.l.RLock()
defer d.l.RUnlock()
f(d.m.fields)
}

func (d *ConcurrentDictionary[K, V]) WOper(f func(m map[K]V)) {
d.l.Lock()
defer d.l.Unlock()
f(d.m.fields)
}
81 changes: 81 additions & 0 deletions pkg/mapx/getter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package mapx

import "strconv"

func GetString(m map[string]any, key string, ifFailed string) string {
if v, ok := m[key]; ok {
if str, ok := v.(string); ok {
return str
}
return ifFailed
}
return ifFailed
}

func GetBool(m map[string]any, key string, ifFailed bool) bool {
if v, ok := m[key]; ok {
switch v.(type) {
case bool:
return v.(bool)
case string:
if v.(string) == "true" {
return true
}
return false
default:
return ifFailed
}
}
return ifFailed
}

func GetInt(m map[string]any, key string, ifFailed int) int {
if v, ok := m[key]; ok {
switch v.(type) {
case int:
return v.(int)
case string:
if i, err := strconv.Atoi(v.(string)); err == nil {
return i
}
return ifFailed
default:
return ifFailed
}
}
return ifFailed
}

func GetInt64(m map[string]any, key string, ifFailed int64) int64 {
if v, ok := m[key]; ok {
switch v.(type) {
case int64:
return v.(int64)
case string:
if i, err := strconv.ParseInt(v.(string), 10, 64); err == nil {
return i
}
return ifFailed
default:
return ifFailed
}
}
return ifFailed
}

func GetFloat64(m map[string]any, key string, ifFailed float64) float64 {
if v, ok := m[key]; ok {
switch v.(type) {
case float64:
return v.(float64)
case string:
if i, err := strconv.ParseFloat(v.(string), 64); err == nil {
return i
}
return ifFailed
default:
return ifFailed
}
}
return ifFailed
}
77 changes: 77 additions & 0 deletions pkg/x/assert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package x

func Assert[T any](in any, ifFailed T) T {
v, ok := in.(T)
if !ok {
return ifFailed
}
return v
}

func AssertPanic[T any](in any) T {
v, ok := in.(T)
if !ok {
panic("assertion failed")
}
return v
}

func AssertAsInt(in any, ifFailed int) int {
switch in.(type) {
case int:
return in.(int)
case int8:
return int(in.(int8))
case int16:
return int(in.(int16))
case int32:
return int(in.(int32))
case int64:
return int(in.(int64))
case uint:
return int(in.(uint))
case uint8:
return int(in.(uint8))
case uint16:
return int(in.(uint16))
case uint32:
return int(in.(uint32))
case uint64:
return int(in.(uint64))
case float32:
return int(in.(float32))
case float64:
return int(in.(float64))
}
return ifFailed
}

func AssertAsFloat(in any, ifFailed float64) float64 {
switch in.(type) {
case int:
return float64(in.(int))
case int8:
return float64(in.(int8))
case int16:
return float64(in.(int16))
case int32:
return float64(in.(int32))
case int64:
return float64(in.(int64))
case uint:
return float64(in.(uint))
case uint8:
return float64(in.(uint8))
case uint16:
return float64(in.(uint16))
case uint32:
return float64(in.(uint32))
case uint64:
return float64(in.(uint64))
case float32:
return float64(in.(float32))
case float64:
return in.(float64)
}
return ifFailed
}
18 changes: 18 additions & 0 deletions pkg/x/nil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package x

func NotNil(a ...any) bool {
for _, v := range a {
if v == nil {
return false
}
}
return true
}

func PanicIfNil(a ...any) {
for _, v := range a {
if v == nil {
panic("nil")
}
}
}
29 changes: 29 additions & 0 deletions pkg/x/syntax.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package x

func Ite[T any](conExpr bool, ifTrue, ifFalse T) T {
if conExpr {
return ifTrue
}
return ifFalse
}

func IteFunc[T any](conExpr bool, ifTrue, ifFalse func() T) T {
if conExpr {
return ifTrue()
}
return ifFalse()
}

func IteFuncTrue[T any](conExpr bool, ifTrue func() T, ifFalse T) T {
if conExpr {
return ifTrue()
}
return ifFalse
}

func IteFuncElse[T any](conExpr bool, ifTrue T, ifFalse func() T) T {
if conExpr {
return ifTrue
}
return ifFalse()
}

0 comments on commit 3a90441

Please sign in to comment.