Documentation ¶
Overview ¶
Package parth provides functions for accessing path segments.
When returning an int/uint of any size, the first whole number within the specified segment will be returned. When returning a float of any size, the first decimal number within the specified segment will be returned.
Example ¶
package main import ( "fmt" "net/http" "os" "github.com/codemodus/parth" ) func main() { r, err := http.NewRequest("GET", "/zero/1/2/nn3.3nn/key/5.5", nil) if err != nil { fmt.Fprintln(os.Stderr, err) } printFmt := "Type = %T, Value = %v\n" if s, err := parth.SegmentToString(r.URL.Path, 0); err == nil { fmt.Printf(printFmt, s, s) } if b, err := parth.SegmentToBool(r.URL.Path, 1); err == nil { fmt.Printf(printFmt, b, b) } if i, err := parth.SegmentToInt(r.URL.Path, -4); err == nil { fmt.Printf(printFmt, i, i) } if f, err := parth.SegmentToFloat32(r.URL.Path, 3); err == nil { fmt.Printf(printFmt, f, f) } if s, err := parth.SpanToString(r.URL.Path, 0, -3); err == nil { fmt.Printf(printFmt, s, s) } if i, err := parth.SubSegToInt(r.URL.Path, "key"); err == nil { fmt.Printf(printFmt, i, i) } if s, err := parth.SubSpanToString(r.URL.Path, "zero", 2); err == nil { fmt.Printf(printFmt, s, s) } }
Output: Type = string, Value = zero Type = bool, Value = true Type = int, Value = 2 Type = float32, Value = 3.3 Type = string, Value = /zero/1/2 Type = int, Value = 5 Type = string, Value = /1/2
Example (ParthType) ¶
package main import ( "fmt" "net/http" "os" "github.com/codemodus/parth" ) func main() { r, err := http.NewRequest("GET", "/zero/1/2/nn3.3nn/key/5.5", nil) if err != nil { fmt.Fprintln(os.Stderr, err) } printFmt := "Type = %T, Value = %v\n" p := parth.New(r.URL.Path) s := p.SegmentToString(0) f := p.SegmentToFloat32(3) ss := p.SubSpanToString("zero", 2) if p.Err() == nil { fmt.Printf(printFmt, s, s) fmt.Printf(printFmt, f, f) fmt.Printf(printFmt, ss, ss) } }
Output: Type = string, Value = zero Type = float32, Value = 3.3 Type = string, Value = /1/2
Index ¶
- Variables
- func SegmentToBool(path string, i int) (bool, error)
- func SegmentToFloat32(path string, i int) (float32, error)
- func SegmentToFloat64(path string, i int) (float64, error)
- func SegmentToInt(path string, i int) (int, error)
- func SegmentToInt16(path string, i int) (int16, error)
- func SegmentToInt32(path string, i int) (int32, error)
- func SegmentToInt64(path string, i int) (int64, error)
- func SegmentToInt8(path string, i int) (int8, error)
- func SegmentToString(path string, i int) (string, error)
- func SegmentToUint(path string, i int) (uint, error)
- func SegmentToUint16(path string, i int) (uint16, error)
- func SegmentToUint32(path string, i int) (uint32, error)
- func SegmentToUint64(path string, i int) (uint64, error)
- func SegmentToUint8(path string, i int) (uint8, error)
- func SpanToString(path string, firstSeg, lastSeg int) (string, error)
- func SubSegToBool(path, key string) (bool, error)
- func SubSegToFloat32(path, key string) (float32, error)
- func SubSegToFloat64(path, key string) (float64, error)
- func SubSegToInt(path, key string) (int, error)
- func SubSegToInt16(path, key string) (int16, error)
- func SubSegToInt32(path, key string) (int32, error)
- func SubSegToInt64(path, key string) (int64, error)
- func SubSegToInt8(path, key string) (int8, error)
- func SubSegToString(path, key string) (string, error)
- func SubSegToUint(path, key string) (uint, error)
- func SubSegToUint16(path, key string) (uint16, error)
- func SubSegToUint32(path, key string) (uint32, error)
- func SubSegToUint64(path, key string) (uint64, error)
- func SubSegToUint8(path, key string) (uint8, error)
- func SubSpanToString(path, key string, lastSeg int) (string, error)
- type Parth
- func (p *Parth) Err() error
- func (p *Parth) SegmentToBool(i int) bool
- func (p *Parth) SegmentToFloat32(i int) float32
- func (p *Parth) SegmentToFloat64(i int) float64
- func (p *Parth) SegmentToInt(i int) int
- func (p *Parth) SegmentToInt16(i int) int16
- func (p *Parth) SegmentToInt32(i int) int32
- func (p *Parth) SegmentToInt64(i int) int64
- func (p *Parth) SegmentToInt8(i int) int8
- func (p *Parth) SegmentToString(i int) string
- func (p *Parth) SegmentToUint(i int) uint
- func (p *Parth) SegmentToUint16(i int) uint16
- func (p *Parth) SegmentToUint32(i int) uint32
- func (p *Parth) SegmentToUint64(i int) uint64
- func (p *Parth) SegmentToUint8(i int) uint8
- func (p *Parth) SpanToString(firstSeg, lastSeg int) string
- func (p *Parth) SubSegToBool(key string) bool
- func (p *Parth) SubSegToFloat32(key string) float32
- func (p *Parth) SubSegToFloat64(key string) float64
- func (p *Parth) SubSegToInt(key string) int
- func (p *Parth) SubSegToInt16(key string) int16
- func (p *Parth) SubSegToInt32(key string) int32
- func (p *Parth) SubSegToInt64(key string) int64
- func (p *Parth) SubSegToInt8(key string) int8
- func (p *Parth) SubSegToString(key string) string
- func (p *Parth) SubSegToUint(key string) uint
- func (p *Parth) SubSegToUint16(key string) uint16
- func (p *Parth) SubSegToUint32(key string) uint32
- func (p *Parth) SubSegToUint64(key string) uint64
- func (p *Parth) SubSegToUint8(key string) uint8
- func (p *Parth) SubSpanToString(key string, lastSeg int) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrFirstSegNotExist = errors.New("first segment index does not exist") ErrLastSegNotExist = errors.New("last segment index does not exist") ErrSegOrderReversed = errors.New("first segment must precede last segment") ErrSegNotExist = errors.New("segment index does not exist") ErrIntNotFound = errors.New("segment does not contain int") ErrFloatNotFound = errors.New("segment does not contain float") ErrUnparsable = errors.New("unable to parse segment") ErrIndexBad = errors.New("index cannot be found") ErrIndexNotFound = errors.New("no index found") )
Err{Name} values are for error identification.
Functions ¶
func SegmentToBool ¶
SegmentToBool receives an int representing a path segment, then returns both the specified segment as a bool and a nil error. If any error is encountered, a zero value bool and error are returned.
func SegmentToFloat32 ¶
SegmentToFloat32 receives an int representing a path segment, then returns both the specified segment as a float32 and a nil error. If any error is encountered, a zero value float32 and error are returned.
func SegmentToFloat64 ¶
SegmentToFloat64 receives an int representing a path segment, then returns both the specified segment as a float64 and a nil error. If any error is encountered, a zero value float64 and error are returned.
func SegmentToInt ¶
SegmentToInt receives an int representing a path segment, then returns both the specified segment as an int and a nil error. If any error is encountered, a zero value int and error are returned.
func SegmentToInt16 ¶
SegmentToInt16 receives an int representing a path segment, then returns both the specified segment as an int16 and a nil error. If any error is encountered, a zero value int16 and error are returned.
func SegmentToInt32 ¶
SegmentToInt32 receives an int representing a path segment, then returns both the specified segment as an int32 and a nil error. If any error is encountered, a zero value int32 and error are returned.
func SegmentToInt64 ¶
SegmentToInt64 receives an int representing a path segment, then returns both the specified segment as an int64 and a nil error. If any error is encountered, a zero value int64 and error are returned.
func SegmentToInt8 ¶
SegmentToInt8 receives an int representing a path segment, then returns both the specified segment as an int8 and a nil error. If any error is encountered, a zero value int8 and error are returned.
func SegmentToString ¶
SegmentToString receives an int representing a path segment, then returns both the specified segment as a string and a nil error. If any error is encountered, a zero value string and error are returned.
func SegmentToUint ¶ added in v1.1.2
SegmentToUint receives an int representing a path segment, then returns both the specified segment as a uint and a nil error. If any error is encountered, a zero value uint and error are returned.
func SegmentToUint16 ¶ added in v1.1.2
SegmentToUint16 receives an int representing a path segment, then returns both the specified segment as a uint16 and a nil error. If any error is encountered, a zero value uint16 and error are returned.
func SegmentToUint32 ¶ added in v1.1.2
SegmentToUint32 receives an int representing a path segment, then returns both the specified segment as a uint32 and a nil error. If any error is encountered, a zero value uint32 and error are returned.
func SegmentToUint64 ¶ added in v1.1.2
SegmentToUint64 receives an int representing a path segment, then returns both the specified segment as a uint64 and a nil error. If any error is encountered, a zero value uint64 and error are returned.
func SegmentToUint8 ¶ added in v1.1.2
SegmentToUint8 receives an int representing a path segment, then returns both the specified segment as a uint8 and a nil error. If any error is encountered, a zero value uint8 and error are returned.
func SpanToString ¶
SpanToString receives two int values representing path segments, and returns the content between those segments, including the first segment, as a string and a nil error. If any error is encountered, a zero value string and error are returned. The segments can be of negative values, but the first segment must come before the last segment. Providing a 0 int for the second int is a special case which indicates the end of the path.
func SubSegToBool ¶
SubSegToBool receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as a bool and a nil error. If any error is encountered, a zero value bool and error are returned.
func SubSegToFloat32 ¶
SubSegToFloat32 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as a float32 and a nil error. If any error is encountered, a zero value float32 and error are returned.
func SubSegToFloat64 ¶
SubSegToFloat64 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as a float64 and a nil error. If any error is encountered, a zero value float64 and error are returned.
func SubSegToInt ¶
SubSegToInt receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as an int and a nil error. If any error is encountered, a zero value int and error are returned.
func SubSegToInt16 ¶
SubSegToInt16 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as an int16 and a nil error. If any error is encountered, a zero value int16 and error are returned.
func SubSegToInt32 ¶
SubSegToInt32 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as an int32 and a nil error. If any error is encountered, a zero value int32 and error are returned.
func SubSegToInt64 ¶
SubSegToInt64 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as an int64 and a nil error. If any error is encountered, a zero value int64 and error are returned.
func SubSegToInt8 ¶
SubSegToInt8 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as an int8 and a nil error. If any error is encountered, a zero value int8 and error are returned.
func SubSegToString ¶
SubSegToString receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as a string and a nil error. If any error is encountered, a zero value string and error are returned.
func SubSegToUint ¶ added in v1.1.2
SubSegToUint receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as a uint and a nil error. If any error is encountered, a zero value uint and error are returned.
func SubSegToUint16 ¶ added in v1.1.2
SubSegToUint16 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as a uint16 and a nil error. If any error is encountered, a zero value uint16 and error are returned.
func SubSegToUint32 ¶ added in v1.1.2
SubSegToUint32 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as a uint32 and a nil error. If any error is encountered, a zero value uint32 and error are returned.
func SubSegToUint64 ¶ added in v1.1.2
SubSegToUint64 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as a uint64 and a nil error. If any error is encountered, a zero value uint64 and error are returned.
func SubSegToUint8 ¶ added in v1.1.2
SubSegToUint8 receives a key which is used to search for the first matching path segment, then returns both the subsequent segment as a uint8 and a nil error. If any error is encountered, a zero value uint8 and error are returned.
func SubSpanToString ¶
SubSpanToString receives a key which is used to search for the first matching path segment and an int value representing a second segment by it's distance from the matched segment, then returns the content between those segments as a string and a nil error. If any error is encountered, a zero value string and error are returned. The int representing a segment can be of negative values. Providing a 0 int is a special case which indicates the end of the path.
Types ¶
type Parth ¶ added in v1.1.0
type Parth struct {
// contains filtered or unexported fields
}
Parth holds path and error data for processing paths multiple times, and then checking for errors only once. Only the first encountered error will be returned.
func New ¶ added in v1.1.0
New receives a path as a string, then returns a new Parth set with the provided path.
func NewFromSpan ¶ added in v1.1.0
NewFromSpan receives a path as a string, and two int values representing path segments, then returns a new Parth set with the content between those segments, and any error encountered. See SpanToString for more info.
func NewFromSubSpan ¶ added in v1.1.0
NewFromSubSpan receives a path as a string, a key which is used to search for the first matching path segment, and an int value representing a second segment by it's distance from the matched segment, then returns a new Parth set with the content between those segments, and any error encountered. See SubSpanToString for more info.
func (*Parth) SegmentToBool ¶ added in v1.1.0
SegmentToBool receives an int representing a path segment, then returns the specified segment as a bool. If any error is encountered, a zero value bool is returned, and the Parth instance's err value is set. If an error has already been set, a zero value bool is returned.
func (*Parth) SegmentToFloat32 ¶ added in v1.1.0
SegmentToFloat32 receives an int representing a path segment, then returns the specified segment as a float32. If any error is encountered, a zero value float32 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value float32 is returned.
func (*Parth) SegmentToFloat64 ¶ added in v1.1.0
SegmentToFloat64 receives an int representing a path segment, then returns the specified segment as a float64. If any error is encountered, a zero value float64 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value float64 is returned.
func (*Parth) SegmentToInt ¶ added in v1.1.0
SegmentToInt receives an int representing a path segment, then returns the specified segment as an int. If any error is encountered, a zero value int is returned, and the Parth instance's err value is set. If an error has already been set, a zero value int is returned.
func (*Parth) SegmentToInt16 ¶ added in v1.1.0
SegmentToInt16 receives an int representing a path segment, then returns the specified segment as an int16. If any error is encountered, a zero value int16 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value int16 is returned.
func (*Parth) SegmentToInt32 ¶ added in v1.1.0
SegmentToInt32 receives an int representing a path segment, then returns the specified segment as an int32. If any error is encountered, a zero value int32 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value int32 is returned.
func (*Parth) SegmentToInt64 ¶ added in v1.1.0
SegmentToInt64 receives an int representing a path segment, then returns the specified segment as an int64. If any error is encountered, a zero value int64 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value int64 is returned.
func (*Parth) SegmentToInt8 ¶ added in v1.1.0
SegmentToInt8 receives an int representing a path segment, then returns the specified segment as an int8. If any error is encountered, a zero value int8 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value int8 is returned.
func (*Parth) SegmentToString ¶ added in v1.1.0
SegmentToString receives an int representing a path segment, then returns the specified segment as a string. If any error is encountered, a zero value string is returned, and the Parth instance's err value is set. If an error has already been set, a zero value string is returned.
func (*Parth) SegmentToUint ¶ added in v1.1.2
SegmentToUint receives an int representing a path segment, then returns the specified segment as a uint. If any error is encountered, a zero value uint is returned, and the Parth instance's err value is set. If an error has already been set, a zero value uint is returned.
func (*Parth) SegmentToUint16 ¶ added in v1.1.2
SegmentToUint16 receives an int representing a path segment, then returns the specified segment as a uint16. If any error is encountered, a zero value uint16 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value uint16 is returned.
func (*Parth) SegmentToUint32 ¶ added in v1.1.2
SegmentToUint32 receives an int representing a path segment, then returns the specified segment as a uint32. If any error is encountered, a zero value uint32 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value uint32 is returned.
func (*Parth) SegmentToUint64 ¶ added in v1.1.2
SegmentToUint64 receives an int representing a path segment, then returns the specified segment as a uint64. If any error is encountered, a zero value uint64 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value uint64 is returned.
func (*Parth) SegmentToUint8 ¶ added in v1.1.2
SegmentToUint8 receives an int representing a path segment, then returns the specified segment as a uint8. If any error is encountered, a zero value uint8 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value uint8 is returned.
func (*Parth) SpanToString ¶ added in v1.1.0
SpanToString receives two int values representing path segments, then returns the content between those segments, including the first segment, as a string. If any error is encountered, a zero value string is returned, and the Parth instance's err value is set. If an error has already been set, a zero value string is returned. See SpanToString for more info.
func (*Parth) SubSegToBool ¶ added in v1.1.0
SubSegToBool receives a key which is used to search for the first matching path segment, then returns the subsequent segment as an bool. If any error is encountered, a zero value bool is returned, and the Parth instances err value is set. If an error has already been set, a zero value bool is returned.
func (*Parth) SubSegToFloat32 ¶ added in v1.1.0
SubSegToFloat32 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as an float32. If any error is encountered, a zero value float32 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value float32 is returned.
func (*Parth) SubSegToFloat64 ¶ added in v1.1.0
SubSegToFloat64 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as an float64. If any error is encountered, a zero value float64 is returned, and the Parth instance's err value is set. If an error has already been set, a zero value float64 is returned.
func (*Parth) SubSegToInt ¶ added in v1.1.0
SubSegToInt receives a key which is used to search for the first matching path segment, then returns the subsequent segment as an int. If any error is encountered, a zero value int8 is returned, and the Parth instances err value is set. If an error has already been set, a zero value int8 is returned.
func (*Parth) SubSegToInt16 ¶ added in v1.1.0
SubSegToInt16 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as an int16. If any error is encountered, a zero value int16 is returned, and the Parth instances err value is set. If an error has already been set, a zero value int16 is returned.
func (*Parth) SubSegToInt32 ¶ added in v1.1.0
SubSegToInt32 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as an int32. If any error is encountered, a zero value int32 is returned, and the Parth instances err value is set. If an error has already been set, a zero value int32 is returned.
func (*Parth) SubSegToInt64 ¶ added in v1.1.0
SubSegToInt64 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as an int64. If any error is encountered, a zero value int64 is returned, and the Parth instances err value is set. If an error has already been set, a zero value int64 is returned.
func (*Parth) SubSegToInt8 ¶ added in v1.1.0
SubSegToInt8 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as an int8. If any error is encountered, a zero value int8 is returned, and the Parth instances err value is set. If an error has already been set, a zero value int8 is returned.
func (*Parth) SubSegToString ¶ added in v1.1.0
SubSegToString receives a key which is used to search for the first matching path segment, then returns the subsequent segment as a string. If any error is encountered, a zero value string is returned, and the Parth instances err value is set. If an error has already been set, a zero value string is returned.
func (*Parth) SubSegToUint ¶ added in v1.1.2
SubSegToUint receives a key which is used to search for the first matching path segment, then returns the subsequent segment as a uint. If any error is encountered, a zero value uint is returned, and the Parth instances err value is set. If an error has already been set, a zero value uint is returned.
func (*Parth) SubSegToUint16 ¶ added in v1.1.2
SubSegToUint16 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as a uint16. If any error is encountered, a zero value uint16 is returned, and the Parth instances err value is set. If an error has already been set, a zero value uint16 is returned.
func (*Parth) SubSegToUint32 ¶ added in v1.1.2
SubSegToUint32 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as a uint32. If any error is encountered, a zero value uint32 is returned, and the Parth instances err value is set. If an error has already been set, a zero value uint32 is returned.
func (*Parth) SubSegToUint64 ¶ added in v1.1.2
SubSegToUint64 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as a uint64. If any error is encountered, a zero value uint64 is returned, and the Parth instances err value is set. If an error has already been set, a zero value uint64 is returned.
func (*Parth) SubSegToUint8 ¶ added in v1.1.2
SubSegToUint8 receives a key which is used to search for the first matching path segment, then returns the subsequent segment as a uint8. If any error is encountered, a zero value uint8 is returned, and the Parth instances err value is set. If an error has already been set, a zero value uint8 is returned.
func (*Parth) SubSpanToString ¶ added in v1.1.0
SubSpanToString receives a key which is used to search for the first matching path segment, and an int value representing a second segment by it's distance from the matched segment, then returns the content between those segments as a string. If an error has already been set, a zero value string is returned. See SubSpanToString for more info.