示例#1
0
func newFilters(u url.URL, whitelist []string) []Filter {
	var filters []Filter
	queryValues := u.Query()

	for _, key := range whitelist {
		var filtersToAdd []Filter

		filterStr := queryValues.Get(key)
		if filterStr != "" {
			fieldName := translation.PropertyToField(key)

			csv := strings.Split(filterStr, ",")
			for _, s := range csv {
				if strings.HasPrefix(s, "gt:") {
					filtersToAdd = append(filtersToAdd, Filter{Field: fieldName, Operator: ">", Value: s[3:len(s)]})
				} else if strings.HasPrefix(s, "gte:") {
					filtersToAdd = append(filtersToAdd, Filter{Field: fieldName, Operator: ">=", Value: s[4:len(s)]})
				} else if strings.HasPrefix(s, "lt:") {
					filtersToAdd = append(filtersToAdd, Filter{Field: fieldName, Operator: "<", Value: s[3:len(s)]})
				} else if strings.HasPrefix(s, "lte:") {
					filtersToAdd = append(filtersToAdd, Filter{Field: fieldName, Operator: "<=", Value: s[4:len(s)]})
				}
			}

			// No comparison operator so default to equals
			if len(filtersToAdd) == 0 {
				filtersToAdd = append(filtersToAdd, Filter{Field: fieldName, Operator: "=", Value: filterStr})
			}

			filters = append(filters, filtersToAdd...)
		}
	}

	return filters
}
示例#2
0
func newSorters(u url.URL, whitelist []string) []Sorter {
	sorters := []Sorter{}
	sortStr := u.Query().Get("sort")

	if sortStr != "" {
		splitStrings := strings.Split(sortStr, ",")
		for _, s := range splitStrings {
			prefix := "ASC"
			field := s
			if string(s[0]) == "-" {
				prefix = "DESC"
				field = strings.TrimPrefix(s, "-")
			}

			if contains(field, whitelist) {
				sorters = append(sorters, Sorter{Field: translation.PropertyToField(field), Direction: prefix})
			}
		}
	}

	return sorters
}
示例#3
0
			}),
			Entry("with valid strings", InputCase{
				s:        "id",
				expected: "id",
			}),
			Entry("with valid strings", InputCase{
				s:        "rock",
				expected: "rock",
			}),
		)
	})

	Describe("#PropertyToField", func() {
		translator := func(i InputCase) {
			// When
			result := translation.PropertyToField(i.s)

			Expect(result).To(Equal(i.expected))
		}

		DescribeTable("Strings",
			translator,
			Entry("with valid strings", InputCase{
				s:        "rockId",
				expected: "RockID",
			}),
			Entry("with valid strings", InputCase{
				s:        "id",
				expected: "ID",
			}),
			Entry("with valid strings", InputCase{