utils

package
v0.0.0-...-15de22a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidJSON is returned when the input is not valid JSON
	ErrInvalidJSON = errors.New("invalid JSON input")
)

Functions

func GetEnv

func GetEnv(key, fallback string) string

func GetEnvAsInt

func GetEnvAsInt(key string, fallback int) int

func ReadAndTrim

func ReadAndTrim(r io.Reader, v interface{}, opts TrimOptions) error

ReadAndTrim reads from an io.Reader, trims the JSON using gjson/sjson, and unmarshals it into the provided value.

func TrimJSON

func TrimJSON(jsonStr string, opts TrimOptions) (string, bool, error)

TrimJSON takes a JSON string and trims whitespace from string values at the specified paths. Returns the trimmed JSON and a boolean indicating if any modifications were made.

func TrimJSONBytes

func TrimJSONBytes(data []byte, opts TrimOptions) ([]byte, bool, error)

TrimJSONBytes is like TrimJSON but works with byte slices

func UnmarshalAndTrim

func UnmarshalAndTrim(data []byte, v interface{}, opts TrimOptions) error

UnmarshalAndTrim unmarshals JSON data into a struct while trimming string fields

Types

type TrimOptions

type TrimOptions struct {
	// Paths to trim with gjson path syntax
	// See: https://github.com/tidwall/gjson#path-syntax
	Paths []string
}

TrimOptions configures the JSON trimming behavior

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL