mirror of
https://github.com/aykhans/dodo.git
synced 2025-09-03 18:03:34 +00:00
🔨 Restructure entire project logic
- Moved readers to the config package - Added an option to read remote config files - Moved the validation package to the config package and removed the validator dependency - Moved the customerrors package to the config package - Replaced fatih/color with jedib0t/go-pretty/v6/text - Removed proxy check functionality - Added param, header, cookie, body, and proxy flags to the CLI - Allowed multiple values for the same key in params, headers, and cookies
This commit is contained in:
14
utils/compare.go
Normal file
14
utils/compare.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package utils
|
||||
|
||||
func IsNilOrZero[T comparable](value *T) bool {
|
||||
if value == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
var zero T
|
||||
if *value == zero {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
@@ -1,85 +1,5 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type TruncatedMarshaller struct {
|
||||
Value interface{}
|
||||
MaxItems int
|
||||
}
|
||||
|
||||
func (t TruncatedMarshaller) MarshalJSON() ([]byte, error) {
|
||||
val := reflect.ValueOf(t.Value)
|
||||
|
||||
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
|
||||
return json.Marshal(t.Value)
|
||||
}
|
||||
if val.Len() == 0 {
|
||||
return []byte("[]"), nil
|
||||
}
|
||||
|
||||
length := val.Len()
|
||||
if length <= t.MaxItems {
|
||||
return json.Marshal(t.Value)
|
||||
}
|
||||
|
||||
truncated := make([]interface{}, t.MaxItems+1)
|
||||
|
||||
for i := 0; i < t.MaxItems; i++ {
|
||||
truncated[i] = val.Index(i).Interface()
|
||||
}
|
||||
|
||||
remaining := length - t.MaxItems
|
||||
truncated[t.MaxItems] = fmt.Sprintf("+%d", remaining)
|
||||
|
||||
return json.Marshal(truncated)
|
||||
}
|
||||
|
||||
func PrettyJSONMarshal(v interface{}, maxItems int, prefix, indent string) []byte {
|
||||
truncated := processValue(v, maxItems)
|
||||
d, _ := json.MarshalIndent(truncated, prefix, indent)
|
||||
return d
|
||||
}
|
||||
|
||||
func processValue(v interface{}, maxItems int) interface{} {
|
||||
val := reflect.ValueOf(v)
|
||||
|
||||
switch val.Kind() {
|
||||
case reflect.Map:
|
||||
newMap := make(map[string]interface{})
|
||||
iter := val.MapRange()
|
||||
for iter.Next() {
|
||||
k := iter.Key().String()
|
||||
newMap[k] = processValue(iter.Value().Interface(), maxItems)
|
||||
}
|
||||
return newMap
|
||||
|
||||
case reflect.Slice, reflect.Array:
|
||||
return TruncatedMarshaller{Value: v, MaxItems: maxItems}
|
||||
|
||||
case reflect.Struct:
|
||||
newMap := make(map[string]interface{})
|
||||
t := val.Type()
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
if field.IsExported() {
|
||||
jsonTag := field.Tag.Get("json")
|
||||
if jsonTag == "-" {
|
||||
continue
|
||||
}
|
||||
fieldName := field.Name
|
||||
if jsonTag != "" {
|
||||
fieldName = jsonTag
|
||||
}
|
||||
newMap[fieldName] = processValue(val.Field(i).Interface(), maxItems)
|
||||
}
|
||||
}
|
||||
return newMap
|
||||
|
||||
default:
|
||||
return v
|
||||
}
|
||||
func ToPtr[T any](value T) *T {
|
||||
return &value
|
||||
}
|
||||
|
@@ -4,11 +4,11 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/jedib0t/go-pretty/v6/text"
|
||||
)
|
||||
|
||||
func PrintErr(err error) {
|
||||
color.New(color.FgRed).Fprintln(os.Stderr, err.Error())
|
||||
fmt.Fprintln(os.Stderr, text.FgRed.Sprint(err.Error()))
|
||||
}
|
||||
|
||||
func PrintErrAndExit(err error) {
|
||||
|
@@ -10,15 +10,6 @@ func Flatten[T any](nested [][]*T) []*T {
|
||||
return flattened
|
||||
}
|
||||
|
||||
func Contains[T comparable](slice []T, item T) bool {
|
||||
for _, i := range slice {
|
||||
if i == item {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// RandomValueCycle returns a function that cycles through the provided slice of values
|
||||
// in a random order. Each call to the returned function will yield a value from the slice.
|
||||
// The order of values is determined by the provided random number generator.
|
||||
|
Reference in New Issue
Block a user