Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sashamelentyev committed Nov 13, 2022
1 parent fb6bf93 commit a0f5a76
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 71 deletions.
4 changes: 2 additions & 2 deletions internal/httprule/fuzz.go
@@ -1,10 +1,10 @@
//go:build gofuzz
// +build gofuzz

package httprule

func Fuzz(data []byte) int {
_, err := Parse(string(data))
if err != nil {
if _, err := Parse(string(data)); err != nil {
return 0
}
return 0
Expand Down
@@ -1,4 +1,5 @@
//+build !go1.12
//go:build !go1.12
// +build !go1.12

package genopenapi

Expand Down
46 changes: 21 additions & 25 deletions runtime/convert.go
Expand Up @@ -37,7 +37,7 @@ func BoolSlice(val, sep string) ([]bool, error) {
for i, v := range s {
value, err := Bool(v)
if err != nil {
return values, err
return nil, err
}
values[i] = value
}
Expand All @@ -57,7 +57,7 @@ func Float64Slice(val, sep string) ([]float64, error) {
for i, v := range s {
value, err := Float64(v)
if err != nil {
return values, err
return nil, err
}
values[i] = value
}
Expand All @@ -81,7 +81,7 @@ func Float32Slice(val, sep string) ([]float32, error) {
for i, v := range s {
value, err := Float32(v)
if err != nil {
return values, err
return nil, err
}
values[i] = value
}
Expand All @@ -101,7 +101,7 @@ func Int64Slice(val, sep string) ([]int64, error) {
for i, v := range s {
value, err := Int64(v)
if err != nil {
return values, err
return nil, err
}
values[i] = value
}
Expand All @@ -125,7 +125,7 @@ func Int32Slice(val, sep string) ([]int32, error) {
for i, v := range s {
value, err := Int32(v)
if err != nil {
return values, err
return nil, err
}
values[i] = value
}
Expand All @@ -145,7 +145,7 @@ func Uint64Slice(val, sep string) ([]uint64, error) {
for i, v := range s {
value, err := Uint64(v)
if err != nil {
return values, err
return nil, err
}
values[i] = value
}
Expand All @@ -169,7 +169,7 @@ func Uint32Slice(val, sep string) ([]uint32, error) {
for i, v := range s {
value, err := Uint32(v)
if err != nil {
return values, err
return nil, err
}
values[i] = value
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func BytesSlice(val, sep string) ([][]byte, error) {
for i, v := range s {
value, err := Bytes(v)
if err != nil {
return values, err
return nil, err
}
values[i] = value
}
Expand All @@ -209,8 +209,7 @@ func Timestamp(val string) (*timestamppb.Timestamp, error) {
var r timestamppb.Timestamp
val = strconv.Quote(strings.Trim(val, `"`))
unmarshaler := &protojson.UnmarshalOptions{}
err := unmarshaler.Unmarshal([]byte(val), &r)
if err != nil {
if err := unmarshaler.Unmarshal([]byte(val), &r); err != nil {
return nil, err
}
return &r, nil
Expand All @@ -221,8 +220,7 @@ func Duration(val string) (*durationpb.Duration, error) {
var r durationpb.Duration
val = strconv.Quote(strings.Trim(val, `"`))
unmarshaler := &protojson.UnmarshalOptions{}
err := unmarshaler.Unmarshal([]byte(val), &r)
if err != nil {
if err := unmarshaler.Unmarshal([]byte(val), &r); err != nil {
return nil, err
}
return &r, nil
Expand Down Expand Up @@ -257,66 +255,64 @@ func EnumSlice(val, sep string, enumValMap map[string]int32) ([]int32, error) {
for i, v := range s {
value, err := Enum(v, enumValMap)
if err != nil {
return values, err
return nil, err
}
values[i] = value
}
return values, nil
}

/*
Support for google.protobuf.wrappers on top of primitive types
*/
// Support for google.protobuf.wrappers on top of primitive types

// StringValue well-known type support as wrapper around string type
func StringValue(val string) (*wrapperspb.StringValue, error) {
return &wrapperspb.StringValue{Value: val}, nil
return wrapperspb.String(val), nil
}

// FloatValue well-known type support as wrapper around float32 type
func FloatValue(val string) (*wrapperspb.FloatValue, error) {
parsedVal, err := Float32(val)
return &wrapperspb.FloatValue{Value: parsedVal}, err
return wrapperspb.Float(parsedVal), err
}

// DoubleValue well-known type support as wrapper around float64 type
func DoubleValue(val string) (*wrapperspb.DoubleValue, error) {
parsedVal, err := Float64(val)
return &wrapperspb.DoubleValue{Value: parsedVal}, err
return wrapperspb.Double(parsedVal), err
}

// BoolValue well-known type support as wrapper around bool type
func BoolValue(val string) (*wrapperspb.BoolValue, error) {
parsedVal, err := Bool(val)
return &wrapperspb.BoolValue{Value: parsedVal}, err
return wrapperspb.Bool(parsedVal), err
}

// Int32Value well-known type support as wrapper around int32 type
func Int32Value(val string) (*wrapperspb.Int32Value, error) {
parsedVal, err := Int32(val)
return &wrapperspb.Int32Value{Value: parsedVal}, err
return wrapperspb.Int32(parsedVal), err
}

// UInt32Value well-known type support as wrapper around uint32 type
func UInt32Value(val string) (*wrapperspb.UInt32Value, error) {
parsedVal, err := Uint32(val)
return &wrapperspb.UInt32Value{Value: parsedVal}, err
return wrapperspb.UInt32(parsedVal), err
}

// Int64Value well-known type support as wrapper around int64 type
func Int64Value(val string) (*wrapperspb.Int64Value, error) {
parsedVal, err := Int64(val)
return &wrapperspb.Int64Value{Value: parsedVal}, err
return wrapperspb.Int64(parsedVal), err
}

// UInt64Value well-known type support as wrapper around uint64 type
func UInt64Value(val string) (*wrapperspb.UInt64Value, error) {
parsedVal, err := Uint64(val)
return &wrapperspb.UInt64Value{Value: parsedVal}, err
return wrapperspb.UInt64(parsedVal), err
}

// BytesValue well-known type support as wrapper around bytes[] type
func BytesValue(val string) (*wrapperspb.BytesValue, error) {
parsedVal, err := Bytes(val)
return &wrapperspb.BytesValue{Value: parsedVal}, err
return wrapperspb.Bytes(parsedVal), err
}
5 changes: 2 additions & 3 deletions runtime/marshal_json_test.go
Expand Up @@ -100,9 +100,8 @@ func TestJSONBuiltinUnmarshalField(t *testing.T) {
func alloc(t reflect.Type) reflect.Value {
if t == nil {
return reflect.ValueOf(new(interface{}))
} else {
return reflect.New(t)
}
return reflect.New(t)
}

func TestJSONBuiltinUnmarshalFieldKnownErrors(t *testing.T) {
Expand Down Expand Up @@ -250,7 +249,7 @@ var (
json: `"2016-05-10T10:19:13.123Z"`,
},
{
data: &wrapperspb.Int32Value{Value: 123},
data: wrapperspb.Int32(123),
json: "123",
},
}
Expand Down
14 changes: 7 additions & 7 deletions runtime/marshal_jsonpb_test.go
Expand Up @@ -637,31 +637,31 @@ var (
},

{
data: &wrapperspb.BoolValue{Value: true},
data: wrapperspb.Bool(true),
json: "true",
},
{
data: &wrapperspb.DoubleValue{Value: 123.456},
data: wrapperspb.Double(123.456),
json: "123.456",
},
{
data: &wrapperspb.FloatValue{Value: 123.456},
data: wrapperspb.Float(123.456),
json: "123.456",
},
{
data: &wrapperspb.Int32Value{Value: -123},
data: wrapperspb.Int32(-123),
json: "-123",
},
{
data: &wrapperspb.Int64Value{Value: -123},
data: wrapperspb.Int64(-123),
json: `"-123"`,
},
{
data: &wrapperspb.UInt32Value{Value: 123},
data: wrapperspb.UInt32(123),
json: "123",
},
{
data: &wrapperspb.UInt64Value{Value: 123},
data: wrapperspb.UInt64(123),
json: `"123"`,
},
// TODO(yugui) Add other well-known types once jsonpb supports them
Expand Down
29 changes: 14 additions & 15 deletions runtime/query.go
Expand Up @@ -180,10 +180,10 @@ func parseField(fieldDescriptor protoreflect.FieldDescriptor, value string) (pro
return protoreflect.ValueOfBool(v), nil
case protoreflect.EnumKind:
enum, err := protoregistry.GlobalTypes.FindEnumByName(fieldDescriptor.Enum().FullName())
switch {
case errors.Is(err, protoregistry.NotFound):
return protoreflect.Value{}, fmt.Errorf("enum %q is not registered", fieldDescriptor.Enum().FullName())
case err != nil:
if err != nil {
if errors.Is(err, protoregistry.NotFound) {
return protoreflect.Value{}, fmt.Errorf("enum %q is not registered", fieldDescriptor.Enum().FullName())
}
return protoreflect.Value{}, fmt.Errorf("failed to look up enum: %w", err)
}
// Look for enum by name
Expand All @@ -194,8 +194,7 @@ func parseField(fieldDescriptor protoreflect.FieldDescriptor, value string) (pro
return protoreflect.Value{}, fmt.Errorf("%q is not a valid value", value)
}
// Look for enum by number
v = enum.Descriptor().Values().ByNumber(protoreflect.EnumNumber(i))
if v == nil {
if v = enum.Descriptor().Values().ByNumber(protoreflect.EnumNumber(i)); v == nil {
return protoreflect.Value{}, fmt.Errorf("%q is not a valid value", value)
}
}
Expand Down Expand Up @@ -271,51 +270,51 @@ func parseMessage(msgDescriptor protoreflect.MessageDescriptor, value string) (p
if err != nil {
return protoreflect.Value{}, err
}
msg = &wrapperspb.DoubleValue{Value: v}
msg = wrapperspb.Double(v)
case "google.protobuf.FloatValue":
v, err := strconv.ParseFloat(value, 32)
if err != nil {
return protoreflect.Value{}, err
}
msg = &wrapperspb.FloatValue{Value: float32(v)}
msg = wrapperspb.Float(float32(v))
case "google.protobuf.Int64Value":
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return protoreflect.Value{}, err
}
msg = &wrapperspb.Int64Value{Value: v}
msg = wrapperspb.Int64(v)
case "google.protobuf.Int32Value":
v, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return protoreflect.Value{}, err
}
msg = &wrapperspb.Int32Value{Value: int32(v)}
msg = wrapperspb.Int32(int32(v))
case "google.protobuf.UInt64Value":
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return protoreflect.Value{}, err
}
msg = &wrapperspb.UInt64Value{Value: v}
msg = wrapperspb.UInt64(v)
case "google.protobuf.UInt32Value":
v, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return protoreflect.Value{}, err
}
msg = &wrapperspb.UInt32Value{Value: uint32(v)}
msg = wrapperspb.UInt32(uint32(v))
case "google.protobuf.BoolValue":
v, err := strconv.ParseBool(value)
if err != nil {
return protoreflect.Value{}, err
}
msg = &wrapperspb.BoolValue{Value: v}
msg = wrapperspb.Bool(v)
case "google.protobuf.StringValue":
msg = &wrapperspb.StringValue{Value: value}
msg = wrapperspb.String(value)
case "google.protobuf.BytesValue":
v, err := Bytes(value)
if err != nil {
return protoreflect.Value{}, err
}
msg = &wrapperspb.BytesValue{Value: v}
msg = wrapperspb.Bytes(v)
case "google.protobuf.FieldMask":
fm := &field_mask.FieldMask{}
fm.Paths = append(fm.Paths, strings.Split(value, ",")...)
Expand Down
36 changes: 18 additions & 18 deletions runtime/query_test.go
Expand Up @@ -190,15 +190,15 @@ func TestPopulateParameters(t *testing.T) {
TimestampValue: timePb,
DurationValue: durationPb,
FieldmaskValue: fieldmaskPb,
WrapperFloatValue: &wrapperspb.FloatValue{Value: 1.5},
WrapperDoubleValue: &wrapperspb.DoubleValue{Value: 2.5},
WrapperInt64Value: &wrapperspb.Int64Value{Value: -1},
WrapperInt32Value: &wrapperspb.Int32Value{Value: -2},
WrapperUInt64Value: &wrapperspb.UInt64Value{Value: 3},
WrapperUInt32Value: &wrapperspb.UInt32Value{Value: 4},
WrapperBoolValue: &wrapperspb.BoolValue{Value: true},
WrapperStringValue: &wrapperspb.StringValue{Value: "str"},
WrapperBytesValue: &wrapperspb.BytesValue{Value: []byte("abc123!?$*&()'-=@~")},
WrapperFloatValue: wrapperspb.Float(1.5),
WrapperDoubleValue: wrapperspb.Double(2.5),
WrapperInt64Value: wrapperspb.Int64(-1),
WrapperInt32Value: wrapperspb.Int32(-2),
WrapperUInt64Value: wrapperspb.UInt64(3),
WrapperUInt32Value: wrapperspb.UInt32(4),
WrapperBoolValue: wrapperspb.Bool(true),
WrapperStringValue: wrapperspb.String("str"),
WrapperBytesValue: wrapperspb.Bytes([]byte("abc123!?$*&()'-=@~")),
MapValue: map[string]string{
"key": "value",
"second": "bar",
Expand Down Expand Up @@ -269,15 +269,15 @@ func TestPopulateParameters(t *testing.T) {
TimestampValue: timePb,
DurationValue: durationPb,
FieldmaskValue: fieldmaskPb,
WrapperFloatValue: &wrapperspb.FloatValue{Value: 1.5},
WrapperDoubleValue: &wrapperspb.DoubleValue{Value: 2.5},
WrapperInt64Value: &wrapperspb.Int64Value{Value: -1},
WrapperInt32Value: &wrapperspb.Int32Value{Value: -2},
WrapperUInt64Value: &wrapperspb.UInt64Value{Value: 3},
WrapperUInt32Value: &wrapperspb.UInt32Value{Value: 4},
WrapperBoolValue: &wrapperspb.BoolValue{Value: true},
WrapperStringValue: &wrapperspb.StringValue{Value: "str"},
WrapperBytesValue: &wrapperspb.BytesValue{Value: []byte("bytes")},
WrapperFloatValue: wrapperspb.Float(1.5),
WrapperDoubleValue: wrapperspb.Double(2.5),
WrapperInt64Value: wrapperspb.Int64(-1),
WrapperInt32Value: wrapperspb.Int32(-2),
WrapperUInt64Value: wrapperspb.UInt64(3),
WrapperUInt32Value: wrapperspb.UInt32(4),
WrapperBoolValue: wrapperspb.Bool(true),
WrapperStringValue: wrapperspb.String("str"),
WrapperBytesValue: wrapperspb.Bytes([]byte("bytes")),
StructValueValue: structValueValues[1],
StructValue: structValues[1],
},
Expand Down

0 comments on commit a0f5a76

Please sign in to comment.