Documentation
¶
Overview ¶
Package parth provides path parsing for segment slicing and unmarshaling. In other words, parth provides simple and flexible access to (URL) path parameters.
Valid values are:
- builtin: *string, *bool, *int, *int64, *int32, *int16, *int8, *uint, *uint64, *uint32, *uint16, *uint8, *float64, *float32
- stdlib: *time.Duration, encoding.TextUnmarshaler, flag.Value
When handling any size of int, uint, or float, the first valid value within the specified segment will be used. Three important terms used in this package are "segment", "sequent", and "span". A segment is any single path section. A sequent is a segment that follows a "key" path section. Segments are able to be unmarshaled into variables. A span is any multiple path sections, and is handled as a string.
Example ¶
package main import ( "fmt" "net/http" "github.com/daved/parth" ) var req, _ = http.NewRequest("GET", "/zero/1/2/key/nn4.4nn/5.5", nil) func main() { var segFour string if err := parth.Segment(&segFour, req.URL.Path, 4); err != nil { fmt.Println(err) } fmt.Printf("%[1]v (%[1]T)\n", segFour) }
Output: nn4.4nn (string)
Example (EncodingTextUnmarshaler) ¶
package main import ( "fmt" "net/http" "github.com/daved/parth" ) var req, _ = http.NewRequest("GET", "/zero/1/2/key/nn4.4nn/5.5", nil) type MyType []byte // UnmarshalText implements encoding.TextUnmarshaler. Let's pretend something // interesting is actually happening here. func (m *MyType) UnmarshalText(text []byte) error { *m = text return nil } func main() { var m MyType if err := parth.Segment(&m, req.URL.Path, 4); err != nil { fmt.Println(err) } fmt.Printf("%[1]v == %[1]q (%[1]T)\n", m) }
Output: [110 110 52 46 52 110 110] == "nn4.4nn" (parth_test.MyType)
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnknownType = errors.New("unknown type provided") ErrFirstSegNotFound = errors.New("first segment not found by index") ErrLastSegNotFound = errors.New("last segment not found by index") ErrSegOrderReversed = errors.New("first segment must precede last segment") ErrKeySegNotFound = errors.New("segment not found by key") ErrDataUnparsable = errors.New("data cannot be parsed") )
Err{Name} values facilitate error identification.
Functions ¶
func Segment ¶
Segment locates the path segment indicated by index i. If the index is negative, the negative count begins with the last segment.
Example ¶
package main import ( "fmt" "net/http" "github.com/daved/parth" ) var req, _ = http.NewRequest("GET", "/zero/1/2/key/nn4.4nn/5.5", nil) func main() { var segFour string if err := parth.Segment(&segFour, req.URL.Path, 4); err != nil { fmt.Println(err) } fmt.Printf("%[1]v (%[1]T)\n", segFour) }
Output: nn4.4nn (string)
func Sequent ¶
Sequent is similar to Segment, except that it locates the segment that is subsequent to the "key" segment.
Example ¶
package main import ( "fmt" "net/http" "github.com/daved/parth" ) var req, _ = http.NewRequest("GET", "/zero/1/2/key/nn4.4nn/5.5", nil) func main() { var afterKey float32 if err := parth.Sequent(&afterKey, req.URL.Path, "key"); err != nil { fmt.Println(err) } fmt.Printf("%[1]v (%[1]T)\n", afterKey) }
Output: 4.4 (float32)
func Span ¶
Span returns the path segments between indexes i and j, including the segment indicated by index i. If an index is negative, the negative count begins with the last segment. Providing a 0 for index j is a special case which acts as an alias for the end of the path. If the first segment does not begin with a slash and it is part of the requested span, no slash will be added. Index i must not precede index j.
Example ¶
package main import ( "fmt" "net/http" "github.com/daved/parth" ) var req, _ = http.NewRequest("GET", "/zero/1/2/key/nn4.4nn/5.5", nil) func main() { span, err := parth.Span(req.URL.Path, 2, 4) if err != nil { fmt.Println(err) } fmt.Println(span) }
Output: /2/key
func SubSeg ¶
SubSeg is similar to both Sequent and Segment. It first locates the "key", then uses index i to locate a segment. For example, to access the segment immediately after the "key", an index of 0 should be provided (which is how Sequent is implemented). Technically, a negative index is valid, but it is nonsensical in this function.
Example ¶
package main import ( "fmt" "net/http" "github.com/daved/parth" ) var req, _ = http.NewRequest("GET", "/zero/1/2/key/nn4.4nn/5.5", nil) func main() { var twoAfterKey float64 if err := parth.SubSeg(&twoAfterKey, req.URL.Path, "key", 1); err != nil { fmt.Println(err) } fmt.Printf("%[1]v (%[1]T)\n", twoAfterKey) }
Output: 5.5 (float64)
func SubSpan ¶
SubSpan is similar to Span, but only handles the portion of the path subsequent to the "key".
Example ¶
package main import ( "fmt" "net/http" "github.com/daved/parth" ) var req, _ = http.NewRequest("GET", "/zero/1/2/key/nn4.4nn/5.5", nil) func main() { subSpanZero, err := parth.SubSpan(req.URL.Path, "zero", 2, 4) if err != nil { fmt.Println(err) } subSpanOne, err := parth.SubSpan(req.URL.Path, "1", 1, 3) if err != nil { fmt.Println(err) } fmt.Println(subSpanZero) fmt.Println(subSpanOne) }
Output: /key/nn4.4nn /key/nn4.4nn
Types ¶
type Parth ¶
type Parth struct {
// contains filtered or unexported fields
}
Parth manages path and error data for processing a single path multiple times while handling errors only once. Only the first encountered error is stored since all subsequent calls to Parth methods will have no effect.
Example ¶
package main import ( "fmt" "net/http" "github.com/daved/parth" ) var req, _ = http.NewRequest("GET", "/zero/1/2/key/nn4.4nn/5.5", nil) func main() { var segZero string var twoAfterKey float32 p := parth.New(req.URL.Path) p.Segment(&segZero, 0) p.SubSeg(&twoAfterKey, "key", 1) if err := p.Err(); err != nil { fmt.Println(err) } fmt.Printf("%[1]v (%[1]T)\n", segZero) fmt.Printf("%[1]v (%[1]T)\n", twoAfterKey) }
Output: zero (string) 5.5 (float32)
func NewBySpan ¶
NewBySpan constructs a pointer to an instance of Parth after preprocessing the provided path with Span.
func NewBySubSpan ¶
NewBySubSpan constructs a pointer to an instance of Parth after preprocessing the provided path with SubSpan.