-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These generators return `net.IP`, and are also used in generating URLs.
- Loading branch information
Showing
4 changed files
with
125 additions
and
8 deletions.
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,61 @@ | ||
// Copyright 2020 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 | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
package rapid | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"reflect" | ||
) | ||
|
||
const ( | ||
ipv4Len = 4 | ||
ipv6Len = 16 | ||
) | ||
|
||
var ( | ||
ipType = reflect.TypeOf(net.IP{}) | ||
|
||
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 (*ipGen) type_() reflect.Type { | ||
return ipType | ||
} | ||
|
||
func (g *ipGen) value(t *T) value { | ||
var gen *Generator | ||
if g.ipv6 { | ||
gen = ipv6Gen | ||
} else { | ||
gen = ipv4Gen | ||
} | ||
|
||
b := gen.Draw(t, g.String()).([]byte) | ||
return net.IP(b) | ||
} | ||
|
||
func IPv4() *Generator { | ||
return newGenerator(&ipGen{ | ||
ipv6: false, | ||
}) | ||
} | ||
|
||
func IPv6() *Generator { | ||
return newGenerator(&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,46 @@ | ||
// Copyright 2020 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 | ||
// file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
||
package rapid_test | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"pgregory.net/rapid" | ||
) | ||
|
||
func ExampleIPv4() { | ||
gen := rapid.IPv4() | ||
|
||
for i := 0; i < 5; i++ { | ||
addr := gen.Example(i).(net.IP) | ||
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).(net.IP) | ||
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 | ||
} |
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
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