From 10df61612919541db69236744d65cca3b7b4fd96 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Thu, 27 Feb 2020 09:02:11 +0300 Subject: [PATCH] Add info about builtins and operators to docgen tool --- docgen/docgen.go | 26 ++++++++++++++++++++++++++ docgen/docgen_test.go | 4 ++++ 2 files changed, 30 insertions(+) diff --git a/docgen/docgen.go b/docgen/docgen.go index 2fd9bf076..f4c78c8a1 100644 --- a/docgen/docgen.go +++ b/docgen/docgen.go @@ -32,6 +32,22 @@ type Type struct { Return *Type `json:"return,omitempty"` } +var ( + Operators = []string{"matches", "contains", "startsWith", "endsWith"} + Builtins = map[Identifier]*Type{ + "true": {Kind: "bool"}, + "false": {Kind: "bool"}, + "len": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}}, Return: &Type{Kind: "int"}}, + "all": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}}, + "none": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}}, + "any": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}}, + "one": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "bool"}}, + "filter": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "array", Type: &Type{Kind: "any"}}}, + "map": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "array", Type: &Type{Kind: "any"}}}, + "count": {Kind: "func", Arguments: []*Type{{Kind: "array", Type: &Type{Kind: "any"}}, {Kind: "func"}}, Return: &Type{Kind: "int"}}, + } +) + func CreateDoc(i interface{}) *Context { c := &Context{ Variables: make(map[Identifier]*Type), @@ -42,6 +58,16 @@ func CreateDoc(i interface{}) *Context { c.Variables[Identifier(name)] = c.use(t.Type, fromMethod(t.Method)) } + for _, op := range Operators { + c.Variables[Identifier(op)] = &Type{ + Kind: "operator", + } + } + + for builtin, t := range Builtins { + c.Variables[builtin] = t + } + return c } diff --git a/docgen/docgen_test.go b/docgen/docgen_test.go index 00fe379ea..b3cc3ff34 100644 --- a/docgen/docgen_test.go +++ b/docgen/docgen_test.go @@ -33,6 +33,8 @@ func (*Env) Duration(s string) Duration { } func TestCreateDoc(t *testing.T) { + Operators = nil + Builtins = nil doc := CreateDoc(&Env{}) expected := &Context{ Variables: map[Identifier]*Type{ @@ -96,6 +98,8 @@ func TestCreateDoc_FromMap(t *testing.T) { }{}, "Max": math.Max, } + Operators = nil + Builtins = nil doc := CreateDoc(env) expected := &Context{ Variables: map[Identifier]*Type{