Skip to content

Commit

Permalink
Update to use new generic API
Browse files Browse the repository at this point in the history
  • Loading branch information
wfscheper committed Sep 25, 2022
1 parent 4846a6a commit 1499ace
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 92 deletions.
23 changes: 8 additions & 15 deletions ip_addr.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Walter Scheper <walter.scheper@gmail.com>
// Copyright 2022 Walter Scheper <walter.scheper@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -9,7 +9,6 @@ package rapid
import (
"fmt"
"net"
"reflect"
)

const (
Expand All @@ -18,8 +17,6 @@ const (
)

var (
ipType = reflect.TypeOf(net.IP{})

ipv4Gen = SliceOfN(Byte(), ipv4Len, ipv4Len)
ipv6Gen = SliceOfN(Byte(), ipv6Len, ipv6Len)
)
Expand All @@ -32,30 +29,26 @@ func (g *ipGen) String() string {
return fmt.Sprintf("IP(ipv6=%v)", g.ipv6)
}

func (*ipGen) type_() reflect.Type {
return ipType
}

func (g *ipGen) value(t *T) value {
var gen *Generator
func (g *ipGen) value(t *T) net.IP {
var gen *Generator[[]byte]
if g.ipv6 {
gen = ipv6Gen
} else {
gen = ipv4Gen
}

b := gen.Draw(t, g.String()).([]byte)
b := gen.Draw(t, g.String())
return net.IP(b)
}

func IPv4() *Generator {
return newGenerator(&ipGen{
func IPv4() *Generator[net.IP] {
return newGenerator[net.IP](&ipGen{
ipv6: false,
})
}

func IPv6() *Generator {
return newGenerator(&ipGen{
func IPv6() *Generator[net.IP] {
return newGenerator[net.IP](&ipGen{
ipv6: true,
})
}
5 changes: 2 additions & 3 deletions ip_addr_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package rapid_test

import (
"fmt"
"net"

"pgregory.net/rapid"
)
Expand All @@ -17,7 +16,7 @@ func ExampleIPv4() {
gen := rapid.IPv4()

for i := 0; i < 5; i++ {
addr := gen.Example(i).(net.IP)
addr := gen.Example(i)
fmt.Println(addr.String())
}

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

for i := 0; i < 5; i++ {
addr := gen.Example(i).(net.IP)
addr := gen.Example(i)
fmt.Println(addr.String())
}

Expand Down
17 changes: 3 additions & 14 deletions tld.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package rapid
import "strings"

// sourced from https://data.iana.org/TLD/tlds-alpha-by-domain.txt
// Version 2021090100, Last Updated Thu Sep 2 02:57:25 UTC 2021
// Version 2022092400, Last Updated Sun Sep 25 02:07:59 UTC 2022
const tldsByAlpha = `
AAA
AARP
Expand Down Expand Up @@ -37,7 +37,6 @@ AEG
AERO
AETNA
AF
AFAMILYCOMPANY
AFL
AFRICA
AG
Expand Down Expand Up @@ -189,7 +188,6 @@ BROTHER
BRUSSELS
BS
BT
BUDAPEST
BUGATTI
BUILD
BUILDERS
Expand Down Expand Up @@ -313,7 +311,6 @@ CROWN
CRS
CRUISE
CRUISES
CSC
CU
CUISINELLA
CV
Expand Down Expand Up @@ -372,7 +369,6 @@ DOWNLOAD
DRIVE
DTV
DUBAI
DUCK
DUNLOP
DUPONT
DURBAN
Expand Down Expand Up @@ -506,7 +502,6 @@ GIFTS
GIVES
GIVING
GL
GLADE
GLASS
GLE
GLOBAL
Expand Down Expand Up @@ -670,6 +665,7 @@ KG
KH
KI
KIA
KIDS
KIM
KINDER
KINDLE
Expand Down Expand Up @@ -732,7 +728,6 @@ LINK
LIPSY
LIVE
LIVING
LIXIL
LK
LLC
LLP
Expand Down Expand Up @@ -833,6 +828,7 @@ MTN
MTR
MU
MUSEUM
MUSIC
MUTUAL
MV
MW
Expand Down Expand Up @@ -889,7 +885,6 @@ NYC
NZ
OBI
OBSERVER
OFF
OFFICE
OKINAWA
OLAYAN
Expand Down Expand Up @@ -988,10 +983,8 @@ QA
QPON
QUEBEC
QUEST
QVC
RACING
RADIO
RAID
RE
READ
REALESTATE
Expand Down Expand Up @@ -1023,7 +1016,6 @@ RICOH
RIL
RIO
RIP
RMIT
RO
ROCHER
ROCKS
Expand Down Expand Up @@ -1069,7 +1061,6 @@ SCHOOL
SCHULE
SCHWARZ
SCIENCE
SCJOHNSON
SCOT
SD
SE
Expand Down Expand Up @@ -1162,7 +1153,6 @@ SURGERY
SUZUKI
SV
SWATCH
SWIFTCOVER
SWISS
SX
SY
Expand Down Expand Up @@ -1341,7 +1331,6 @@ XN--3BST00M
XN--3DS443G
XN--3E0B707E
XN--3HCRJ9C
XN--3OQ18VL8PN36A
XN--3PXU8K
XN--42C2D9A
XN--45BR5CYL
Expand Down
2 changes: 1 addition & 1 deletion update_tld.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package rapid
import "strings"
// sourced from https://data.iana.org/TLD/tlds-alpha-by-domain.txt
// Version $(date +%Y%m%d00), Last Updated $(date --utc)
// Version $(date +%Y%m%d00), Last Updated $(date -u)
const tldsByAlpha = \`
${TLD}
\`
Expand Down
81 changes: 36 additions & 45 deletions url.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package rapid

import (
"fmt"
"net"
"net/url"
"reflect"
"strings"
Expand All @@ -31,29 +30,17 @@ func (*domainNameGen) String() string {
return "Domain()"
}

func (*domainNameGen) type_() reflect.Type {
return domainType
}

var tldGenerator = SampledFrom(tlds)

func (g *domainNameGen) value(t *T) value {
func (g *domainNameGen) value(t *T) string {
domain := tldGenerator.
Filter(func(s string) bool { return len(s)+2 <= domainMaxLength }).
Map(func(s string) string {
var n string
for _, ch := range s {
n += string(SampledFrom([]rune{unicode.ToLower(ch), unicode.ToUpper(ch)}).Draw(t, "").(rune))
}

return n
}).
Draw(t, "domain").(string)
Draw(t, "domain")

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

// Domain generates an RFC 1035 compliant domain name.
func Domain() *Generator {
return newGenerator(&domainNameGen{})
func Domain() *Generator[string] {
return newGenerator[string](&domainNameGen{})
}

type urlGenerator struct {
Expand All @@ -76,38 +63,42 @@ func (g *urlGenerator) String() string {
return "URL()"
}

func (g *urlGenerator) type_() reflect.Type {
return urlType
}

var printableGen = StringOf(RuneFrom(nil, unicode.PrintRanges...))

func (g *urlGenerator) value(t *T) value {
scheme := SampledFrom(g.schemes).Draw(t, "scheme").(string)
func (g *urlGenerator) value(t *T) url.URL {
scheme := SampledFrom(g.schemes).Draw(t, "scheme")
var domain string
switch SampledFrom([]int{0, 1, 2}).Draw(t, "g").(int) {
switch SampledFrom([]int{0, 1, 2}).Draw(t, "g") {
case 2:
domain = Domain().Draw(t, "domain").(string)
domain = Domain().Draw(t, "domain")
case 1:
domain = IPv6().Draw(t, "domain").(net.IP).String()
domain = IPv6().Draw(t, "domain").String()
domain = "[" + domain + "]"
case 0:
domain = IPv4().Draw(t, "domain").(net.IP).String()
domain = IPv4().Draw(t, "domain").String()
}
port := IntRange(0, 2^16-1).Draw(t, "port")
path_ := SliceOf(printableGen).Draw(t, "path")
query := SliceOf(printableGen).Draw(t, "query")
fragment := printableGen.Draw(t, "fragment")

// join domain and port
if port > 0 {
domain += fmt.Sprintf(":%d", port)
}

// URL escape path
for i := range path_ {
path_[i] = url.PathEscape(path_[i])
}

// url escape query strings
for i := range query {
query[i] = url.QueryEscape(query[i])
}
port := IntRange(0, 2^16-1).
Map(func(i int) string {
if i == 0 {
return ""
}
return fmt.Sprintf(":%d", i)
}).
Draw(t, "port").(string)
path_ := SliceOf(printableGen).Draw(t, "path").([]string)
query := SliceOf(printableGen.Map(url.QueryEscape)).Draw(t, "query").([]string)
fragment := printableGen.Draw(t, "fragment").(string)

return url.URL{
Host: domain + port,
Host: domain,
Path: strings.Join(path_, "/"),
Scheme: scheme,
RawQuery: strings.Join(query, "&"),
Expand All @@ -116,12 +107,12 @@ func (g *urlGenerator) value(t *T) value {
}

// URL generates RFC 3986 compliant http/https URLs.
func URL() *Generator {
func URL() *Generator[url.URL] {
return urlOf([]string{"http", "https"})
}

func urlOf(schemes []string) *Generator {
return newGenerator(&urlGenerator{
func urlOf(schemes []string) *Generator[url.URL] {
return newGenerator[url.URL](&urlGenerator{
schemes: schemes,
})
}
Loading

0 comments on commit 1499ace

Please sign in to comment.