typed

package module
v0.0.0-...-dd42c0f Latest Latest
Warning

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

Go to latest
Published: May 7, 2023 License: MPL-2.0 Imports: 5 Imported by: 0

README

typed

Golang strongly typed JSON validation and parsing without struct tags.

⚠️ Unreleased ⚠️

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New[T any](val any) T

TODO: New doesn't store any value yet

func Parse

func Parse(json string, typed Typed) error

func ParseBytes

func ParseBytes(json []byte, typed Typed) error

func Serialize

func Serialize(typed Typed) []byte

Serialized the Typed nodes to JSON

Types

type Array

type Array[T any] struct {

	// TODO: Add a safe `.at()` method
	Items []T
	// contains filtered or unexported fields
}

func NewArray

func NewArray[T any](items []T) Array[T]

func (*Array) Check

func (*Array) Check() error

func (*Array[T]) Type

func (*Array[T]) Type() Type

type Boolean

type Boolean struct {
	// contains filtered or unexported fields
}

func NewBoolean

func NewBoolean(val bool) Boolean

func (*Boolean) Check

func (*Boolean) Check() error

func (*Boolean) Type

func (*Boolean) Type() Type

func (*Boolean) Value

func (s *Boolean) Value() bool

type Number

type Number[T NumberType] struct {
	RawVal T
	// contains filtered or unexported fields
}

func NewNumber

func NewNumber[T NumberType](num T) Number[T]

func (*Number) Check

func (*Number) Check() error

func (*Number[T]) Type

func (*Number[T]) Type() Type

func (*Number[T]) Value

func (s *Number[T]) Value() T

type NumberType

type NumberType interface {
	constraints.Integer | constraints.Float
}

type Object

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

func NewObject

func NewObject[T any](props T) Object[T]

func (*Object) Check

func (*Object) Check() error

func (*Object[T]) Type

func (*Object[T]) Type() Type

type Optional

type Optional[T any] struct {
	RawVal *T
	// contains filtered or unexported fields
}

TODO: Change to "Typed" constraint. This will force all implementations to be pointers.

func NewOptional

func NewOptional[T any](typed T) Optional[T]

TODO: Get this working correctly

func (*Optional) Check

func (*Optional) Check() error

func (*Optional[T]) Expect

func (s *Optional[T]) Expect(format string, a ...any) (*T, error)

func (*Optional[T]) IsAbsent

func (s *Optional[T]) IsAbsent() bool

True if Optional Value is absent

func (*Optional[T]) IsPresent

func (s *Optional[T]) IsPresent() bool

True if Optional Value is present

func (*Optional[T]) Maybe

func (s *Optional[T]) Maybe() *T

Get the optional value. May be nil

func (*Optional[T]) OrElse

func (s *Optional[T]) OrElse(fallback T) *T

Gets value. If absent, uses the fallback. The return will not be nil.

func (*Optional[T]) Type

func (*Optional[T]) Type() Type

type Record

type Record[T any] struct {
	Items map[string]T
	// contains filtered or unexported fields
}

Record Object is a mixed bag of properties. Keys are strings and value is user defined.

func NewRecord

func NewRecord[T any](items map[string]T) Record[T]

func (*Record) Check

func (*Record) Check() error

func (*Record[T]) Type

func (*Record[T]) Type() Type

type String

type String struct {
	Val string
	// contains filtered or unexported fields
}

func NewString

func NewString(val string) String

func (*String) Check

func (*String) Check() error

func (*String) Type

func (*String) Type() Type

func (*String) Value

func (s *String) Value() string

type Type

type Type int
const (
	TypeObject Type = iota
	TypeArray
	TypeString
	TypeNumber
	TypeBoolean
	TypeRecord
	TypeOptional
)

func (Type) String

func (t Type) String() string

Returns a string representation of Type

type Typed

type Typed interface {
	Type() Type
	Check() error
	// contains filtered or unexported methods
}

Jump to

Keyboard shortcuts

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