Skip to content

Commit

Permalink
Merge pull request #36 from nlnwa/report-filtering
Browse files Browse the repository at this point in the history
Improved report filtering
  • Loading branch information
Langvann authored Apr 1, 2022
2 parents d383206 + e5dd1c2 commit 1c60239
Show file tree
Hide file tree
Showing 16 changed files with 399 additions and 331 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ require (
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/pkg/errors v0.9.1
github.com/pquerna/cachecontrol v0.0.0-20200921180117-858c6e7e6b7e // indirect
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 // indirect
github.com/sirupsen/logrus v1.7.0
github.com/spf13/afero v1.4.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
Expand Down
54 changes: 1 addition & 53 deletions go.sum

Large diffs are not rendered by default.

62 changes: 28 additions & 34 deletions src/apiutil/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@ import (
configV1 "github.com/nlnwa/veidemann-api/go/config/v1"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"reflect"
"strconv"
"strings"
)

func CreateSelector(labelString string) []string {
var result []string
if labelString != "" {
result = strings.Split(labelString, ",")
return strings.Split(labelString, ",")
}
return result
return nil
}

func CreateTemplateFilter(filterString string, templateObj proto.Message) (*commonsV1.FieldMask, proto.Message, error) {
func CreateTemplateFilter(filterString string, obj proto.Message, mask *commonsV1.FieldMask) error {
q := strings.Split(filterString, "=")
mask := &commonsV1.FieldMask{}
mask.Paths = append(mask.Paths, q[0])
obj := templateObj
path := strings.TrimRight(q[0], "+-")
value := q[1]
tokens := strings.Split(path, ".")
Expand All @@ -51,13 +47,13 @@ func CreateTemplateFilter(filterString string, templateObj proto.Message) (*comm
for i := 0; i < msgType.Fields().Len(); i++ {
sa[i] = msgType.Fields().Get(i).JSONName()
}
return nil, templateObj, fmt.Errorf("no field with name '%s' in '%s'. Valid field names: %s", token, msgType.FullName(), strings.Join(sa, ", "))
return fmt.Errorf("no field with name '%s' in '%s'. Valid field names: %s", token, msgType.FullName(), strings.Join(sa, ", "))
}
switch fieldType.Kind() {
case protoreflect.BoolKind:
v, err := strconv.ParseBool(value)
if err != nil {
return nil, templateObj, fmt.Errorf("error converting %v to boolean: %w", value, err)
return fmt.Errorf("error converting %v to boolean: %w", value, err)
}
setValue(protoreflect.ValueOfBool(v), obj, fieldType)

Expand All @@ -68,7 +64,7 @@ func CreateTemplateFilter(filterString string, templateObj proto.Message) (*comm
case protoreflect.Sfixed32Kind:
v, err := strconv.ParseInt(value, 0, 32)
if err != nil {
return nil, templateObj, fmt.Errorf("error converting %v to int: %w", value, err)
return fmt.Errorf("error converting %v to int: %w", value, err)
}
setValue(protoreflect.ValueOfInt32(int32(v)), obj, fieldType)

Expand All @@ -79,7 +75,7 @@ func CreateTemplateFilter(filterString string, templateObj proto.Message) (*comm
case protoreflect.Sfixed64Kind:
v, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return nil, templateObj, fmt.Errorf("error converting %v to int: %w", value, err)
return fmt.Errorf("error converting %v to int: %w", value, err)
}
setValue(protoreflect.ValueOfInt64(v), obj, fieldType)

Expand All @@ -88,7 +84,7 @@ func CreateTemplateFilter(filterString string, templateObj proto.Message) (*comm
case protoreflect.Fixed32Kind:
v, err := strconv.ParseUint(value, 0, 32)
if err != nil {
return nil, templateObj, fmt.Errorf("error converting %v to uint: %w", value, err)
return fmt.Errorf("error converting %v to uint: %w", value, err)
}
setValue(protoreflect.ValueOfUint32(uint32(v)), obj, fieldType)

Expand All @@ -97,21 +93,21 @@ func CreateTemplateFilter(filterString string, templateObj proto.Message) (*comm
case protoreflect.Fixed64Kind:
v, err := strconv.ParseUint(value, 0, 64)
if err != nil {
return nil, templateObj, fmt.Errorf("error converting %v to uint: %w", value, err)
return fmt.Errorf("error converting %v to uint: %w", value, err)
}
setValue(protoreflect.ValueOfUint64(v), obj, fieldType)

case protoreflect.FloatKind:
v, err := strconv.ParseFloat(value, 32)
if err != nil {
return nil, templateObj, fmt.Errorf("error converting %v to float: %w", value, err)
return fmt.Errorf("error converting %v to float: %w", value, err)
}
setValue(protoreflect.ValueOfFloat32(float32(v)), obj, fieldType)

case protoreflect.DoubleKind:
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, templateObj, fmt.Errorf("error converting %v to float: %w", value, err)
return fmt.Errorf("error converting %v to float: %w", value, err)
}
setValue(protoreflect.ValueOfFloat64(v), obj, fieldType)

Expand All @@ -128,7 +124,7 @@ func CreateTemplateFilter(filterString string, templateObj proto.Message) (*comm
for i := 0; i < fieldType.Enum().Values().Len(); i++ {
sa[i] = string(fieldType.Enum().Values().Get(i).Name())
}
return nil, templateObj, fmt.Errorf("not a valid enum value '%s' for '%s'. Valid values: %s", value, fieldType.FullName(), strings.Join(sa, ", "))
return fmt.Errorf("not a valid enum value '%s' for '%s'. Valid values: %s", value, fieldType.FullName(), strings.Join(sa, ", "))
}
setValue(protoreflect.ValueOfEnum(enumVal.Number()), obj, fieldType)

Expand All @@ -149,14 +145,14 @@ func CreateTemplateFilter(filterString string, templateObj proto.Message) (*comm
case *configV1.ConfigRef:
kindId := strings.SplitN(value, ":", 2)
if len(kindId) != 2 {
return nil, templateObj, fmt.Errorf("not a valid configRef value %v for: %v. ConfigRef should have format [kind]:[id]", value, fieldType.FullName())
return fmt.Errorf("not a valid configRef value %v for: %v. ConfigRef should have format [kind]:[id]", value, fieldType.FullName())
}
m.Kind = configV1.Kind(configV1.Kind_value[kindId[0]])
m.Id = kindId[1]
case *configV1.Label:
keyVal := strings.SplitN(value, ":", 2)
if len(keyVal) != 2 {
return nil, templateObj, fmt.Errorf("not a valid label value %v for: %v. Label should have format [name]:[value]", value, fieldType.FullName())
return fmt.Errorf("not a valid label value %v for: %v. Label should have format [name]:[value]", value, fieldType.FullName())
}
m.Key = keyVal[0]
m.Value = keyVal[1]
Expand All @@ -166,7 +162,7 @@ func CreateTemplateFilter(filterString string, templateObj proto.Message) (*comm
}
}

return mask, templateObj, nil
return nil
}

func setValue(v protoreflect.Value, obj proto.Message, fieldType protoreflect.FieldDescriptor) {
Expand All @@ -179,27 +175,25 @@ func setValue(v protoreflect.Value, obj proto.Message, fieldType protoreflect.Fi
}

func CreateListRequest(kind configV1.Kind, ids []string, name string, labelString string, filterString string, pageSize int32, page int32) (*configV1.ListRequest, error) {
selector := CreateSelector(labelString)

request := &configV1.ListRequest{}
request.Kind = kind
request.Id = ids
request.NameRegex = name
request.LabelSelector = selector

request.Offset = page
request.PageSize = pageSize
request := &configV1.ListRequest{
Kind: kind,
Id: ids,
NameRegex: name,
LabelSelector: CreateSelector(labelString),
Offset: page,
PageSize: pageSize,
}

if filterString != "" {
m, o, err := CreateTemplateFilter(filterString, &configV1.ConfigObject{})
if err != nil {
queryMask := new(commonsV1.FieldMask)
queryTemplate := new(configV1.ConfigObject)
if err := CreateTemplateFilter(filterString, queryTemplate, queryMask); err != nil {
return nil, err
}
request.QueryMask = m
request.QueryTemplate = o.(*configV1.ConfigObject)
request.QueryMask = queryMask
request.QueryTemplate = queryTemplate
}

return request, nil
}

var typeRegistry = make(map[string]reflect.Type)
9 changes: 6 additions & 3 deletions src/apiutil/apiutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package apiutil

import (
import (
commonsV1 "github.com/nlnwa/veidemann-api/go/commons/v1"
configV1 "github.com/nlnwa/veidemann-api/go/config/v1"
frontierV1 "github.com/nlnwa/veidemann-api/go/frontier/v1"
Expand Down Expand Up @@ -177,12 +177,15 @@ func TestCreateTemplateFilter(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotMask, gotTemplate, err := CreateTemplateFilter(tt.args.filterString, tt.args.templateObj)
gotMask := new(commonsV1.FieldMask)
gotTemplate := tt.args.templateObj

err := CreateTemplateFilter(tt.args.filterString, gotTemplate, gotMask)
if (err != nil) != tt.wantErr {
t.Errorf("CreateTemplateFilter() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !proto.Equal(gotMask, tt.wantMask) {
if tt.wantMask != nil && !proto.Equal(gotMask, tt.wantMask) {
t.Errorf("CreateTemplateFilter() gotMask = %v, wantMask %v", gotMask, tt.wantMask)
}
if !proto.Equal(gotTemplate, tt.wantTemplate) {
Expand Down
6 changes: 0 additions & 6 deletions src/cmd/logconfig/logconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,4 @@ var LogconfigCmd = &cobra.Command{
Use: "logconfig",
Short: "Configure logging",
Long: `Configure logging.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

func init() {
}
Loading

0 comments on commit 1c60239

Please sign in to comment.