Skip to content

Commit

Permalink
Add info about builtins and operators to docgen tool
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Feb 27, 2020
1 parent 79ed70d commit 10df616
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions docgen/docgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 10df616

Please sign in to comment.