-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use protovalidate constraints when generating scalar values
- Loading branch information
1 parent
740871e
commit bf4eb5c
Showing
15 changed files
with
619 additions
and
15 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 @@ | ||
.DS_Store |
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 @@ | ||
package fauxrpc | ||
|
||
import ( | ||
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" | ||
"github.com/brianvoe/gofakeit/v7" | ||
) | ||
|
||
type BytesHints struct { | ||
Rules *validate.BytesRules | ||
FirstName bool | ||
LastName bool | ||
Name bool | ||
UUID bool | ||
URL bool | ||
Version bool | ||
} | ||
|
||
func GenerateBytes(faker *gofakeit.Faker, hints BytesHints) []byte { | ||
if hints.Rules == nil { | ||
return []byte(faker.HipsterSentence(3)) | ||
} | ||
|
||
if hints.Rules.Const != nil { | ||
return hints.Rules.Const | ||
} | ||
minLen, maxLen := uint64(0), uint64(20) | ||
if hints.Rules.Len != nil { | ||
minLen = *hints.Rules.Len | ||
maxLen = *hints.Rules.Len | ||
} | ||
if hints.Rules.MinLen != nil { | ||
minLen = *hints.Rules.MinLen | ||
} | ||
if hints.Rules.MaxLen != nil { | ||
maxLen = *hints.Rules.MaxLen | ||
} | ||
if hints.Rules.Pattern != nil { | ||
return []byte(faker.Regex(*hints.Rules.Pattern)) | ||
} | ||
|
||
if len(hints.Rules.In) > 0 { | ||
return hints.Rules.In[faker.IntRange(0, len(hints.Rules.In)-1)] | ||
} | ||
|
||
return []byte(faker.Sentence(int(maxLen / uint64(4)))[minLen:maxLen]) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package fauxrpc | ||
|
||
import ( | ||
"math" | ||
|
||
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" | ||
"github.com/brianvoe/gofakeit/v7" | ||
) | ||
|
||
type Fixed32Hints struct { | ||
Rules *validate.Fixed32Rules | ||
} | ||
|
||
func GenerateFixed32(faker *gofakeit.Faker, hints Fixed32Hints) uint32 { | ||
if hints.Rules == nil { | ||
return faker.Uint32() | ||
} | ||
|
||
if hints.Rules.Const != nil { | ||
return *hints.Rules.Const | ||
} | ||
minVal, maxVal := uint32(0), uint32(math.MaxInt32) | ||
if hints.Rules.GreaterThan != nil { | ||
switch v := hints.Rules.GreaterThan.(type) { | ||
case *validate.Fixed32Rules_Gt: | ||
minVal = v.Gt + 1 | ||
case *validate.Fixed32Rules_Gte: | ||
minVal = v.Gte | ||
} | ||
} | ||
if hints.Rules.LessThan != nil { | ||
switch v := hints.Rules.LessThan.(type) { | ||
case *validate.Fixed32Rules_Lt: | ||
maxVal = v.Lt + 1 | ||
case *validate.Fixed32Rules_Lte: | ||
maxVal = v.Lte | ||
} | ||
} | ||
|
||
if len(hints.Rules.In) > 0 { | ||
return hints.Rules.In[faker.IntRange(0, len(hints.Rules.In)-1)] | ||
} | ||
|
||
return uint32(faker.IntRange(int(minVal), int(maxVal))) | ||
} |
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 @@ | ||
package fauxrpc | ||
|
||
import ( | ||
"math" | ||
|
||
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" | ||
"github.com/brianvoe/gofakeit/v7" | ||
) | ||
|
||
type Fixed64Hints struct { | ||
Rules *validate.Fixed64Rules | ||
} | ||
|
||
func GenerateFixed64(faker *gofakeit.Faker, hints Fixed64Hints) uint64 { | ||
if hints.Rules == nil { | ||
return faker.Uint64() | ||
} | ||
|
||
if hints.Rules.Const != nil { | ||
return *hints.Rules.Const | ||
} | ||
minVal, maxVal := uint64(0), uint64(math.MaxInt64) | ||
if hints.Rules.GreaterThan != nil { | ||
switch v := hints.Rules.GreaterThan.(type) { | ||
case *validate.Fixed64Rules_Gt: | ||
minVal = v.Gt + 1 | ||
case *validate.Fixed64Rules_Gte: | ||
minVal = v.Gte | ||
} | ||
} | ||
if hints.Rules.LessThan != nil { | ||
switch v := hints.Rules.LessThan.(type) { | ||
case *validate.Fixed64Rules_Lt: | ||
maxVal = v.Lt + 1 | ||
case *validate.Fixed64Rules_Lte: | ||
maxVal = v.Lte | ||
} | ||
} | ||
|
||
if len(hints.Rules.In) > 0 { | ||
return hints.Rules.In[faker.IntRange(0, len(hints.Rules.In)-1)] | ||
} | ||
|
||
return uint64(faker.UintRange(uint(minVal), uint(maxVal))) | ||
} |
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 @@ | ||
package fauxrpc | ||
|
||
import ( | ||
"math" | ||
|
||
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" | ||
"github.com/brianvoe/gofakeit/v7" | ||
) | ||
|
||
type Float32Hints struct { | ||
Rules *validate.FloatRules | ||
} | ||
|
||
func GenerateFloat32(faker *gofakeit.Faker, hints Float32Hints) float32 { | ||
if hints.Rules == nil { | ||
return faker.Float32() | ||
} | ||
|
||
if hints.Rules.Const != nil { | ||
return *hints.Rules.Const | ||
} | ||
minVal, maxVal := float32(0), float32(math.MaxFloat32) | ||
if hints.Rules.GreaterThan != nil { | ||
switch v := hints.Rules.GreaterThan.(type) { | ||
case *validate.FloatRules_Gt: | ||
minVal = v.Gt + 1 | ||
case *validate.FloatRules_Gte: | ||
minVal = v.Gte | ||
} | ||
} | ||
if hints.Rules.LessThan != nil { | ||
switch v := hints.Rules.LessThan.(type) { | ||
case *validate.FloatRules_Lt: | ||
maxVal = v.Lt + 1 | ||
case *validate.FloatRules_Lte: | ||
maxVal = v.Lte | ||
} | ||
} | ||
|
||
if len(hints.Rules.In) > 0 { | ||
return hints.Rules.In[faker.IntRange(0, len(hints.Rules.In)-1)] | ||
} | ||
|
||
return faker.Float32Range(minVal, maxVal) | ||
} |
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 @@ | ||
package fauxrpc | ||
|
||
import ( | ||
"math" | ||
|
||
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" | ||
"github.com/brianvoe/gofakeit/v7" | ||
) | ||
|
||
type Float64Hints struct { | ||
Rules *validate.DoubleRules | ||
} | ||
|
||
func GenerateFloat64(faker *gofakeit.Faker, hints Float64Hints) float64 { | ||
if hints.Rules == nil { | ||
return faker.Float64() | ||
} | ||
|
||
if hints.Rules.Const != nil { | ||
return *hints.Rules.Const | ||
} | ||
minVal, maxVal := float64(0), float64(math.MaxFloat64) | ||
if hints.Rules.GreaterThan != nil { | ||
switch v := hints.Rules.GreaterThan.(type) { | ||
case *validate.DoubleRules_Gt: | ||
minVal = v.Gt + 1 | ||
case *validate.DoubleRules_Gte: | ||
minVal = v.Gte | ||
} | ||
} | ||
if hints.Rules.LessThan != nil { | ||
switch v := hints.Rules.LessThan.(type) { | ||
case *validate.DoubleRules_Lt: | ||
maxVal = v.Lt + 1 | ||
case *validate.DoubleRules_Lte: | ||
maxVal = v.Lte | ||
} | ||
} | ||
|
||
if len(hints.Rules.In) > 0 { | ||
return hints.Rules.In[faker.IntRange(0, len(hints.Rules.In)-1)] | ||
} | ||
|
||
return faker.Float64Range(minVal, maxVal) | ||
} |
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 @@ | ||
package fauxrpc | ||
|
||
import ( | ||
"math" | ||
|
||
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" | ||
"github.com/brianvoe/gofakeit/v7" | ||
) | ||
|
||
type Int32Hints struct { | ||
Rules *validate.Int32Rules | ||
} | ||
|
||
func GenerateInt32(faker *gofakeit.Faker, hints Int32Hints) int32 { | ||
if hints.Rules == nil { | ||
return faker.Int32() | ||
} | ||
|
||
if hints.Rules.Const != nil { | ||
return *hints.Rules.Const | ||
} | ||
minVal, maxVal := int32(0), int32(math.MaxInt32) | ||
if hints.Rules.GreaterThan != nil { | ||
switch v := hints.Rules.GreaterThan.(type) { | ||
case *validate.Int32Rules_Gt: | ||
minVal = v.Gt + 1 | ||
case *validate.Int32Rules_Gte: | ||
minVal = v.Gte | ||
} | ||
} | ||
if hints.Rules.LessThan != nil { | ||
switch v := hints.Rules.LessThan.(type) { | ||
case *validate.Int32Rules_Lt: | ||
maxVal = v.Lt + 1 | ||
case *validate.Int32Rules_Lte: | ||
maxVal = v.Lte | ||
} | ||
} | ||
|
||
if len(hints.Rules.In) > 0 { | ||
return hints.Rules.In[faker.IntRange(0, len(hints.Rules.In)-1)] | ||
} | ||
|
||
return int32(faker.IntRange(int(minVal), int(maxVal))) | ||
} |
Oops, something went wrong.