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{