Skip to content

Commit

Permalink
fix(o2k): change regex prio to int from uint (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske authored Mar 26, 2024
1 parent 0c28239 commit 497a692
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 5 deletions.
36 changes: 32 additions & 4 deletions jsonbasics/jsonbasics.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,9 @@ func GetBoolIndex(arr []interface{}, index int) (bool, error) {
func GetUInt64Field(object map[string]interface{}, fieldName string) (uint64, error) {
value, err := GetFloat64Field(object, fieldName)
if err == nil {
if value == float64(int(value)) {
return uint64(value), nil
uivalue := uint64(value)
if value == float64(uivalue) {
return uivalue, nil
}
}
return 0, fmt.Errorf("expected key '%s' to be an unsigned integer", fieldName)
Expand All @@ -211,13 +212,40 @@ func GetUInt64Field(object map[string]interface{}, fieldName string) (uint64, er
func GetUInt64Index(arr []interface{}, index int) (uint64, error) {
value, err := GetFloat64Index(arr, index)
if err == nil {
if value == float64(int(value)) {
return uint64(value), nil
uivalue := uint64(value)
if value == float64(uivalue) {
return uivalue, nil
}
}
return 0, fmt.Errorf("expected index '%d' to be an unsigned integer", index)
}

// GetInt64Field returns an int64 from an object field. Returns an error if the field
// is not an integer, or is not found.
func GetInt64Field(object map[string]interface{}, fieldName string) (int64, error) {
value, err := GetFloat64Field(object, fieldName)
if err == nil {
ivalue := int64(value)
if value == float64(ivalue) {
return ivalue, nil
}
}
return 0, fmt.Errorf("expected key '%s' to be an integer", fieldName)
}

// GetInt64Index returns an int64-value from an array index. Returns an error if the entry
// is not an integer.
func GetInt64Index(arr []interface{}, index int) (int64, error) {
value, err := GetFloat64Index(arr, index)
if err == nil {
ivalue := int64(value)
if value == float64(ivalue) {
return ivalue, nil
}
}
return 0, fmt.Errorf("expected index '%d' to be an integer", index)
}

// GetFloat64Field returns a float64 from an object field. Returns an error if the field
// is not a float, or is not found.
func GetFloat64Field(object map[string]interface{}, fieldName string) (float64, error) {
Expand Down
114 changes: 114 additions & 0 deletions jsonbasics/jsonbasics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,118 @@ var _ = Describe("jsonbasics", func() {
PIt("still to do", func() {
})
})

Describe("GetFloat64Index", func() {
It("returns the float or error", func() {
data := []interface{}{
1.5,
-1.5,
"text",
}
val, err := GetFloat64Index(data, 0)
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(1.5))
val, err = GetFloat64Index(data, 1)
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(-1.5))
val, err = GetFloat64Index(data, 2)
Expect(err).To(MatchError("expected index '2' to be a float"))
Expect(val).To(BeEquivalentTo(0))
})
})

Describe("GetFloat64Field", func() {
It("returns the float or error", func() {
data := map[string]interface{}{
"one": 1.5,
"two": -1.5,
"three": "text",
}
val, err := GetFloat64Field(data, "one")
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(1.5))
val, err = GetFloat64Field(data, "two")
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(-1.5))
val, err = GetFloat64Field(data, "three")
Expect(err).To(MatchError("expected key 'three' to be a float"))
Expect(val).To(BeEquivalentTo(0))
})
})

Describe("GetInt64Index", func() {
It("returns the integer or error", func() {
data := []interface{}{
1,
-1,
1.5,
}
val, err := GetInt64Index(data, 0)
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(int64(1)))
val, err = GetInt64Index(data, 1)
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(int64(-1)))
val, err = GetInt64Index(data, 2)
Expect(err).To(MatchError("expected index '2' to be an integer"))
Expect(val).To(BeEquivalentTo(0))
})
})

Describe("GetInt64Field", func() {
It("returns the integer or error", func() {
data := map[string]interface{}{
"one": 1,
"two": -1,
"three": 1.5,
}
val, err := GetInt64Field(data, "one")
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(int64(1)))
val, err = GetInt64Field(data, "two")
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(int64(-1)))
val, err = GetInt64Field(data, "three")
Expect(err).To(MatchError("expected key 'three' to be an integer"))
Expect(val).To(BeEquivalentTo(0))
})
})

Describe("GetUInt64Index", func() {
It("Returns the unsigned integer or error", func() {
data := []interface{}{
uint64(1),
uint64(2),
1.5,
}
val, err := GetUInt64Index(data, 0)
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(uint64(1)))
val, err = GetUInt64Index(data, 1)
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(uint64(2)))
val, err = GetUInt64Index(data, 2)
Expect(err).To(MatchError("expected index '2' to be an unsigned integer"))
Expect(val).To(BeEquivalentTo(0))
})
})

Describe("GetUInt64Field", func() {
It("returns the unsigned integer or error", func() {
data := map[string]interface{}{
"one": uint64(1),
"two": uint64(2),
"three": 1.5,
}
val, err := GetUInt64Field(data, "one")
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(uint64(1)))
val, err = GetUInt64Field(data, "two")
Expect(err).To(BeNil())
Expect(val).To(BeEquivalentTo(uint64(2)))
val, err = GetUInt64Field(data, "three")
Expect(err).To(MatchError("expected key 'three' to be an unsigned integer"))
Expect(val).To(BeEquivalentTo(0))
})
})
})
2 changes: 1 addition & 1 deletion openapi2kong/openapi2kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ func Convert(content []byte, opts O2kOptions) (map[string]interface{}, error) {
route["regex_priority"] = regexPriority
} else {
// a regex_priority was provided in the defaults
currentRegexPrio, err := jsonbasics.GetUInt64Field(route, "regex_priority")
currentRegexPrio, err := jsonbasics.GetInt64Field(route, "regex_priority")
if err != nil {
return nil, fmt.Errorf("failed to parse 'regex_priority' from route defaults: %w", err)
}
Expand Down

0 comments on commit 497a692

Please sign in to comment.