-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generators for URLs and domain names #18
Open
wfscheper
wants to merge
12
commits into
flyingmutant:master
Choose a base branch
from
wfscheper:feat-url-generator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,842
−0
Open
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
be1e99a
Add generators for URLs and domain names
wfscheper e4d47fd
Add examples for URL and domain generators
wfscheper 0130cb5
Update copyright headers
wfscheper 131a8ce
Trim tlds list before splitting
wfscheper c8b1ad2
Simplify domain generation
wfscheper e7bad3e
URL generator also generates query and fragment
wfscheper 0a6a872
Add IPv4 and IPv6 generators
wfscheper 18ad723
Fix URL example tests
flyingmutant 4846a6a
Add script to update top-level domains
wfscheper 1499ace
Update to use new generic API
wfscheper b91d28f
IPv4 and IPv6 return netip.Addr
wfscheper 8aaba74
Use Transform for URL generation
wfscheper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2022 Walter Scheper <[email protected]> | ||
// | ||
// 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 | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
package rapid | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
) | ||
|
||
const ( | ||
ipv4Len = 4 | ||
ipv6Len = 16 | ||
) | ||
|
||
var ( | ||
ipv4Gen = SliceOfN(Byte(), ipv4Len, ipv4Len) | ||
ipv6Gen = SliceOfN(Byte(), ipv6Len, ipv6Len) | ||
) | ||
|
||
type ipGen struct { | ||
ipv6 bool | ||
} | ||
|
||
func (g *ipGen) String() string { | ||
return fmt.Sprintf("IP(ipv6=%v)", g.ipv6) | ||
} | ||
|
||
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()) | ||
return net.IP(b) | ||
} | ||
|
||
func IPv4() *Generator[net.IP] { | ||
return newGenerator[net.IP](&ipGen{ | ||
ipv6: false, | ||
}) | ||
} | ||
|
||
func IPv6() *Generator[net.IP] { | ||
return newGenerator[net.IP](&ipGen{ | ||
ipv6: true, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2020 Walter Scheper <[email protected]> | ||
// | ||
// 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 | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
package rapid_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"pgregory.net/rapid" | ||
) | ||
|
||
func ExampleIPv4() { | ||
gen := rapid.IPv4() | ||
|
||
for i := 0; i < 5; i++ { | ||
addr := gen.Example(i) | ||
fmt.Println(addr.String()) | ||
} | ||
|
||
// Output: | ||
// 0.23.24.7 | ||
// 100.146.0.0 | ||
// 0.222.65.1 | ||
// 1.49.104.14 | ||
// 11.56.0.83 | ||
} | ||
|
||
func ExampleIPv6() { | ||
gen := rapid.IPv6() | ||
|
||
for i := 0; i < 5; i++ { | ||
addr := gen.Example(i) | ||
fmt.Println(addr.String()) | ||
} | ||
|
||
// Output: | ||
// 17:1807:e2c4:8210:7202:f4b2:a0e2:8dc | ||
// 6492:0:fa37:b00:b5c3:4e6:6a01:c802 | ||
// de:4101:9f5:3:104:5dc:b600:905 | ||
// 131:680e:97ff:d200:ae1:4d00:2300:103 | ||
// b38:53:ff07:200:8c28:ee:ad00:1b | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is good idea to use
netip
? With generics we already require Go 1.18.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Done.