Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lesiw committed Dec 29, 2024
0 parents commit f6ea6a3
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*.go]
tab_width = 4
max_line_length = 80
9 changes: 9 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: main
on: [push]
jobs:
op:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- run: '"$(wget -O- lesiw.io/op | sh)"'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.sandbox/*
testdata/*
24 changes: 24 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
linters:
enable:
- errname
- errcheck
- gocheckcompilerdirectives
- gocyclo
- lll
- makezero
- godot
- unparam
- unused

linters-settings:
gocyclo:
min-complexity: 15
lll:
tab-width: 4
line-length: 79

issues:
exclude-rules:
- linters:
- errcheck
source: "defer "
1 change: 1 addition & 0 deletions .ops/.uuid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2f55c3f1-5bb9-4d4e-9272-4162b0808eb6
19 changes: 19 additions & 0 deletions .ops/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module ops.localhost

go 1.23.4

require (
labs.lesiw.io/ops v0.0.0-20241225025618-0c8ebe205d8d
lesiw.io/ops v0.14.0
)

require (
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sync v0.10.0 // indirect
lesiw.io/clerk v0.2.0 // indirect
lesiw.io/cmdio v0.8.0 // indirect
lesiw.io/cmdio/x/busybox v0.1.0 // indirect
lesiw.io/defers v0.9.0 // indirect
lesiw.io/flag v0.7.0 // indirect
lesiw.io/prefix v0.1.0 // indirect
)
28 changes: 28 additions & 0 deletions .ops/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
labs.lesiw.io/ops v0.0.0-20241225025618-0c8ebe205d8d h1:4SDyitrvoWmFKS6hGNZW7K9YOstcpNS26AGU2176ECQ=
labs.lesiw.io/ops v0.0.0-20241225025618-0c8ebe205d8d/go.mod h1:Df/3NUzp0iS9TjL5mvuacI+q2pXWEuyuHjFtfg+Picg=
lesiw.io/clerk v0.2.0 h1:Rcq19dHx9TRk4Rkpm9Sw/KGySUxrNmPhrcmU2EQhBYo=
lesiw.io/clerk v0.2.0/go.mod h1:WMyvgTe+3Eob36b6KX86MOaxaeNMvv/HZTSsf2Dedhg=
lesiw.io/cmdio v0.8.0 h1:5Psx1eHK1osIuII62dWr5WoBqeTrlb2R9SuVY9Rpk0Y=
lesiw.io/cmdio v0.8.0/go.mod h1:kYVnG1zsDgkrjBmlECbWf34Q83J8YeiQxyE2QOT3tBQ=
lesiw.io/cmdio/x/busybox v0.1.0 h1:e1G1JJttIcwpGuUki7oLQTx7Ve6BWExe2Cs4aw0kExM=
lesiw.io/cmdio/x/busybox v0.1.0/go.mod h1:PkwZVzE2tTrNI4BNOgiz595SwwF8BAbvI9LSz1P5GJY=
lesiw.io/defers v0.9.0 h1:Sg7RYbhxfHhXMHclO65MJ4oRbyhfSBSeHQw4YjLr6n0=
lesiw.io/defers v0.9.0/go.mod h1:AP09yGFHxL5vmTVJxkPL33N1hWI4OzHwTEOzilbDZU4=
lesiw.io/flag v0.7.0 h1:+8rTdoplDMBhOSKok5eKP6ZuLLPTodkDABRY7jfX5JU=
lesiw.io/flag v0.7.0/go.mod h1:bJx6Hir8MAXkNiO6BbrvhwZuaJF4rQWthQ7pc1DlWZY=
lesiw.io/ops v0.14.0 h1:WTdNxlqL2HjDgpDd5VqoaQwKOqvu1CeR1Wi1wk6wJ0E=
lesiw.io/ops v0.14.0/go.mod h1:onq4+252Tz6+DzGMtUiibOm7+KJW+ty8v5OBJ91Yb7M=
lesiw.io/prefix v0.1.0 h1:2Ors12avAiADgMbsQHh27wQmoSI+4aZSW2uTBdDmIZg=
lesiw.io/prefix v0.1.0/go.mod h1:yrUaJpvikavNodwcL64crLnSf3t1NWeNi/vNu5hUi2Y=
17 changes: 17 additions & 0 deletions .ops/ops.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"os"

"labs.lesiw.io/ops/golib"
"lesiw.io/ops"
)

type Ops struct{ golib.Ops }

func main() {
if len(os.Args) < 2 {
os.Args = append(os.Args, "check")
}
ops.Handle(Ops{})
}
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2024 Chris Lesiw

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# lesiw.io/fill

A utility for filling Go values with random data.

## Example

``` go
package main

import (
"crypto/tls"
"math/rand/v2"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"lesiw.io/fill"
)

func FuzzTlsConfigClone(f *testing.F) {
opts := cmp.Options{cmpopts.IgnoreUnexported(tls.Config{})}
f.Fuzz(func(t *testing.T, seed1 uint64, seed2 uint64) {
cfg1 := &tls.Config{}
fill.Do(&cfg1, rand.New(rand.NewPCG(seed1, seed2)))
cfg2 := cfg1.Clone()
if !cmp.Equal(cfg1, cfg2, opts) {
t.Errorf("-original +cloned\n%s", cmp.Diff(cfg1, cfg2, opts))
}
})
}
```

[▶️ Run this example on the Go Playground](https://go.dev/play/p/_HX3J0f__AF)

To run locally, `go test -fuzz=Fuzz -fuzztime=10s`.
3 changes: 3 additions & 0 deletions clerk.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.editorconfig 3632ddbf531e998e5990a3d4e88b547fea86dd00
.github/workflows/main.yml 62efcdd9a8f00f489693e4f0f916429822dc311f
.golangci.yml 755dceae1c8c48598bdb383e8016e74d2adcc8dc
141 changes: 141 additions & 0 deletions fill.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package fill

import (
"fmt"
"math/rand/v2"
"reflect"
)

func Do(a any, rng *rand.Rand) {
if rng == nil {
panic("bad parameter: nil rand.Rand")
}
val := reflect.ValueOf(a)
if val.Kind() != reflect.Ptr {
panic("bad parameter: value to fill must be pointer")
}
val = val.Elem()
fillValue(val, rng)
}

func fillValue(val reflect.Value, rng *rand.Rand) {
k := val.Kind()
switch {
case intKind(k):
val.SetInt(rng.Int64())
case uintKind(k):
val.SetUint(rng.Uint64())
case floatKind(k):
val.SetFloat(rng.Float64())
case complexKind(k):
val.SetComplex(complex(rng.Float64(), rng.Float64()))
case k == reflect.String:
fillString(val, rng)
case k == reflect.Bool:
val.SetBool(rng.IntN(2) == 0)
case k == reflect.Struct:
fillStruct(val, rng)
case k == reflect.Slice:
fillSlice(val, rng)
case k == reflect.Map:
fillMap(val, rng)
case k == reflect.Array:
fillArray(val, rng)
case k == reflect.Chan:
fillChan(val, rng)
case k == reflect.Pointer:
val.Set(reflect.New(val.Type().Elem()))
fillValue(val.Elem(), rng)
case k == reflect.Interface || k == reflect.Func:
// Can't fill.
default:
panic(fmt.Sprintf("unhandled type: %s", val.Kind()))
}
}

func fillStruct(val reflect.Value, rng *rand.Rand) {
for i := range val.NumField() {
f := val.Field(i)
if !f.CanSet() {
continue
}
fillValue(f, rng)
}
}

func fillSlice(val reflect.Value, rng *rand.Rand) {
for range rng.IntN(16) {
e := reflect.New(val.Type().Elem()).Elem()
fillValue(e, rng)
val.Set(reflect.Append(val, e))
}
}

func fillMap(val reflect.Value, rng *rand.Rand) {
sz := rng.IntN(16)
val.Set(reflect.MakeMapWithSize(val.Type(), sz))
for range sz {
k := reflect.New(val.Type().Key()).Elem()
fillValue(k, rng)
v := reflect.New(val.Type().Elem()).Elem()
fillValue(v, rng)
val.SetMapIndex(k, v)
}
}

func fillChan(val reflect.Value, _ *rand.Rand) {
reflect.MakeChan(val.Type(), 0)
}

func fillArray(val reflect.Value, rng *rand.Rand) {
for i := range val.Len() {
fillValue(val.Index(i), rng)
}
}

func fillString(val reflect.Value, rng *rand.Rand) {
sz := rng.IntN(16)
b := make([]byte, sz)
for i := range sz {
b[i] = byte(rng.IntN(256))
}
val.SetString(string(b))
}

func intKind(k reflect.Kind) bool {
switch k {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Int:
return true
default:
return false
}
}

func uintKind(k reflect.Kind) bool {
switch k {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Uint:
return true
default:
return false
}
}

func floatKind(k reflect.Kind) bool {
switch k {
case reflect.Float32, reflect.Float64:
return true
default:
return false
}
}

func complexKind(k reflect.Kind) bool {
switch k {
case reflect.Complex64, reflect.Complex128:
return true
default:
return false
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module lesiw.io/fill

go 1.22.0

0 comments on commit f6ea6a3

Please sign in to comment.