-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc6fa53
commit 99d2512
Showing
19 changed files
with
110 additions
and
50 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,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 | ||
} |
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,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) | ||
} |
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 |
---|---|---|
@@ -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) | ||
} |
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
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
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
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
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
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
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
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