Documentation
¶
Overview ¶
Package parse provides parsing functionality for converting similar looking values into others with validation.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse attempts to copy data from src into dst. If dst implements the Parser interface, Parse simply calls dst.Parse(src). Otherwise, it uses reflection to assign fields or elements to dst. To succeed, dst must be a non-nil pointer to a settable value.
The function supports struct-to-struct, map-to-struct, and slice-to-slice copying, as well as direct conversions between basic types (including []byte to string). If src is nil, no assignment is performed. If dst is not a valid pointer, an InvalidParseError is returned. If a type conversion is not possible, an UnconvertableTypeError is returned.
Successfully parsed value is already validated and can be used safely.
Any errors encountered during parsing are wrapped in a ParseError.
Parse also accepts options. See Option.
Example ¶
package main import ( "fmt" "github.com/metafates/schema/parse" "github.com/metafates/schema/required" ) func main() { type User struct { Name required.Any[string] `json:"such_tags_are_ignored_by_default"` Comment string Age int } var user1, user2 User parse.Parse(map[string]any{ "Name": "john", "Comment": "lorem ipsum", "Age": 99, "UnknownField": "this field will be ignored", }, &user1) parse.Parse(struct { Name, Comment string Age uint8 // types will be converted }{ Name: "jane", Comment: "dolor sit", Age: 55, }, &user2) fmt.Printf("user1: name=%q comment=%q age=%v\n", user1.Name.Get(), user1.Comment, user1.Age) fmt.Printf("user2: name=%q comment=%q age=%v\n", user2.Name.Get(), user2.Comment, user2.Age) }
Output: user1: name="john" comment="lorem ipsum" age=99 user2: name="jane" comment="dolor sit" age=55
Types ¶
type InvalidParseError ¶
InvalidParseError describes an invalid argument passed to Parse. The argument to Parse must be a non-nil pointer.
func (InvalidParseError) Error ¶
func (e InvalidParseError) Error() string
type Option ¶
type Option func(cfg *config)
Option modifies the parsing logic.
func WithDisallowUnknownFields ¶
func WithDisallowUnknownFields() Option
WithDisallowUnknownFields is an option that will return an error if unknown field is supplied.
func WithRenameFunc ¶
func WithRenameFunc(f RenameFunc) Option
WithRenameFunc is an option that will rename src fields/keys during parsing before matching with dst fields.
type ParseError ¶
func (ParseError) Error ¶
func (e ParseError) Error() string
func (ParseError) Path ¶
func (e ParseError) Path() string
Path returns the path to the value which raised this error.
func (ParseError) Unwrap ¶
func (e ParseError) Unwrap() error
type RenameFunc ¶
type UnconvertableTypeError ¶
type UnconvertableTypeError struct {
Target, Original string
}
func (UnconvertableTypeError) Error ¶
func (e UnconvertableTypeError) Error() string
type UnknownFieldError ¶
type UnknownFieldError struct {
Name string
}
func (UnknownFieldError) Error ¶
func (e UnknownFieldError) Error() string