Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement parseFloat for annotations #9195

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions internal/ingress/annotations/parser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ func (a ingAnnotations) parseInt(name string) (int, error) {
return 0, errors.ErrMissingAnnotations
}

func (a ingAnnotations) parseFloat32(name string) (float32, error) {
val, ok := a[name]
if ok {
i, err := strconv.ParseFloat(val, 32)
if err != nil {
return 0, errors.NewInvalidAnnotationContent(name, val)
}
return float32(i), nil
}
return 0, errors.ErrMissingAnnotations
}

func checkAnnotation(name string, ing *networking.Ingress) error {
if ing == nil || len(ing.GetAnnotations()) == 0 {
return errors.ErrMissingAnnotations
Expand Down Expand Up @@ -122,6 +134,16 @@ func GetIntAnnotation(name string, ing *networking.Ingress) (int, error) {
return ingAnnotations(ing.GetAnnotations()).parseInt(v)
}

// GetFloatAnnotation extracts a float32 from an Ingress annotation
func GetFloatAnnotation(name string, ing *networking.Ingress) (float32, error) {
v := GetAnnotationWithPrefix(name)
err := checkAnnotation(v, ing)
if err != nil {
return 0, err
}
return ingAnnotations(ing.GetAnnotations()).parseFloat32(v)
}

// GetAnnotationWithPrefix returns the prefix of ingress annotations
func GetAnnotationWithPrefix(suffix string) string {
return fmt.Sprintf("%v/%v", AnnotationsPrefix, suffix)
Expand Down
41 changes: 41 additions & 0 deletions internal/ingress/annotations/parser/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,47 @@ rewrite (?i)/arcgis/services/Utilities/Geometry/GeometryServer(.*)$ /arcgis/serv
}
}

func TestGetFloatAnnotation(t *testing.T) {
ing := buildIngress()

_, err := GetFloatAnnotation("", nil)
if err == nil {
t.Errorf("expected error but retuned nil")
}

tests := []struct {
name string
field string
value string
exp float32
expErr bool
}{
{"valid - A", "string", "1.5", 1.5, false},
{"valid - B", "string", "2", 2, false},
{"valid - C", "string", "100.0", 100, false},
}

data := map[string]string{}
ing.SetAnnotations(data)

for _, test := range tests {
data[GetAnnotationWithPrefix(test.field)] = test.value

s, err := GetFloatAnnotation(test.field, ing)
if test.expErr {
if err == nil {
t.Errorf("%v: expected error but retuned nil", test.name)
}
continue
}
if s != test.exp {
t.Errorf("%v: expected \"%v\" but \"%v\" was returned", test.name, test.exp, s)
}

delete(data, test.field)
}
}

func TestGetIntAnnotation(t *testing.T) {
ing := buildIngress()

Expand Down