Skip to content

Commit 1499ace

Browse files
committed
Update to use new generic API
1 parent 4846a6a commit 1499ace

File tree

7 files changed

+64
-92
lines changed

7 files changed

+64
-92
lines changed

ip_addr.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020 Walter Scheper <[email protected]>
1+
// Copyright 2022 Walter Scheper <[email protected]>
22
//
33
// This Source Code Form is subject to the terms of the Mozilla Public
44
// License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -9,7 +9,6 @@ package rapid
99
import (
1010
"fmt"
1111
"net"
12-
"reflect"
1312
)
1413

1514
const (
@@ -18,8 +17,6 @@ const (
1817
)
1918

2019
var (
21-
ipType = reflect.TypeOf(net.IP{})
22-
2320
ipv4Gen = SliceOfN(Byte(), ipv4Len, ipv4Len)
2421
ipv6Gen = SliceOfN(Byte(), ipv6Len, ipv6Len)
2522
)
@@ -32,30 +29,26 @@ func (g *ipGen) String() string {
3229
return fmt.Sprintf("IP(ipv6=%v)", g.ipv6)
3330
}
3431

35-
func (*ipGen) type_() reflect.Type {
36-
return ipType
37-
}
38-
39-
func (g *ipGen) value(t *T) value {
40-
var gen *Generator
32+
func (g *ipGen) value(t *T) net.IP {
33+
var gen *Generator[[]byte]
4134
if g.ipv6 {
4235
gen = ipv6Gen
4336
} else {
4437
gen = ipv4Gen
4538
}
4639

47-
b := gen.Draw(t, g.String()).([]byte)
40+
b := gen.Draw(t, g.String())
4841
return net.IP(b)
4942
}
5043

51-
func IPv4() *Generator {
52-
return newGenerator(&ipGen{
44+
func IPv4() *Generator[net.IP] {
45+
return newGenerator[net.IP](&ipGen{
5346
ipv6: false,
5447
})
5548
}
5649

57-
func IPv6() *Generator {
58-
return newGenerator(&ipGen{
50+
func IPv6() *Generator[net.IP] {
51+
return newGenerator[net.IP](&ipGen{
5952
ipv6: true,
6053
})
6154
}

ip_addr_example_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package rapid_test
88

99
import (
1010
"fmt"
11-
"net"
1211

1312
"pgregory.net/rapid"
1413
)
@@ -17,7 +16,7 @@ func ExampleIPv4() {
1716
gen := rapid.IPv4()
1817

1918
for i := 0; i < 5; i++ {
20-
addr := gen.Example(i).(net.IP)
19+
addr := gen.Example(i)
2120
fmt.Println(addr.String())
2221
}
2322

@@ -33,7 +32,7 @@ func ExampleIPv6() {
3332
gen := rapid.IPv6()
3433

3534
for i := 0; i < 5; i++ {
36-
addr := gen.Example(i).(net.IP)
35+
addr := gen.Example(i)
3736
fmt.Println(addr.String())
3837
}
3938

tld.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package rapid
99
import "strings"
1010

1111
// sourced from https://data.iana.org/TLD/tlds-alpha-by-domain.txt
12-
// Version 2021090100, Last Updated Thu Sep 2 02:57:25 UTC 2021
12+
// Version 2022092400, Last Updated Sun Sep 25 02:07:59 UTC 2022
1313
const tldsByAlpha = `
1414
AAA
1515
AARP
@@ -37,7 +37,6 @@ AEG
3737
AERO
3838
AETNA
3939
AF
40-
AFAMILYCOMPANY
4140
AFL
4241
AFRICA
4342
AG
@@ -189,7 +188,6 @@ BROTHER
189188
BRUSSELS
190189
BS
191190
BT
192-
BUDAPEST
193191
BUGATTI
194192
BUILD
195193
BUILDERS
@@ -313,7 +311,6 @@ CROWN
313311
CRS
314312
CRUISE
315313
CRUISES
316-
CSC
317314
CU
318315
CUISINELLA
319316
CV
@@ -372,7 +369,6 @@ DOWNLOAD
372369
DRIVE
373370
DTV
374371
DUBAI
375-
DUCK
376372
DUNLOP
377373
DUPONT
378374
DURBAN
@@ -506,7 +502,6 @@ GIFTS
506502
GIVES
507503
GIVING
508504
GL
509-
GLADE
510505
GLASS
511506
GLE
512507
GLOBAL
@@ -670,6 +665,7 @@ KG
670665
KH
671666
KI
672667
KIA
668+
KIDS
673669
KIM
674670
KINDER
675671
KINDLE
@@ -732,7 +728,6 @@ LINK
732728
LIPSY
733729
LIVE
734730
LIVING
735-
LIXIL
736731
LK
737732
LLC
738733
LLP
@@ -833,6 +828,7 @@ MTN
833828
MTR
834829
MU
835830
MUSEUM
831+
MUSIC
836832
MUTUAL
837833
MV
838834
MW
@@ -889,7 +885,6 @@ NYC
889885
NZ
890886
OBI
891887
OBSERVER
892-
OFF
893888
OFFICE
894889
OKINAWA
895890
OLAYAN
@@ -988,10 +983,8 @@ QA
988983
QPON
989984
QUEBEC
990985
QUEST
991-
QVC
992986
RACING
993987
RADIO
994-
RAID
995988
RE
996989
READ
997990
REALESTATE
@@ -1023,7 +1016,6 @@ RICOH
10231016
RIL
10241017
RIO
10251018
RIP
1026-
RMIT
10271019
RO
10281020
ROCHER
10291021
ROCKS
@@ -1069,7 +1061,6 @@ SCHOOL
10691061
SCHULE
10701062
SCHWARZ
10711063
SCIENCE
1072-
SCJOHNSON
10731064
SCOT
10741065
SD
10751066
SE
@@ -1162,7 +1153,6 @@ SURGERY
11621153
SUZUKI
11631154
SV
11641155
SWATCH
1165-
SWIFTCOVER
11661156
SWISS
11671157
SX
11681158
SY
@@ -1341,7 +1331,6 @@ XN--3BST00M
13411331
XN--3DS443G
13421332
XN--3E0B707E
13431333
XN--3HCRJ9C
1344-
XN--3OQ18VL8PN36A
13451334
XN--3PXU8K
13461335
XN--42C2D9A
13471336
XN--45BR5CYL

update_tld.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package rapid
1717
import "strings"
1818
1919
// sourced from https://data.iana.org/TLD/tlds-alpha-by-domain.txt
20-
// Version $(date +%Y%m%d00), Last Updated $(date --utc)
20+
// Version $(date +%Y%m%d00), Last Updated $(date -u)
2121
const tldsByAlpha = \`
2222
${TLD}
2323
\`

url.go

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package rapid
88

99
import (
1010
"fmt"
11-
"net"
1211
"net/url"
1312
"reflect"
1413
"strings"
@@ -31,29 +30,17 @@ func (*domainNameGen) String() string {
3130
return "Domain()"
3231
}
3332

34-
func (*domainNameGen) type_() reflect.Type {
35-
return domainType
36-
}
37-
3833
var tldGenerator = SampledFrom(tlds)
3934

40-
func (g *domainNameGen) value(t *T) value {
35+
func (g *domainNameGen) value(t *T) string {
4136
domain := tldGenerator.
4237
Filter(func(s string) bool { return len(s)+2 <= domainMaxLength }).
43-
Map(func(s string) string {
44-
var n string
45-
for _, ch := range s {
46-
n += string(SampledFrom([]rune{unicode.ToLower(ch), unicode.ToUpper(ch)}).Draw(t, "").(rune))
47-
}
48-
49-
return n
50-
}).
51-
Draw(t, "domain").(string)
38+
Draw(t, "domain")
5239

5340
expr := fmt.Sprintf(`[a-zA-Z]([a-zA-Z0-9\-]{0,%d}[a-zA-Z0-9])?`, domainMaxElementLength-2)
54-
elements := newRepeat(1, 126, 1)
55-
for elements.more(t.s, g.String()) {
56-
subDomain := StringMatching(expr).Draw(t, "subdomain").(string)
41+
elements := newRepeat(1, 126, 1, g.String())
42+
for elements.more(t.s) {
43+
subDomain := StringMatching(expr).Draw(t, "subdomain")
5744
if len(domain)+len(subDomain) >= domainMaxLength {
5845
break
5946
}
@@ -64,8 +51,8 @@ func (g *domainNameGen) value(t *T) value {
6451
}
6552

6653
// Domain generates an RFC 1035 compliant domain name.
67-
func Domain() *Generator {
68-
return newGenerator(&domainNameGen{})
54+
func Domain() *Generator[string] {
55+
return newGenerator[string](&domainNameGen{})
6956
}
7057

7158
type urlGenerator struct {
@@ -76,38 +63,42 @@ func (g *urlGenerator) String() string {
7663
return "URL()"
7764
}
7865

79-
func (g *urlGenerator) type_() reflect.Type {
80-
return urlType
81-
}
82-
8366
var printableGen = StringOf(RuneFrom(nil, unicode.PrintRanges...))
8467

85-
func (g *urlGenerator) value(t *T) value {
86-
scheme := SampledFrom(g.schemes).Draw(t, "scheme").(string)
68+
func (g *urlGenerator) value(t *T) url.URL {
69+
scheme := SampledFrom(g.schemes).Draw(t, "scheme")
8770
var domain string
88-
switch SampledFrom([]int{0, 1, 2}).Draw(t, "g").(int) {
71+
switch SampledFrom([]int{0, 1, 2}).Draw(t, "g") {
8972
case 2:
90-
domain = Domain().Draw(t, "domain").(string)
73+
domain = Domain().Draw(t, "domain")
9174
case 1:
92-
domain = IPv6().Draw(t, "domain").(net.IP).String()
75+
domain = IPv6().Draw(t, "domain").String()
9376
domain = "[" + domain + "]"
9477
case 0:
95-
domain = IPv4().Draw(t, "domain").(net.IP).String()
78+
domain = IPv4().Draw(t, "domain").String()
79+
}
80+
port := IntRange(0, 2^16-1).Draw(t, "port")
81+
path_ := SliceOf(printableGen).Draw(t, "path")
82+
query := SliceOf(printableGen).Draw(t, "query")
83+
fragment := printableGen.Draw(t, "fragment")
84+
85+
// join domain and port
86+
if port > 0 {
87+
domain += fmt.Sprintf(":%d", port)
88+
}
89+
90+
// URL escape path
91+
for i := range path_ {
92+
path_[i] = url.PathEscape(path_[i])
93+
}
94+
95+
// url escape query strings
96+
for i := range query {
97+
query[i] = url.QueryEscape(query[i])
9698
}
97-
port := IntRange(0, 2^16-1).
98-
Map(func(i int) string {
99-
if i == 0 {
100-
return ""
101-
}
102-
return fmt.Sprintf(":%d", i)
103-
}).
104-
Draw(t, "port").(string)
105-
path_ := SliceOf(printableGen).Draw(t, "path").([]string)
106-
query := SliceOf(printableGen.Map(url.QueryEscape)).Draw(t, "query").([]string)
107-
fragment := printableGen.Draw(t, "fragment").(string)
10899

109100
return url.URL{
110-
Host: domain + port,
101+
Host: domain,
111102
Path: strings.Join(path_, "/"),
112103
Scheme: scheme,
113104
RawQuery: strings.Join(query, "&"),
@@ -116,12 +107,12 @@ func (g *urlGenerator) value(t *T) value {
116107
}
117108

118109
// URL generates RFC 3986 compliant http/https URLs.
119-
func URL() *Generator {
110+
func URL() *Generator[url.URL] {
120111
return urlOf([]string{"http", "https"})
121112
}
122113

123-
func urlOf(schemes []string) *Generator {
124-
return newGenerator(&urlGenerator{
114+
func urlOf(schemes []string) *Generator[url.URL] {
115+
return newGenerator[url.URL](&urlGenerator{
125116
schemes: schemes,
126117
})
127118
}

0 commit comments

Comments
 (0)