Skip to content

Commit

Permalink
add error handling for func AsEntryXpath
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianczech committed Mar 8, 2024
1 parent 54f99e3 commit 96e300a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions pkg/translate/funcs.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package translate

import (
"errors"
"github.com/paloaltonetworks/pan-os-codegen/pkg/naming"
"strings"
)

func AsEntryXpath(location, xpath string) string {
func AsEntryXpath(location, xpath string) (string, error) {
if !strings.Contains(xpath, "$") || !strings.Contains(xpath, "}") {
return "", errors.New("$ followed by } should exists in xpath'")
}
xpath = strings.TrimSpace(strings.Split(strings.Split(xpath, "$")[1], "}")[0])
xpath = naming.CamelCase("", xpath, "", true)
return "util.AsEntryXpath([]string{o." + location + "." + xpath + "}),"
return "util.AsEntryXpath([]string{o." + location + "." + xpath + "}),", nil
}
2 changes: 1 addition & 1 deletion pkg/translate/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func TestAsEntryXpath(t *testing.T) {
// given

// when
asEntryXpath := AsEntryXpath("DeviceGroup", "{{ Entry $panorama_device }}")
asEntryXpath, _ := AsEntryXpath("DeviceGroup", "{{ Entry $panorama_device }}")

// then
assert.Equal(t, "util.AsEntryXpath([]string{o.DeviceGroup.PanoramaDevice}),", asEntryXpath)
Expand Down

0 comments on commit 96e300a

Please sign in to comment.