Skip to content

Commit

Permalink
chore: add jp functions docs (#52)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly authored Oct 4, 2023
1 parent 8906b0a commit c8a9c54
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ $(PACKAGE_SHIM): $(GOPATH_SHIM)
codegen-register: $(PACKAGE_SHIM) $(REGISTER_GEN) ## Generate types registrations
@echo Generate registration... >&2
@GOPATH=$(GOPATH_SHIM) $(REGISTER_GEN) \
--go-header-file=./.hack/boilerplate.go.txt \
--go-header-file=./hack/boilerplate.go.txt \
--input-dirs=$(INPUT_DIRS)

.PHONY: codegen-deepcopy
codegen-deepcopy: $(PACKAGE_SHIM) $(DEEPCOPY_GEN) ## Generate deep copy functions
@echo Generate deep copy functions... >&2
@GOPATH=$(GOPATH_SHIM) $(DEEPCOPY_GEN) \
--go-header-file=./.hack/boilerplate.go.txt \
--go-header-file=./hack/boilerplate.go.txt \
--input-dirs=$(INPUT_DIRS) \
--output-file-base=zz_generated.deepcopy

Expand Down Expand Up @@ -109,8 +109,14 @@ codegen-cli-docs: build ## Generate CLI docs
@rm -rf docs/user/commands && mkdir -p docs/user/commands
@./kyverno-json docs -o docs/user/commands --autogenTag=false

.PHONY: codegen-jp-docs
codegen-jp-docs: ## Generate JP docs
@echo Generate jp docs... >&2
@rm -rf docs/user/jp && mkdir -p docs/user/jp
@go run ./hack/docs/jp/main.go > docs/user/jp/functions.md

.PHONY: codegen-docs
codegen-docs: codegen-api-docs-md codegen-cli-docs ## Generate docs
codegen-docs: codegen-api-docs-md codegen-cli-docs codegen-jp-docs ## Generate docs

.PHONY: codegen-all
codegen-all: codegen-crds codegen-deepcopy codegen-register codegen-docs ## Rebuild all generated code and docs
Expand Down
1 change: 1 addition & 0 deletions docs/user/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ This documentation contains the following user docs:

- [API reference docs](./apis/README.md)
- [CLI commands docs](./commands/kyverno-json.md)
- [JMESPath functions docs](./jp/functions.md)
56 changes: 56 additions & 0 deletions docs/user/jp/functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# jp docs

## built-in functions

| Name | Signature |
|---|---|
| abs | `abs(number)` |
| avg | `avg(array[number])` |
| ceil | `ceil(number)` |
| contains | `contains(array\|string, any)` |
| ends_with | `ends_with(string, string)` |
| find_first | `find_first(string, string, number, number)` |
| find_last | `find_last(string, string, number, number)` |
| floor | `floor(number)` |
| from_items | `from_items(array[array])` |
| group_by | `group_by(array, expref)` |
| items | `items(object)` |
| join | `join(string, array[string])` |
| keys | `keys(object)` |
| length | `length(string\|array\|object)` |
| lower | `lower(string)` |
| map | `map(expref, array)` |
| max | `max(array[number]\|array[string])` |
| max_by | `max_by(array, expref)` |
| merge | `merge(object)` |
| min | `min(array[number]\|array[string])` |
| min_by | `min_by(array, expref)` |
| not_null | `not_null(any)` |
| pad_left | `pad_left(string, number, string)` |
| pad_right | `pad_right(string, number, string)` |
| replace | `replace(string, string, string, number)` |
| reverse | `reverse(array\|string)` |
| sort | `sort(array[string]\|array[number])` |
| sort_by | `sort_by(array, expref)` |
| split | `split(string, string, number)` |
| starts_with | `starts_with(string, string)` |
| sum | `sum(array[number])` |
| to_array | `to_array(any)` |
| to_number | `to_number(any)` |
| to_string | `to_string(any)` |
| trim | `trim(string, string)` |
| trim_left | `trim_left(string, string)` |
| trim_right | `trim_right(string, string)` |
| type | `type(any)` |
| upper | `upper(string)` |
| values | `values(object)` |
| zip | `zip(array, array)` |

## custom functions

| Name | Signature |
|---|---|
| at | `at(array, any)` |
| concat | `concat(string, string)` |
| wildcard | `wildcard(string, string)` |

File renamed without changes.
46 changes: 46 additions & 0 deletions hack/docs/jp/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"strings"

jpfunctions "github.com/jmespath-community/go-jmespath/pkg/functions"
"github.com/kyverno/kyverno-json/pkg/engine/template/functions"
)

func main() {
fmt.Println("# jp docs")
fmt.Println()
fmt.Println("## built-in functions")
fmt.Println()
printFunctions(jpfunctions.GetDefaultFunctions()...)
fmt.Println()
fmt.Println("## custom functions")
fmt.Println()
printFunctions(functions.GetFunctions()...)
fmt.Println()
}

func printFunctions(funcs ...jpfunctions.FunctionEntry) {
fmt.Println("| Name | Signature |")
fmt.Println("|---|---|")
for _, function := range funcs {
fmt.Println("|", function.Name, "|", "`"+strings.ReplaceAll(functionString(function), "|", `\|`)+"`", "|")
}
}

func functionString(f jpfunctions.FunctionEntry) string {
if f.Name == "" {
return ""
}
var args []string
for _, a := range f.Arguments {
var aTypes []string
for _, t := range a.Types {
aTypes = append(aTypes, string(t))
}
args = append(args, strings.Join(aTypes, "|"))
}
output := fmt.Sprintf("%s(%s)", f.Name, strings.Join(args, ", "))
return output
}

0 comments on commit c8a9c54

Please sign in to comment.