types

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: MPL-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package types provides common types.

Index

Constants

This section is empty.

Variables

View Source
var (
	ContentTypeNames = map[ContentType]string{
		ContentTypeJson:           "application/json",
		ContentTypeMsgPack:        "application/msgpack",
		ContentTypeYaml:           "application/yaml",
		ContentTypeHtml:           "text/html",
		ContentTypeFormUrlEncoded: "application/x-www-form-urlencoded",
		ContentTypePlain:          "text/plain",
	}
)
View Source
var (
	TypeNames = map[Type]string{
		TypeBoolean:  "Boolean",
		TypeDateTime: "DateTime",
		TypeFloat:    "Float",
		TypeID:       "ID",
		TypeInteger:  "Integer",
		TypeString:   "String",
	}
)

Functions

func BytesSize

func BytesSize(n uint64) string

BytesSize returns a human readable bytes size.

func ConflictKeys

func ConflictKeys[T any](a, b Map[T]) (conflicts []string)

ConflictKeys returns a map of conflicting keys between the two maps.

func DiffKeys

func DiffKeys[T any](a, b Map[T]) (added, removed []string)

DiffKeys returns a map of added and removed keys between the two maps.

func Equal

func Equal(a, b any) bool

Equal returns whether the provided values are equal.

func Less

func Less(a, b any) bool

Less returns whether the first value is less than the second.

func MarshalFile added in v0.0.12

func MarshalFile(name string, in any) error

MarshalFile marshals a file.

func More

func More(a, b any) bool

More returns whether the first value is more than the second.

func NotEqual

func NotEqual(a, b any) bool

NotEqual returns whether the provided values are not equal.

func UnmarshalFile added in v0.0.12

func UnmarshalFile(name string, out any) error

UnmarshalFile unmarshals a file.

Types

type ContentType

type ContentType uint8

ContentType represents the content type.

const (
	ContentTypeInvalid ContentType = iota //
	ContentTypeJson
	ContentTypeMsgPack
	ContentTypeYaml
	ContentTypeHtml
	ContentTypeFormUrlEncoded
	ContentTypePlain
)

func ParseContentType

func ParseContentType(value string) ContentType

ParseContentType parses ContentType from string.

func (ContentType) Bytes

func (ct ContentType) Bytes() []byte

Bytes returns the ContentType as a []byte.

func (ContentType) Marshal

func (ct ContentType) Marshal(in any) (io.Reader, error)

Marshal returns the ContentType as a []byte.

func (ContentType) MarshalJSON

func (ct ContentType) MarshalJSON() ([]byte, error)

MarshalJSON outputs the ContentType as a json.

func (ContentType) String

func (ct ContentType) String() string

String outputs the ContentType as a string.

func (ContentType) Unmarshal

func (ct ContentType) Unmarshal(r io.Reader, out any) error

Unmarshal parses ContentType from []byte.

func (*ContentType) UnmarshalJSON

func (ct *ContentType) UnmarshalJSON(data []byte) error

UnmarshalJSON parses ContentType from json.

func (ContentType) Validate

func (ct ContentType) Validate() bool

Validate returns true if the ContentType is valid.

type DateTime

type DateTime struct {
	time.Time
}

DateTime represents the date and time using RFC3339 format.

func DateTimeNow added in v0.0.15

func DateTimeNow() DateTime

DateTimeNow returns the current date and time.

func ParseDateTime added in v0.0.15

func ParseDateTime(value string) DateTime

ParseDateTime parses DateTime from string.

func (DateTime) MarshalJSON

func (dt DateTime) MarshalJSON() ([]byte, error)

MarshalJSON outputs the DateTime as a json.

func (DateTime) String

func (dt DateTime) String() string

String outputs the DateTime as a string using RFC3339 format.

func (*DateTime) UnmarshalJSON

func (dt *DateTime) UnmarshalJSON(data []byte) error

UnmarshalJSON parses DateTime from json.

func (DateTime) Validate

func (dt DateTime) Validate() bool

Validate returns true if the DateTime is valid.

func (DateTime) Value

func (dt DateTime) Value() (driver.Value, error)

Value outputs the DateTime as a value.

type Map

type Map[T any] map[string]T

Map defines a map of key:value. It implements Map.

func Merge

func Merge[T any](maps ...Map[T]) Map[T]

Merge merges the provided maps into a new map.

func NewMap

func NewMap[T any]() Map[T]

func ToMap

func ToMap[M ~map[K]T, K comparable, T any](m M) Map[T]

ToMap converts a map to a Map.

func (Map[T]) AsString

func (m Map[T]) AsString(key string) string

AsString returns the value for the provided key as a string.

func (Map[T]) Clear

func (m Map[T]) Clear()

Clear clears the map.

func (Map[T]) Clone

func (m Map[T]) Clone() Map[T]

Clone returns a clone of the map.

func (Map[T]) Delete

func (m Map[T]) Delete(key string)

Delete deletes the value for the provided key.

func (Map[T]) Get

func (m Map[T]) Get(key string) T

Get returns the value for the provided key.

func (Map[T]) Has

func (m Map[T]) Has(key string) bool

Has returns whether the provided key exists in the map.

func (Map[T]) IsEmpty

func (m Map[T]) IsEmpty() bool

IsEmpty returns whether the map is empty.

func (Map[T]) Keys

func (m Map[T]) Keys() (keys []string)

Keys returns a slice of keys in the map.

func (Map[T]) Len

func (m Map[T]) Len() int

Len returns the length of the map.

func (Map[T]) Merge

func (m Map[T]) Merge(maps ...Map[T]) Map[T]

Merge merges the provided map into the current map.

func (Map[T]) Range

func (m Map[T]) Range(fn func(key string, value T) bool)

Range iterates over elements in the map.

func (Map[T]) Set

func (m Map[T]) Set(key string, value T)

Set sets the value for the provided key.

func (Map[T]) String

func (m Map[T]) String(sep, join string) string

String returns a string representation of the map.

func (Map[T]) Value

func (m Map[T]) Value() (driver.Value, error)

Value returns a value representation of the map.

func (Map[T]) Values

func (m Map[T]) Values() (values []T)

Values returns a slice of values in the map.

type Option added in v0.0.10

type Option struct {
	Name        string   `json:"name"`
	Description string   `json:"description"`
	Required    bool     `json:"required"`
	Type        Type     `json:"type"`
	Default     any      `json:"default"`
	Min         any      `json:"min,omitempty"`
	Max         any      `json:"max,omitempty"`
	Choices     []string `json:"choices,omitempty"`
}

Option represents the ui option.

func NewBooleanOption added in v0.0.10

func NewBooleanOption(name string, description string, required bool, def bool) *Option

NewBooleanOption creates a new Option instance with type boolean.

func NewDateTimeOption added in v0.0.15

func NewDateTimeOption(name string, description string, required bool, def string, min string, max string) *Option

NewDateTimeOption creates a new Option instance with type datetime.

func NewFloatOption added in v0.0.10

func NewFloatOption(name string, description string, required bool, def float64, min float64, max float64) *Option

NewFloatOption creates a new Option instance with type float.

func NewIDOption added in v0.0.15

func NewIDOption(name string, description string, required bool, def string) *Option

NewIDOption creates a new Option instance with type id.

func NewIntegerOption added in v0.0.10

func NewIntegerOption(name string, description string, required bool, def int, min int, max int) *Option

NewIntegerOption creates a new Option instance with type integer.

func NewOption added in v0.0.10

func NewOption(name string, description string, required bool, t Type, def any, min any, max any, choices []string) *Option

NewOption creates a new Option instance.

func NewStringOption added in v0.0.10

func NewStringOption(name string, description string, required bool, def string, choices []string) *Option

NewStringOption creates a new Option instance with type string.

type Options added in v0.0.10

type Options []*Option

Options represents the options.

func NewOptions added in v0.0.10

func NewOptions(options ...*Option) Options

NewOptions creates a new Options instance.

func (Options) DefaultValue added in v0.0.18

func (o Options) DefaultValue(name string, value any) any

DefaultValue returns the default value of the option with the given name.

func (Options) Get added in v0.0.16

func (o Options) Get(name string) (*Option, error)

Get returns the option with the given name.

func (Options) Index added in v0.0.11

func (o Options) Index(name string) int

Index returns the index of the option with the given name.

func (Options) Len added in v0.0.11

func (o Options) Len() int

Len returns the length of the options.

func (Options) SetDefaultValue added in v0.0.18

func (o Options) SetDefaultValue(name string, value any) error

SetDefaultValue sets the default value of the option with the given name.

func (Options) ToSlice added in v0.0.15

func (o Options) ToSlice() []string

ToSlice returns the options as a slice of strings.

type Type

type Type uint8

Type represents the data type.

const (
	TypeInvalid Type = iota //
	TypeBoolean
	TypeDateTime
	TypeFloat
	TypeID
	TypeInteger
	TypeString
)

func ParseType

func ParseType(value string) Type

ParseType parses Type from string.

func (Type) Bytes

func (t Type) Bytes() []byte

Bytes returns the Type as a []byte.

func (Type) MarshalJSON

func (t Type) MarshalJSON() ([]byte, error)

MarshalJSON outputs the Type as a json.

func (Type) String

func (t Type) String() string

String outputs the Type as a string.

func (*Type) UnmarshalJSON

func (t *Type) UnmarshalJSON(data []byte) error

UnmarshalJSON parses Type from json.

func (Type) Validate

func (t Type) Validate() bool

Validate returns true if the Type is valid.

type URI

type URI struct {
	url.URL
}

URI represents the URI.

func ParseURI

func ParseURI(value string) URI

ParseURI parses URI from string.

func (URI) MarshalJSON

func (u URI) MarshalJSON() ([]byte, error)

MarshalJSON outputs the URI as a json.

func (URI) String

func (u URI) String() string

String outputs the URI as a string.

func (*URI) UnmarshalJSON

func (u *URI) UnmarshalJSON(data []byte) error

UnmarshalJSON parses URI from json.

func (URI) Validate

func (u URI) Validate() bool

Validate returns true if the URI-Reference is valid.

func (URI) Value

func (U URI) Value() (driver.Value, error)

Value outputs the URI as a value.

Jump to

Keyboard shortcuts

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