in api/internal/handler/label/label.go [137:270]
func (h *Handler) List(c droplet.Context) (interface{}, error) {
input := c.Input().(*ListInput)
typ := input.Type
reqLabels, err := utils.GenLabelMap(input.Label)
if err != nil {
return &data.SpecCodeResponse{StatusCode: http.StatusBadRequest},
fmt.Errorf("%s: \"%s\"", err.Error(), input.Label)
}
var items []interface{}
switch typ {
case "route":
items = append(items, h.routeStore)
case "service":
items = append(items, h.serviceStore)
case "consumer":
items = append(items, h.consumerStore)
case "ssl":
items = append(items, h.sslStore)
case "upstream":
items = append(items, h.upstreamStore)
case "plugin_config":
items = append(items, h.pluginConfigStore)
case "all":
items = append(items, h.routeStore, h.serviceStore, h.upstreamStore,
h.sslStore, h.consumerStore, h.pluginConfigStore)
}
predicate := func(obj interface{}) bool {
var ls map[string]string
switch obj := obj.(type) {
case *entity.Route:
ls = obj.Labels
case *entity.Consumer:
ls = obj.Labels
case *entity.SSL:
ls = obj.Labels
case *entity.Service:
ls = obj.Labels
case *entity.Upstream:
ls = obj.Labels
case *entity.PluginConfig:
ls = obj.Labels
default:
return false
}
return utils.LabelContains(ls, reqLabels)
}
format := func(obj interface{}) interface{} {
val := reflect.ValueOf(obj).Elem()
l := val.FieldByName("Labels")
if l.IsNil() {
return nil
}
ls := l.Interface().(map[string]string)
return subsetOf(reqLabels, ls)
}
var totalRet = store.NewListOutput()
var existMap = make(map[string]struct{})
for _, item := range items {
ret, err := item.(store.Interface).List(c.Context(),
store.ListInput{
Predicate: predicate,
Format: format,
// Sort it later.
PageSize: 0,
PageNumber: 0,
Less: func(i, j interface{}) bool {
return true
},
},
)
if err != nil {
return nil, err
}
for _, r := range ret.Rows {
if r == nil {
continue
}
for k, v := range r.(map[string]string) {
key := fmt.Sprintf("%s:%s", k, v)
if _, exist := existMap[key]; exist {
continue
}
existMap[key] = struct{}{}
p := Pair{Key: k, Val: v}
totalRet.Rows = append(totalRet.Rows, p)
}
}
}
totalRet.TotalSize = len(totalRet.Rows)
sort.Slice(totalRet.Rows, func(i, j int) bool {
p1 := totalRet.Rows[i].(Pair)
p2 := totalRet.Rows[j].(Pair)
if strings.Compare(p1.Key, p2.Key) == 0 {
return strings.Compare(p1.Val, p2.Val) < 0
}
return strings.Compare(p1.Key, p2.Key) < 0
})
/* There are more than one store items,
So we need sort after getting all of labels.
*/
if input.PageSize > 0 && input.PageNumber > 0 {
skipCount := (input.PageNumber - 1) * input.PageSize
if skipCount > totalRet.TotalSize {
totalRet.Rows = []interface{}{}
return totalRet, nil
}
endIdx := skipCount + input.PageSize
if endIdx >= totalRet.TotalSize {
totalRet.Rows = totalRet.Rows[skipCount:]
return totalRet, nil
}
totalRet.Rows = totalRet.Rows[skipCount:endIdx]
}
return totalRet, nil
}