Documentation ¶
Index ¶
- func Marshal(v any) ([]byte, error)
- func ReadString(s string, opts ...ReaderOption) ([][]string, error)
- func Unmarshal(data []byte, v any) error
- type InvalidScannerOptionError
- type InvalidUnmarshalError
- type MarshalError
- type Reader
- type ReaderOption
- type ScannerError
- type UnmarshalError
- type UnmarshalUnsupportedTypeError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal returns tuples encoding of v.
Marshal supports:
- struct with tags
- pointer to a struct with tags
- slice of structs with tags
- pointer to slice of structs with tags
- map
- pointer to a map
- slice of maps
- pointer to a slice of maps
In case of map, map key value used as tuple key. All map entries marshaled.
Only basic types supported as values, i.e string, int, float, boolean. MarshalError returned in case, when unsupported type found.
Example ¶
package main import ( "fmt" "github.com/antklim/tuples" ) func main() { type person struct { Name string `tuples:"full_name"` Age int `tuples:"years-old"` } p := person{Name: "Bob", Age: 33} b, err := tuples.Marshal(p) if err != nil { fmt.Println(err) } fmt.Printf("%s\n", string(b)) pp := []person{{Name: "Bob", Age: 33}, {Name: "Paul", Age: 99}} b, err = tuples.Marshal(pp) if err != nil { fmt.Println(err) } fmt.Printf("%s\n", string(b)) }
Output: full_name=Bob,years-old=33 full_name=Bob,years-old=33 full_name=Paul,years-old=99
func ReadString ¶
func ReadString(s string, opts ...ReaderOption) ([][]string, error)
ReadString reads all tuples from the string. It returns a slice of tuples values. It returns error when reader initialisation failed or read process failed.
Usage:
tuples, err := ReadString("fname=John,lname=Doe dob=2000-01-01 age=17") if err != nil { return err } fmt.Println(tuples) // Output: // [[John Doe] [2000-01-01] [17]]
Example ¶
package main import ( "fmt" "github.com/antklim/tuples" ) func main() { in := "fname=John,lname=Doe,dob=2000-01-01 fname=Bob,lname=Smith,dob=2010-10-10" v, err := tuples.ReadString(in) if err != nil { fmt.Println(err) } fmt.Printf("%v\n", v) }
Output: [[John Doe 2000-01-01] [Bob Smith 2010-10-10]]
func Unmarshal ¶
Unmarshal parses the tuples-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.
Example ¶
package main import ( "fmt" "github.com/antklim/tuples" ) func main() { type person struct { Name string `tuples:"name"` Age int `tuples:"age"` IsAdult bool `tuples:"adult"` } in := "name=John,age=23,adult=true" var p []person if err := tuples.Unmarshal([]byte(in), &p); err != nil { fmt.Println(err) } fmt.Printf("%+v\n", p) in = "name=John,age=23,adult=true name=Bob,adult=true adult=true,age=30" var pp []person if err := tuples.Unmarshal([]byte(in), &pp); err != nil { fmt.Println(err) } fmt.Printf("%+v\n", pp) in = "name=John,lname=Doe,age=17 height=170,weight=50" var ppp any if err := tuples.Unmarshal([]byte(in), &ppp); err != nil { fmt.Println(err) } fmt.Printf("%+v\n", ppp) }
Output: [{Name:John Age:23 IsAdult:true}] [{Name:John Age:23 IsAdult:true} {Name:Bob Age:0 IsAdult:true} {Name: Age:30 IsAdult:true}] [map[age:17 lname:Doe name:John] map[height:170 weight:50]]
Types ¶
type InvalidScannerOptionError ¶
type InvalidScannerOptionError struct {
// contains filtered or unexported fields
}
InvalidScannerOptionError describes an error that occurred while initializing scanner with invalid options.
func (*InvalidScannerOptionError) Error ¶
func (e *InvalidScannerOptionError) Error() string
func (*InvalidScannerOptionError) Unwrap ¶
func (e *InvalidScannerOptionError) Unwrap() error
type InvalidUnmarshalError ¶
InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer).
func (*InvalidUnmarshalError) Error ¶
func (e *InvalidUnmarshalError) Error() string
type MarshalError ¶
type MarshalError struct {
// contains filtered or unexported fields
}
MarshalError describes an error that occurred while marshaling a Go value to a tuple string.
func (*MarshalError) Error ¶
func (e *MarshalError) Error() string
func (*MarshalError) Unwrap ¶
func (e *MarshalError) Unwrap() error
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader describes a tuples reader.
func NewReader ¶
func NewReader(r io.Reader, opts ...ReaderOption) (*Reader, error)
NewReader creates a new instance of the Reader. If reader creation fails it returns error.
func (*Reader) Read ¶
Read reads one tuple at a time and returns fields values in the order they appear in the string. It returns error when read fails or when reached the end of the tuples input.
Example ¶
package main import ( "fmt" "io" "strings" "github.com/antklim/tuples" ) func main() { in := "fname=John,lname=Doe,dob=2000-01-01 fname=Bob,lname=Smith,dob=2010-10-10" r, err := tuples.NewReader(strings.NewReader(in)) if err != nil { fmt.Println(err) } for { tuple, err := r.Read() if err == io.EOF { break } if err != nil { fmt.Println(err) } fmt.Printf("%v\n", tuple) } }
Output: [John Doe 2000-01-01] [Bob Smith 2010-10-10]
func (*Reader) ReadAll ¶
ReadAll reads all tuples from the input. It returns a slice of tuples values. It returns error when reader initialisation failed or read process failed.
Example ¶
package main import ( "fmt" "strings" "github.com/antklim/tuples" ) func main() { in := "fname=John,lname=Doe,dob=2000-01-01 fname=Bob,lname=Smith,dob=2010-10-10" r, err := tuples.NewReader(strings.NewReader(in)) if err != nil { fmt.Println(err) } v, err := r.ReadAll() if err != nil { fmt.Println(err) } fmt.Printf("%v\n", v) }
Output: [[John Doe 2000-01-01] [Bob Smith 2010-10-10]]
type ReaderOption ¶
type ReaderOption func(*readerOptions)
ReaderOption describes a reader option, i.e fields delimiter, key-value delimiter, etc.
func WithFieldsDelimiter ¶
func WithFieldsDelimiter(d rune) ReaderOption
WithFieldsDelimiter sets a custom fields delimiter option for reader. Default delimiter is ','.
func WithKeyValueDelimiter ¶
func WithKeyValueDelimiter(d rune) ReaderOption
WithFieldsDelimiter sets a custom key-value delimiter option for reader. Default delimiter is '='.
type ScannerError ¶
type ScannerError struct {
// contains filtered or unexported fields
}
ScannerError describes an error that occurred while scanning a tuple.
func (*ScannerError) Error ¶
func (e *ScannerError) Error() string
func (*ScannerError) Unwrap ¶
func (e *ScannerError) Unwrap() error
type UnmarshalError ¶
UnmarshalError describes an error that occurred while unmarshaling a tuple fields values into a Go type fields.
func (*UnmarshalError) Error ¶
func (e *UnmarshalError) Error() string
func (*UnmarshalError) Unwrap ¶
func (e *UnmarshalError) Unwrap() error
type UnmarshalUnsupportedTypeError ¶
UnmarshalUnsupportedTypeError describes an unsupported field type of a value of a specific Go type.
func (*UnmarshalUnsupportedTypeError) Error ¶
func (e *UnmarshalUnsupportedTypeError) Error() string