chromedp/cmd/chromedp-gen/templates/domain.qtpl

138 lines
4.5 KiB
Plaintext
Raw Normal View History

2017-01-24 15:09:23 +00:00
{% import (
2017-01-26 07:28:34 +00:00
"github.com/knq/chromedp/cmd/chromedp-gen/internal"
2017-01-24 15:09:23 +00:00
) %}
// DomainTemplate is the template for a single domain.
2017-01-26 07:28:34 +00:00
{% func DomainTemplate(d *internal.Domain, domains []*internal.Domain) %}
{%s= FileImportTemplate(map[string]string{
2017-01-26 07:28:34 +00:00
*internal.FlagPkg: "cdp",
}) %}
2017-01-24 15:09:23 +00:00
{% for _, c := range d.Commands %}
{%s= CommandTemplate(c, d, domains) %}
{% endfor %}
{% endfunc %}
// CommandTemplate is the general command template.
2017-01-26 07:28:34 +00:00
{% func CommandTemplate(c *internal.Type, d *internal.Domain, domains []*internal.Domain) %}
2017-01-24 15:09:23 +00:00
{% code /* add *Param type */ %}
2017-01-26 07:28:34 +00:00
{%s= TypeTemplate(c, internal.CommandTypePrefix, internal.CommandTypeSuffix, d, domains, nil, false, true) %}
2017-01-24 15:09:23 +00:00
{% code /* add Command func */ %}
{%s= CommandFuncTemplate(c, d, domains) %}
{% code /* add param funcs (only if it has parameters and a returns). */ %}
{% if len(c.Parameters) != 0 %}{% for _, p := range c.Parameters %}{% if !p.Optional %}{% continue %}{% endif %}
2017-01-24 15:09:23 +00:00
{%s= CommandOptionFuncTemplate(p, c, d, domains) %}
{% endfor %}{% endif %}
{% code /* add *Returns type */ %}
{% if len(c.Returns) != 0 %}
2017-01-26 07:28:34 +00:00
{%s= TypeTemplate(&internal.Type{
2017-01-24 15:09:23 +00:00
ID: c.Name,
2017-01-26 07:28:34 +00:00
Type: internal.TypeObject,
2017-01-24 15:09:23 +00:00
Description: "Return values.",
Properties: c.Returns,
2017-01-26 07:28:34 +00:00
}, internal.CommandReturnsPrefix, internal.CommandReturnsSuffix, d, domains, nil, false, false) %}
2017-01-24 15:09:23 +00:00
{% endif %}
{% code /* add CommandParams.Do func */ %}
{%s= CommandDoFuncTemplate(c, d, domains) %}
{% endfunc %}
// CommandFuncTemplate is the command func template.
2017-01-26 07:28:34 +00:00
{% func CommandFuncTemplate(c *internal.Type, d *internal.Domain, domains []*internal.Domain) %}{% code
2017-01-24 15:09:23 +00:00
cmdName := c.CamelName()
typ := c.CommandType()
%}
2017-01-26 07:28:34 +00:00
{%s= formatComment(c.GetDescription(), "", cmdName + " ") %}{% if len(c.Parameters) > 0 %}
//
// parameters:{% for _, p := range c.Parameters %}{% if p.Optional %}{% continue %}{% endif %}
// {%s= p.String() %}{% if p.Optional %} (optional){% endif %}{% endfor %}{% endif %}
2017-01-24 15:09:23 +00:00
func {%s= cmdName %}({%s= c.ParamList(d, domains, false) %}) *{%s= typ %}{
return &{%s= typ %}{{% for _, t := range c.Parameters %}{% if !t.Optional %}
{%s= t.GoName(false) %}: {%s= t.GoName(true) %},{% endif %}{% endfor %}
}
}
{% endfunc %}
// CommandOptionFuncTemplate is the command option func template.
2017-01-26 07:28:34 +00:00
{% func CommandOptionFuncTemplate(t *internal.Type, c *internal.Type, d *internal.Domain, domains []*internal.Domain) %}{% code
2017-01-24 15:09:23 +00:00
n := t.GoName(false)
2017-01-26 07:28:34 +00:00
optName := internal.OptionFuncPrefix+n+internal.OptionFuncSuffix
2017-01-24 15:09:23 +00:00
typ := c.CommandType()
v := t.GoName(true)
%}
2017-01-26 07:28:34 +00:00
{%s= formatComment(t.GetDescription(), "", optName + " ") %}
2017-01-24 15:09:23 +00:00
func (p {%s= typ %}) {%s= optName %}({%s= v %} {%s= t.GoType(d, domains) %}) *{%s= typ %}{
p.{%s= n %} = {%s= v %}
return &p
}
{% endfunc %}
// CommandDoFuncTemplate is the command do func template.
2017-01-26 07:28:34 +00:00
{% func CommandDoFuncTemplate(c *internal.Type, d *internal.Domain, domains[]*internal.Domain) %}{% code
2017-01-24 15:09:23 +00:00
typ := c.CommandType()
hasEmptyParams := len(c.Parameters) == 0
hasEmptyRet := len(c.Returns) == 0
emptyRet := c.EmptyRetList(d, domains)
if emptyRet != "" {
emptyRet += ", "
}
retTypeList := c.RetTypeList(d, domains)
if retTypeList != "" {
retTypeList += ", "
}
retValueList := c.RetNameList("res", d, domains)
2017-01-24 15:09:23 +00:00
if retValueList != "" {
retValueList += ", "
}
b64ret := c.Base64EncodedRetParam()
// determine if there's a conditional that indicates whether or not the
// returned value is b64 encoded.
var b64cond bool
2017-01-24 15:09:23 +00:00
for _, p := range c.Returns {
2017-01-26 07:28:34 +00:00
if p.Name == internal.Base64EncodedParamName {
2017-01-24 15:09:23 +00:00
b64cond = true
break
}
}
pval := "p"
if hasEmptyParams {
pval = "nil"
}
2017-01-24 15:09:23 +00:00
%}
// Do executes {%s= c.ProtoName(d) %} against the provided context and
// target handler.{% if !hasEmptyRet %}
2017-01-24 15:09:23 +00:00
//
// returns:{% for _, p := range c.Returns %}{% if p.Name == internal.Base64EncodedParamName %}{% continue %}{% endif %}
2017-01-24 15:09:23 +00:00
// {%s= p.String() %}{% endfor %}{% endif %}
func (p *{%s= typ %}) Do(ctxt context.Context, h cdp.Handler) ({%s= retTypeList %}err error) {{% if hasEmptyRet %}
return h.Execute(ctxt, cdp.{%s= c.CommandMethodType(d) %}, {%s= pval %}, nil){% else %}
// execute
var res {%s= c.CommandReturnsType() %}
err = h.Execute(ctxt, cdp.{%s= c.CommandMethodType(d) %}, {%s= pval %}, &res)
2017-01-24 15:09:23 +00:00
if err != nil {
return {%s= emptyRet %}err
}
{% if b64ret != nil %}
// decode
var dec []byte{% if b64cond %}
if res.Base64encoded {{% endif %}
dec, err = base64.StdEncoding.DecodeString(res.{%s= b64ret.GoName(false) %})
if err != nil {
return nil, err
}{% if b64cond %}
} else {
dec = []byte(res.{%s= b64ret.GoName(false) %})
}{% endif %}{% endif %}
return {%s= retValueList %}nil{% endif %}
2017-01-24 15:09:23 +00:00
}
{% endfunc %}