Exemple #1
0
func (b binding) QueryParamFilter() queryParamFilter {
	var seqs [][]string
	if b.Body != nil {
		seqs = append(seqs, strings.Split(b.Body.FieldPath.String(), "."))
	}
	for _, p := range b.PathParams {
		seqs = append(seqs, strings.Split(p.FieldPath.String(), "."))
	}
	return queryParamFilter{utilities.NewDoubleArray(seqs)}
}
func TestPopulateParameters(t *testing.T) {
	for _, spec := range []struct {
		values url.Values
		filter *utilities.DoubleArray
		want   proto.Message
	}{
		{
			values: url.Values{
				"float_value":    {"1.5"},
				"double_value":   {"2.5"},
				"int64_value":    {"-1"},
				"int32_value":    {"-2"},
				"uint64_value":   {"3"},
				"uint32_value":   {"4"},
				"bool_value":     {"true"},
				"string_value":   {"str"},
				"repeated_value": {"a", "b", "c"},
			},
			filter: utilities.NewDoubleArray(nil),
			want: &proto3Message{
				FloatValue:    1.5,
				DoubleValue:   2.5,
				Int64Value:    -1,
				Int32Value:    -2,
				Uint64Value:   3,
				Uint32Value:   4,
				BoolValue:     true,
				StringValue:   "str",
				RepeatedValue: []string{"a", "b", "c"},
			},
		},
		{
			values: url.Values{
				"float_value":    {"1.5"},
				"double_value":   {"2.5"},
				"int64_value":    {"-1"},
				"int32_value":    {"-2"},
				"uint64_value":   {"3"},
				"uint32_value":   {"4"},
				"bool_value":     {"true"},
				"string_value":   {"str"},
				"repeated_value": {"a", "b", "c"},
			},
			filter: utilities.NewDoubleArray(nil),
			want: &proto2Message{
				FloatValue:    proto.Float32(1.5),
				DoubleValue:   proto.Float64(2.5),
				Int64Value:    proto.Int64(-1),
				Int32Value:    proto.Int32(-2),
				Uint64Value:   proto.Uint64(3),
				Uint32Value:   proto.Uint32(4),
				BoolValue:     proto.Bool(true),
				StringValue:   proto.String("str"),
				RepeatedValue: []string{"a", "b", "c"},
			},
		},
		{
			values: url.Values{
				"nested.nested.nested.repeated_value": {"a", "b", "c"},
				"nested.nested.nested.string_value":   {"s"},
				"nested.nested.string_value":          {"t"},
				"nested.string_value":                 {"u"},
				"nested_non_null.string_value":        {"v"},
			},
			filter: utilities.NewDoubleArray(nil),
			want: &proto3Message{
				Nested: &proto2Message{
					Nested: &proto3Message{
						Nested: &proto2Message{
							RepeatedValue: []string{"a", "b", "c"},
							StringValue:   proto.String("s"),
						},
						StringValue: "t",
					},
					StringValue: proto.String("u"),
				},
				NestedNonNull: proto2Message{
					StringValue: proto.String("v"),
				},
			},
		},
		{
			values: url.Values{
				"uint64_value": {"1", "2", "3", "4", "5"},
			},
			filter: utilities.NewDoubleArray(nil),
			want: &proto3Message{
				Uint64Value: 1,
			},
		},
	} {
		msg := proto.Clone(spec.want)
		msg.Reset()
		err := runtime.PopulateQueryParameters(msg, spec.values, spec.filter)
		if err != nil {
			t.Errorf("runtime.PoplateQueryParameters(msg, %v, %v) failed with %v; want success", spec.values, spec.filter, err)
			continue
		}
		if got, want := msg, spec.want; !proto.Equal(got, want) {
			t.Errorf("runtime.PopulateQueryParameters(msg, %v, %v = %v; want %v", spec.values, spec.filter, got, want)
		}
	}
}
func TestPopulateParametersWithFilters(t *testing.T) {
	for _, spec := range []struct {
		values url.Values
		filter *utilities.DoubleArray
		want   proto.Message
	}{
		{
			values: url.Values{
				"bool_value":     {"true"},
				"string_value":   {"str"},
				"repeated_value": {"a", "b", "c"},
			},
			filter: utilities.NewDoubleArray([][]string{
				{"bool_value"}, {"repeated_value"},
			}),
			want: &proto3Message{
				StringValue: "str",
			},
		},
		{
			values: url.Values{
				"nested.nested.bool_value":   {"true"},
				"nested.nested.string_value": {"str"},
				"nested.string_value":        {"str"},
				"string_value":               {"str"},
			},
			filter: utilities.NewDoubleArray([][]string{
				{"nested"},
			}),
			want: &proto3Message{
				StringValue: "str",
			},
		},
		{
			values: url.Values{
				"nested.nested.bool_value":   {"true"},
				"nested.nested.string_value": {"str"},
				"nested.string_value":        {"str"},
				"string_value":               {"str"},
			},
			filter: utilities.NewDoubleArray([][]string{
				{"nested", "nested"},
			}),
			want: &proto3Message{
				Nested: &proto2Message{
					StringValue: proto.String("str"),
				},
				StringValue: "str",
			},
		},
		{
			values: url.Values{
				"nested.nested.bool_value":   {"true"},
				"nested.nested.string_value": {"str"},
				"nested.string_value":        {"str"},
				"string_value":               {"str"},
			},
			filter: utilities.NewDoubleArray([][]string{
				{"nested", "nested", "string_value"},
			}),
			want: &proto3Message{
				Nested: &proto2Message{
					StringValue: proto.String("str"),
					Nested: &proto3Message{
						BoolValue: true,
					},
				},
				StringValue: "str",
			},
		},
	} {
		msg := proto.Clone(spec.want)
		msg.Reset()
		err := runtime.PopulateQueryParameters(msg, spec.values, spec.filter)
		if err != nil {
			t.Errorf("runtime.PoplateQueryParameters(msg, %v, %v) failed with %v; want success", spec.values, spec.filter, err)
			continue
		}
		if got, want := msg, spec.want; !proto.Equal(got, want) {
			t.Errorf("runtime.PopulateQueryParameters(msg, %v, %v = %v; want %v", spec.values, spec.filter, got, want)
		}
	}
}
Exemple #4
0
func TestAdd(t *testing.T) {
	for _, spec := range []struct {
		tokens [][]string
		want   utilities.DoubleArray
	}{
		{
			want: utilities.DoubleArray{
				Encoding: make(map[string]int),
			},
		},
		{
			tokens: [][]string{{"foo"}},
			want: utilities.DoubleArray{
				Encoding: map[string]int{"foo": 0},
				Base:     []int{1, 1, 0},
				Check:    []int{0, 1, 2},
				// 0: ^
				// 1: ^foo
				// 2: ^foo$
			},
		},
		{
			tokens: [][]string{{"foo"}, {"bar"}},
			want: utilities.DoubleArray{
				Encoding: map[string]int{
					"foo": 0,
					"bar": 1,
				},
				Base:  []int{1, 1, 2, 0, 0},
				Check: []int{0, 1, 1, 2, 3},
				// 0: ^
				// 1: ^foo
				// 2: ^bar
				// 3: ^foo$
				// 4: ^bar$
			},
		},
		{
			tokens: [][]string{{"foo", "bar"}, {"foo", "baz"}},
			want: utilities.DoubleArray{
				Encoding: map[string]int{
					"foo": 0,
					"bar": 1,
					"baz": 2,
				},
				Base:  []int{1, 1, 1, 2, 0, 0},
				Check: []int{0, 1, 2, 2, 3, 4},
				// 0: ^
				// 1: ^foo
				// 2: ^foo.bar
				// 3: ^foo.baz
				// 4: ^foo.bar$
				// 5: ^foo.baz$
			},
		},
		{
			tokens: [][]string{{"foo", "bar"}, {"foo", "baz"}, {"qux"}},
			want: utilities.DoubleArray{
				Encoding: map[string]int{
					"foo": 0,
					"bar": 1,
					"baz": 2,
					"qux": 3,
				},
				Base:  []int{1, 1, 1, 2, 3, 0, 0, 0},
				Check: []int{0, 1, 2, 2, 1, 3, 4, 5},
				// 0: ^
				// 1: ^foo
				// 2: ^foo.bar
				// 3: ^foo.baz
				// 4: ^qux
				// 5: ^foo.bar$
				// 6: ^foo.baz$
				// 7: ^qux$
			},
		},
		{
			tokens: [][]string{
				{"foo", "bar"},
				{"foo", "baz", "bar"},
				{"qux", "foo"},
			},
			want: utilities.DoubleArray{
				Encoding: map[string]int{
					"foo": 0,
					"bar": 1,
					"baz": 2,
					"qux": 3,
				},
				Base:  []int{1, 1, 1, 5, 8, 0, 3, 0, 5, 0},
				Check: []int{0, 1, 2, 2, 1, 3, 4, 7, 5, 9},
				// 0: ^
				// 1: ^foo
				// 2: ^foo.bar
				// 3: ^foo.baz
				// 4: ^qux
				// 5: ^foo.bar$
				// 6: ^foo.baz.bar
				// 7: ^foo.baz.bar$
				// 8: ^qux.foo
				// 9: ^qux.foo$
			},
		},
	} {
		da := utilities.NewDoubleArray(spec.tokens)
		if got, want := da.Encoding, spec.want.Encoding; !reflect.DeepEqual(got, want) {
			t.Errorf("da.Encoding = %v; want %v; tokens = %#v", got, want, spec.tokens)
		}
		if got, want := da.Base, spec.want.Base; !compareArray(got, want) {
			t.Errorf("da.Base = %v; want %v; tokens = %#v", got, want, spec.tokens)
		}
		if got, want := da.Check, spec.want.Check; !compareArray(got, want) {
			t.Errorf("da.Check = %v; want %v; tokens = %#v", got, want, spec.tokens)
		}
	}
}