Skip to content

Commit

Permalink
upf
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinZonda committed Jun 5, 2024
1 parent 3a90441 commit 2bf0727
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 50 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module github.com/KevinZonda/GoX
go 1.19
go 1.22
16 changes: 16 additions & 0 deletions pkg/iox/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,19 @@ func ReadAllLines(file string) (strs []string, err error) {
strs = strings.Split(str, "\n")
return
}

func IOReadAllText(i io.Reader) (string, error) {
bs, err := io.ReadAll(i)
if err != nil {
return "", err
}
return string(bs), nil
}

func IOReadAllLines(i io.Reader) ([]string, error) {
str, err := IOReadAllText(i)
if err != nil {
return nil, err
}
return strings.Split(str, "\n"), nil
}
70 changes: 70 additions & 0 deletions pkg/jsonx/ser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package jsonx

import "encoding/json"

func ToString(o any) string {
bs, err := json.Marshal(o)
if err != nil {
panic(err)
}
return string(bs)
}

func ToStringE(o any) (string, error) {
bs, err := json.Marshal(o)
if err != nil {
return "", err
}
return string(bs), nil
}

// ToStringI with indent
func ToStringI(o any) string {
bs, err := json.MarshalIndent(o, "", " ")
if err != nil {
panic(err)
}
return string(bs)
}

// ToStringIE with indent
func ToStringIE(o any) (string, error) {
bs, err := json.MarshalIndent(o, "", " ")
if err != nil {
return "", err
}
return string(bs), nil
}

func ToObj[T any](s string) T {
var v T
err := json.Unmarshal([]byte(s), &v)
if err != nil {
panic(err)
}
return v
}

func ToObjE[T any](s string) (T, error) {
var v T
err := json.Unmarshal([]byte(s), &v)
return v, err
}

func ToObjPtr[T any](s string) *T {
var v T
err := json.Unmarshal([]byte(s), &v)
if err != nil || v == nil {
return nil
}
return &v
}

func ToObjPtrE[T any](s string) (*T, error) {
var v T
err := json.Unmarshal([]byte(s), &v)
if err != nil || v == nil {
return nil, err
}
return &v, nil
}
53 changes: 53 additions & 0 deletions pkg/lambda/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package lambda

func Map[T any, R any](in []T, f func(T) R) []R {
out := make([]R, len(in))
for i, v := range in {
out[i] = f(v)
}
return out
}

func ForEach[T any](in []T, f func(T)) {
for _, v := range in {
f(v)
}
}

func Filter[T any](in []T, f func(T) bool) []T {
out := make([]T, 0, len(in))
for _, v := range in {
if f(v) {
out = append(out, v)
}
}
return out
}

func Find[T any](in []T, f func(T) bool) (T, bool) {
for _, v := range in {
if f(v) {
return v, true
}
}
var zero T
return zero, false
}

func Any[T any](in []T, f func(T) bool) bool {
for _, v := range in {
if f(v) {
return true
}
}
return false
}

func All[T any](in []T, f func(T) bool) bool {
for _, v := range in {
if !f(v) {
return false
}
}
return true
}
27 changes: 21 additions & 6 deletions pkg/netx/httpx/http.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
package httpx

import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
)

func GET(url string) (string, error) {
resp, err := http.Get(url)
func GETString(url string) (string, error) {
bs, err := GETBytes(url)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
return string(bs), nil
}

func GETBytes(url string) (body []byte, err error) {
resp, err := http.Get(url)
if err != nil {
return "", err
return nil, err
}
body, err = io.ReadAll(resp.Body)
return
}

func GETJson[T any](url string) (T, error) {
var v T
bs, err := GETBytes(url)
if err != nil {
return v, err
}
return string(body), nil
err = json.Unmarshal(bs, &v)
return v, err
}

var _defaultUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
Expand Down
19 changes: 18 additions & 1 deletion pkg/panicx/any.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package panicx

import "strconv"
import (
"fmt"
"strconv"
)

func Panic(a any) {
panic(a)
Expand All @@ -14,6 +17,20 @@ func NotNil(a ...any) {
}
}

func NotNilErr(e error) {
if e != nil {
panic(e)
}
}

func NotImplemented() {
panic("Not implemented")
}

func PanicF(format string, a ...any) {
panic(fmt.Sprintf(format, a...))
}

func Format(format string, a ...any) {
panic(fmt.Sprintf(format, a...))
}
14 changes: 1 addition & 13 deletions pkg/panicx/if.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
package panicx

import "fmt"

func PanicIf(exp bool, v any) {
func If(exp bool, v any) {
if exp {
panic(v)
}
}

func PanicIfNotNil(b any, v any) {
if b != nil {
panic(v)
}
}

func PanicF(format string, a ...any) {
panic(fmt.Sprintf(format, a...))
}
84 changes: 84 additions & 0 deletions pkg/stringx/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,102 @@ func Int32(s string) (int32, error) {
return int32(i), err
}

func Int32Or(s string, or int32) int32 {
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return or
}
return int32(i)
}

func ToInt32Force(s string) int32 {
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
panic(err)
}
return int32(i)
}

func Int(s string) (int, error) {
return strconv.Atoi(s)
}

func IntOr(s string, or int) int {
i, err := strconv.Atoi(s)
if err != nil {
return or
}
return i
}

func ToIntForce(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}

func Int64(s string) (int64, error) {
return strconv.ParseInt(s, 10, 64)
}

func Int64Or(s string, or int64) int64 {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return or
}
return i
}

func ToInt64Force(s string) int64 {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
panic(err)
}
return i
}

func Float64(s string) (float64, error) {
return strconv.ParseFloat(s, 64)
}

func Float64Or(s string, or float64) float64 {
i, err := strconv.ParseFloat(s, 64)
if err != nil {
return or
}
return i
}

func ToFloat64Force(s string) float64 {
i, err := strconv.ParseFloat(s, 64)
if err != nil {
panic(err)
}
return i
}

func Bool(s string) (bool, error) {
return strconv.ParseBool(s)
}

func BoolOr(s string, or bool) bool {
i, err := strconv.ParseBool(s)
if err != nil {
return or
}
return i
}

func ToBoolForce(s string) bool {
i, err := strconv.ParseBool(s)
if err != nil {
panic(err)
}
return i
}

func Ptr(s string) *string {
return &s
}
Expand Down
29 changes: 0 additions & 29 deletions pkg/stringx/concat.go

This file was deleted.

17 changes: 17 additions & 0 deletions pkg/stringx/join.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package stringx

import "strings"

func Join(sep string, ss ...string) string {
return strings.Join(ss, sep)
}

func JoinNotEmpty(sep string, ss ...string) string {
var _ss []string
for _, s := range ss {
if s != "" {
_ss = append(_ss, s)
}
}
return strings.Join(_ss, sep)
}
Loading

0 comments on commit 2bf0727

Please sign in to comment.