OSDN Git Service

change the ts (#284)
[bytom/vapor.git] / toolbar / federation / api / display.go
1 package api
2
3 import (
4         "github.com/vapor/errors"
5 )
6
7 var (
8         errMissingFilterKey  = errors.New("missing filter key")
9         errInvalidFilterType = errors.New("invalid filter type")
10 )
11
12 // Display defines how the data is displayed
13 type Display struct {
14         Filter map[string]interface{} `json:"filter"`
15         Sorter Sorter                 `json:"sort"`
16 }
17
18 type Sorter struct {
19         By    string `json:"by"`
20         Order string `json:"order"`
21 }
22
23 // GetFilterString give the filter keyword return the string value
24 func (d *Display) GetFilterString(filterKey string) (string, error) {
25         if _, ok := d.Filter[filterKey]; !ok {
26                 return "", errMissingFilterKey
27         }
28         switch val := d.Filter[filterKey].(type) {
29         case string:
30                 return val, nil
31         }
32         return "", errInvalidFilterType
33 }
34
35 // GetFilterNum give the filter keyword return the numeric value
36 func (d *Display) GetFilterNum(filterKey string) (interface{}, error) {
37         if _, ok := d.Filter[filterKey]; !ok {
38                 return 0, errMissingFilterKey
39         }
40         switch val := d.Filter[filterKey].(type) {
41         case int, int16, int32, int64, int8, uint, uint16, uint32, uint64, uint8, float32, float64:
42                 return val, nil
43         }
44
45         return 0, errInvalidFilterType
46 }
47
48 // GetFilterBoolean give the filter keyword return the boolean value
49 func (d *Display) GetFilterBoolean(filterKey string) (bool, error) {
50         if _, ok := d.Filter[filterKey]; !ok {
51                 return false, errMissingFilterKey
52         }
53         switch val := d.Filter[filterKey].(type) {
54         case bool:
55                 return val, nil
56         }
57         return false, errInvalidFilterType
58 }