typeconv

package
v0.9.8 Latest Latest
Warning

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

Go to latest
Published: May 15, 2024 License: Apache-2.0 Imports: 9 Imported by: 11

Documentation

Index

Constants

View Source
const TSLayout = "2006-01-02T15:04:05.000Z"

TSLayout is the layout to be used in the timestamp marshaling/unmarshaling everywhere. The timezone must always be UTC.

Variables

This section is empty.

Functions

func Convert

func Convert(to, from interface{}) (err error)

Convert transforms the value of from into to, by translating the structure from into a set of events (go-structform.Visitor) that can applied to the value given by to. The operation fails if the values are not compatible (for example trying to convert an object into an int). To `to` parameter must be a pointer, otherwise the operation fails.

Go structures can influence the transformation via tags using the `struct` namespace. If the tag is missing, the structs field names are used. Additional options are separates by `,`. options:

`squash`, `inline`: The fields in the child struct/map are assumed to be inlined, without reporting a sub-oject.
`omitempty`: The field is not converted if it is "empty". For example an
             empty string, array or `nil` pointers are assumed to be empty. In either case the original value
             in `to` will not be overwritten.
`omit`, `-`: Do not convert the field.

Types

type Converter

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

Converter converts structured data between arbitrary typed (serializable) go structures and maps/slices/arrays. It uses go-structform/gotype for input and output values each, such that any arbitrary structures can be used.

The converter computes and caches mapping operations for go structures it has visited.

func NewConverter

func NewConverter() *Converter

NewConverter creates a new converter with local state for tracking known type conversations.

func (*Converter) Convert

func (c *Converter) Convert(to, from interface{}) (err error)

Convert transforms the value of from into to, by translating the structure from into a set of events (go-structform.Visitor) that can applied to the value given by to. The operation fails if the values are not compatible (for example trying to convert an object into an int), or `to` is no pointer.

type Time

type Time time.Time

Time is an abstraction for the time.Time type

func MustParseTime

func MustParseTime(timespec string) Time

MustParseTime is a convenience equivalent of the ParseTime function that panics in case of errors.

func ParseTime

func ParseTime(timespec string) (Time, error)

ParseTime parses a time in the TSLayout format.

func (Time) Hash32

func (t Time) Hash32(h hash.Hash32) error

func (Time) MarshalJSON

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

MarshalJSON implements json.Marshaler interface. The time is a quoted string in the JsTsLayout format.

func (Time) String

func (t Time) String() string

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements js.Unmarshaler interface. The time is expected to be a quoted string in TSLayout format.

Jump to

Keyboard shortcuts

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