base

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2025 License: Apache-2.0Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clean

func Clean(id string) string

Clean returns a valid filename for id.

func ExtractJSONFromMarkdownadded inv0.1.0

func ExtractJSONFromMarkdown(md string) string

ExtractJSONFromMarkdown returns the contents of the first fenced code block in the markdown text md. If there is none, it returns md.

func GetJsonObjectLinesadded inv0.5.2

func GetJsonObjectLines(text string) []string

GetJsonObjectLines splits a string by newlines, trims whitespace from each line, and returns a slice containing only the lines that start with '{'.

func InferJSONSchema

func InferJSONSchema(x any) (s *jsonschema.Schema)

func JSONString

func JSONString(x any) string

JSONString returns json.Marshal(x) as a string. If json.Marshal returns an error, jsonString returns the error text as a JSON string beginning "ERROR:".

func PrettyJSONStringadded inv0.1.0

func PrettyJSONString(x any) string

PrettyJSONString returns json.MarshalIndent(x, "", " ") as a string. If json.MarshalIndent returns an error, jsonString returns the error text as a JSON string beginning "ERROR:".

func ReadJSONFile

func ReadJSONFile(filename string, pvalue any) error

ReadJSONFile JSON-decodes the contents of filename into pvalue, which must be a pointer.

func SchemaAsMapadded inv0.1.0

func SchemaAsMap(s *jsonschema.Schema) map[string]any

SchemaAsMap converts json schema struct to a map (JSON representation).

func ValidJSONadded inv0.5.0

func ValidJSON(s string) bool

Validates if the given string is a valid JSON string

func ValidateIsJSONArrayadded inv0.5.0

func ValidateIsJSONArray(schema map[string]any) bool

ValidateIsJSONArray will validate if the schema represents a JSON array.

func ValidateJSON

func ValidateJSON(dataBytes json.RawMessage, schema *jsonschema.Schema) error

ValidateJSON will validate JSON against the expected schema. It will return an error if it doesn't match the schema, otherwise it will return nil.

func ValidateRaw

func ValidateRaw(dataBytes json.RawMessage, schemaBytes json.RawMessage) error

ValidateRaw will validate JSON data against the JSON schema. It will return an error if it doesn't match the schema, otherwise it will return nil.

func ValidateValue

func ValidateValue(data any, schema *jsonschema.Schema) error

ValidateValue will validate any value against the expected schema. It will return an error if it doesn't match the schema, otherwise it will return nil.

func WriteJSONFile

func WriteJSONFile(filename string, value any) error

WriteJSONFile writes value to filename as JSON.

func Zero

func Zero[T any]() T

Zero returns the Zero value for T.

Types

type ContextKey

type ContextKey[T any] struct { // contains filtered or unexported fields }

A ContextKey is a unique, typed key for a value stored in a context.

func NewContextKey

func NewContextKey[T any]() ContextKey[T]

NewContextKey returns a context key for a value of type T.

func (ContextKey[T]) FromContext

func (k ContextKey[T]) FromContext(ctx context.Context) T

FromContext returns the value associated with this key in the context, or the internal.Zero value for T if the key is not present.

func (ContextKey[T]) NewContext

func (k ContextKey[T]) NewContext(ctx context.Context, value T) context.Context

NewContext returns ctx augmented with this key and the given value.

type Environment

type Environment string

An Environment is the execution context in which the program is running.

const (  EnvironmentDev Environment = "dev" // development: testing, debugging, etc. EnvironmentProd Environment = "prod" // production: user data, SLOs, etc.)

type HTTPError

type HTTPError struct {  Code int Err error TraceID string}

HTTPError is an error that includes an HTTP status code.

func (*HTTPError) Error

func (e *HTTPError) Error() string
close