File: example_test.go

package info (click to toggle)
golang-github-h2non-parth 2.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 128 kB
  • sloc: makefile: 2
file content (161 lines) | stat: -rw-r--r-- 2,693 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package parth_test

import (
	"fmt"
	"net/http"
	"os"

	"github.com/h2non/parth"
)

var (
	r, rErr = http.NewRequest("GET", "/zero/1/2/key/nn4.4nn/5.5", nil)
)

func init() {
	if rErr != nil {
		panic(rErr)
	}
}

func Example() {
	var s string
	if err := parth.Segment(r.URL.Path, 4, &s); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	fmt.Println(r.URL.Path)
	fmt.Printf("%v (%T)\n", s, s)

	// Output:
	// /zero/1/2/key/nn4.4nn/5.5
	// nn4.4nn (string)
}

func ExampleSegment() {
	var s string
	if err := parth.Segment(r.URL.Path, 4, &s); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	fmt.Println(r.URL.Path)
	fmt.Printf("%v (%T)\n", s, s)

	// Output:
	// /zero/1/2/key/nn4.4nn/5.5
	// nn4.4nn (string)
}

func ExampleSequent() {
	var f float32
	if err := parth.Sequent(r.URL.Path, "key", &f); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	fmt.Println(r.URL.Path)
	fmt.Printf("%v (%T)\n", f, f)

	// Output:
	// /zero/1/2/key/nn4.4nn/5.5
	// 4.4 (float32)
}

func ExampleSpan() {
	s, err := parth.Span(r.URL.Path, 2, 4)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	fmt.Println(r.URL.Path)
	fmt.Println(s)

	// Output:
	// /zero/1/2/key/nn4.4nn/5.5
	// /2/key
}

func ExampleSubSeg() {
	var f float64
	if err := parth.SubSeg(r.URL.Path, "key", 1, &f); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	fmt.Println(r.URL.Path)
	fmt.Printf("%v (%T)\n", f, f)

	// Output:
	// /zero/1/2/key/nn4.4nn/5.5
	// 5.5 (float64)
}

func ExampleSubSpan() {
	s0, err := parth.SubSpan(r.URL.Path, "zero", 2, 4)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	s1, err := parth.SubSpan(r.URL.Path, "1", 1, 3)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	fmt.Println(r.URL.Path)
	fmt.Println(s0)
	fmt.Println(s1)

	// Output:
	// /zero/1/2/key/nn4.4nn/5.5
	// /key/nn4.4nn
	// /key/nn4.4nn
}

func ExampleParth() {
	var s string
	var f float32

	p := parth.New(r.URL.Path)
	p.Segment(0, &s)
	p.SubSeg("key", 1, &f)
	if err := p.Err(); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	fmt.Println(r.URL.Path)
	fmt.Printf("%v (%T)\n", s, s)
	fmt.Printf("%v (%T)\n", f, f)

	// Output:
	// /zero/1/2/key/nn4.4nn/5.5
	// zero (string)
	// 5.5 (float32)
}

func ExampleUnmarshaler() {
	/*
		type mytype []byte

		func (m *mytype) UnmarshalSegment(seg string) error {
			*m = []byte(seg)
		}
	*/

	var m mytype

	if err := parth.Segment(r.URL.Path, 4, &m); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	fmt.Println(r.URL.Path)
	fmt.Printf("%v == %q (%T)\n", m, m, m)

	// Output:
	// /zero/1/2/key/nn4.4nn/5.5
	// [110 110 52 46 52 110 110] == "nn4.4nn" (parth_test.mytype)
}

type mytype []byte

func (m *mytype) UnmarshalSegment(seg string) error {
	*m = []byte(seg)
	return nil
}