Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sudorandom committed Aug 17, 2024
1 parent cc6fa53 commit 99d2512
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 50 deletions.
9 changes: 9 additions & 0 deletions gen_bool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fauxrpc

import "google.golang.org/protobuf/reflect/protoreflect"

// GenerateBool returns a fake boolean value given a field descriptor.
func GenerateBool(fd protoreflect.FieldDescriptor) bool {
// TODO: use protovalidate
return true
}
1 change: 1 addition & 0 deletions gen_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ func generateBytesSimple() []byte {
return []byte(gofakeit.HipsterSentence(3))
}

// GenerateBytes returns a fake []byte value given a field descriptor.
func GenerateBytes(fd protoreflect.FieldDescriptor) []byte {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
13 changes: 13 additions & 0 deletions gen_enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fauxrpc

import (
"github.com/brianvoe/gofakeit/v7"
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateEnum returns a fake enum value given a field descriptor.
func GenerateEnum(fd protoreflect.FieldDescriptor) protoreflect.EnumNumber {
values := fd.Enum().Values()
idx := gofakeit.IntRange(0, values.Len()-1)
return protoreflect.EnumNumber(idx)
}
118 changes: 69 additions & 49 deletions gen_fields.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,109 @@
package fauxrpc

import (
"github.com/brianvoe/gofakeit/v7"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"
)

func getFieldValue(fd protoreflect.FieldDescriptor, st state) *protoreflect.Value {
switch fd.Kind() {
case protoreflect.MessageKind:
switch string(fd.Message().FullName()) {
case "google.protobuf.Duration":
// TODO: hints
return genGoogleDuration()
case "google.protobuf.Timestamp":
// TODO: hints
return genGoogleTimestamp()
case "google.protobuf.Any":
// TODO: hints
return nil
case "google.protobuf.Value":
// TODO: hints
return genGoogleValue()
default:
nested := dynamicpb.NewMessage(fd.Message())
setDataOnMessage(nested, st.Inc())
v := protoreflect.ValueOf(nested)
return &v
}
case protoreflect.GroupKind:
nested := dynamicpb.NewMessage(fd.Message())
setDataOnMessage(nested, st.Inc())
v := protoreflect.ValueOf(nested)
return &v
case protoreflect.BoolKind:
var kindToGenerator = map[protoreflect.Kind]func(fd protoreflect.FieldDescriptor) *protoreflect.Value{
protoreflect.BoolKind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfBool(true)
return &v
case protoreflect.EnumKind:
values := fd.Enum().Values()
idx := gofakeit.IntRange(0, values.Len()-1)
v := protoreflect.ValueOfEnum(protoreflect.EnumNumber(idx))
},
protoreflect.EnumKind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfEnum(GenerateEnum(fd))
return &v
case protoreflect.StringKind:
},
protoreflect.StringKind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfString(GenerateString(fd))
return &v
case protoreflect.BytesKind:
},
protoreflect.BytesKind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfBytes(GenerateBytes(fd))
return &v
case protoreflect.Int32Kind:
},
protoreflect.Int32Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfInt32(GenerateInt32(fd))
return &v
case protoreflect.Sint32Kind:
},
protoreflect.Sint32Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfInt32(GenerateSInt32(fd))
return &v
case protoreflect.Sfixed32Kind:
v := protoreflect.ValueOfInt32(GenerateSFixedInt32(fd))
},
protoreflect.Sfixed32Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfInt32(GenerateSFixed32(fd))
return &v
case protoreflect.Uint32Kind:
},
protoreflect.Uint32Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfUint32(GenerateUInt32(fd))
return &v
case protoreflect.Fixed32Kind:
},
protoreflect.Fixed32Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfUint32(GenerateFixed32(fd))
return &v
case protoreflect.Int64Kind:
},
protoreflect.Int64Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfInt64(GenerateInt64(fd))
return &v
case protoreflect.Sint64Kind:
},
protoreflect.Sint64Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfInt64(GenerateSInt64(fd))
return &v
case protoreflect.Sfixed64Kind:
},
protoreflect.Sfixed64Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfInt64(GenerateSFixed64(fd))
return &v
case protoreflect.Uint64Kind:
},
protoreflect.Uint64Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfUint64(GenerateUInt64(fd))
return &v
case protoreflect.Fixed64Kind:
},
protoreflect.Fixed64Kind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfUint64(GenerateFixed64(fd))
return &v
case protoreflect.FloatKind:
},
protoreflect.FloatKind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfFloat32(GenerateFloat32(fd))
return &v
case protoreflect.DoubleKind:
},
protoreflect.DoubleKind: func(fd protoreflect.FieldDescriptor) *protoreflect.Value {
v := protoreflect.ValueOfFloat64(GenerateFloat64(fd))
return &v
default:
},
}

func getFieldValue(fd protoreflect.FieldDescriptor, st state) *protoreflect.Value {
switch fd.Kind() {
case protoreflect.MessageKind:
switch string(fd.Message().FullName()) {
case "google.protobuf.Duration":
// TODO: hints
return genGoogleDuration()
case "google.protobuf.Timestamp":
// TODO: hints
return genGoogleTimestamp()
case "google.protobuf.Any":
// TODO: hints
return nil
case "google.protobuf.Value":
// TODO: hints
return genGoogleValue()
}

nested := dynamicpb.NewMessage(fd.Message())
setDataOnMessage(nested, st.Inc())
v := protoreflect.ValueOf(nested)
return &v
case protoreflect.GroupKind:
nested := dynamicpb.NewMessage(fd.Message())
setDataOnMessage(nested, st.Inc())
v := protoreflect.ValueOf(nested)
return &v
}

fn, ok := kindToGenerator[fd.Kind()]
if !ok {
return nil
}
return fn(fd)
}
1 change: 1 addition & 0 deletions gen_fixed32.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateFixed32 returns a fake fixed32 value given a field descriptor.
func GenerateFixed32(fd protoreflect.FieldDescriptor) uint32 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_fixedint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateFixed64 returns a fake fixed64 value given a field descriptor.
func GenerateFixed64(fd protoreflect.FieldDescriptor) uint64 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateFloat32 returns a fake float32 value given a field descriptor.
func GenerateFloat32(fd protoreflect.FieldDescriptor) float32 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateFloat64 returns a fake float64 value given a field descriptor.
func GenerateFloat64(fd protoreflect.FieldDescriptor) float64 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateInt32 returns a fake int32 value given a field descriptor.
func GenerateInt32(fd protoreflect.FieldDescriptor) int32 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateInt64 returns a fake int64 value given a field descriptor.
func GenerateInt64(fd protoreflect.FieldDescriptor) int64 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/types/dynamicpb"
)

// SetDataOnMessage generates fake data given a *dynamicpb.Message and sets the field values.
func SetDataOnMessage(msg *dynamicpb.Message) {
setDataOnMessage(msg, state{})
}
Expand Down
1 change: 1 addition & 0 deletions gen_sfixed64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateSFixed64 returns a fake sfixed64 value given a field descriptor.
func GenerateSFixed64(fd protoreflect.FieldDescriptor) int64 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
3 changes: 2 additions & 1 deletion gen_sfixedint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

func GenerateSFixedInt32(fd protoreflect.FieldDescriptor) int32 {
// GenerateSFixed32 returns a fake sfixedint32 value given a field descriptor.
func GenerateSFixed32(fd protoreflect.FieldDescriptor) int32 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
return gofakeit.Int32()
Expand Down
1 change: 1 addition & 0 deletions gen_sint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateSInt32 returns a fake sint32 value given a field descriptor.
func GenerateSInt32(fd protoreflect.FieldDescriptor) int32 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_sint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateSInt64 returns a fake sint64 value given a field descriptor.
func GenerateSInt64(fd protoreflect.FieldDescriptor) int64 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func generateStringSimple(fd protoreflect.FieldDescriptor) string {
return gofakeit.HipsterSentence(int(randInt64GeometricDist(0.5) + 1))
}

// GenerateString returns a fake string value given a field descriptor.
func GenerateString(fd protoreflect.FieldDescriptor) string {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateUInt32 returns a fake uint32 value given a field descriptor.
func GenerateUInt32(fd protoreflect.FieldDescriptor) uint32 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
1 change: 1 addition & 0 deletions gen_uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// GenerateUInt64 returns a fake uint64 value given a field descriptor.
func GenerateUInt64(fd protoreflect.FieldDescriptor) uint64 {
constraints := getResolver().ResolveFieldConstraints(fd)
if constraints == nil {
Expand Down
3 changes: 3 additions & 0 deletions gen_wellknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ import (
)

func genGoogleDuration() *protoreflect.Value {
// TODO: use protovalidate
duration := time.Duration(gofakeit.Uint64() % uint64(30*time.Hour*24))
v := protoreflect.ValueOf(durationpb.New(duration).ProtoReflect())
return &v
}

func genGoogleTimestamp() *protoreflect.Value {
// TODO: use protovalidate
v := protoreflect.ValueOf(timestamppb.New(gofakeit.Date()).ProtoReflect())
return &v
}

func genGoogleValue() *protoreflect.Value {
// TODO: use protovalidate
scalarOptions := []func() *structpb.Value{
func() *structpb.Value { return structpb.NewNullValue() },
func() *structpb.Value { return structpb.NewBoolValue(gofakeit.Bool()) },
Expand Down

0 comments on commit 99d2512

Please sign in to comment.