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

163 lines
4.9 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 %}{% end %}
{%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 %}
//
2017-01-24 15:09:23 +00:00
// parameters:{% for _, p := range c.Parameters %}{% if p.Optional %}{% continue %}{% end %}
// {%s= p.String() %}{% if p.Optional %} (optional){% end %}{% endfor %}{% endif %}
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.RetValueList(d, domains)
if retValueList != "" {
retValueList += ", "
}
b64ret := c.Base64EncodedRetParam()
// determine if there's a conditional return value with it
b64cond := false
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
}
}
%}
// Do executes {%s= c.ProtoName(d) %}.{% if len(c.Returns) > 0 %}
//
2017-01-26 07:28:34 +00:00
// returns:{% for _, p := range c.Returns %}{% if p.Name == internal.Base64EncodedParamName %}{% continue %}{% end %}
2017-01-24 15:09:23 +00:00
// {%s= p.String() %}{% endfor %}{% endif %}
2017-01-26 07:28:34 +00:00
func (p *{%s= typ %}) Do(ctxt context.Context, h cdp.FrameHandler) ({%s= retTypeList %}err error) {
2017-01-24 15:09:23 +00:00
if ctxt == nil {
ctxt = context.Background()
}{% if !hasEmptyParams %}
// marshal
buf, err := easyjson.Marshal(p)
if err != nil {
return {%s= emptyRet %}err
}{% endif %}
// execute
2017-01-26 07:28:34 +00:00
ch := h.Execute(ctxt, cdp.{%s= c.CommandMethodType(d) %}, {% if hasEmptyParams %}cdp.Empty{% else %}easyjson.RawMessage(buf){% end %})
2017-01-24 15:09:23 +00:00
// read response
select {
case res := <-ch:
if res == nil {
2017-01-26 07:28:34 +00:00
return {%s= emptyRet %}cdp.ErrChannelClosed
2017-01-24 15:09:23 +00:00
}
switch v := res.(type) {
case easyjson.RawMessage:{% if !hasEmptyRet %}
// unmarshal
var r {%s= c.CommandReturnsType() %}
err = easyjson.Unmarshal(v, &r)
if err != nil {
2017-01-26 07:28:34 +00:00
return {%s= emptyRet %}cdp.ErrInvalidResult
2017-01-24 15:09:23 +00:00
}{% if b64ret != nil %}
// decode
var dec []byte{% if b64cond %}
if r.Base64encoded {{% endif %}
dec, err = base64.StdEncoding.DecodeString(r.{%s= b64ret.GoName(false) %})
if err != nil {
return nil, err
}{% if b64cond %}
} else {
dec = []byte(r.{%s= b64ret.GoName(false) %})
}{% endif %}{% endif %}
{% endif %}
return {%s= retValueList %}nil
case error:
return {%s= emptyRet %}v
}
case <-ctxt.Done():
2017-01-26 07:28:34 +00:00
return {%s= emptyRet %}cdp.ErrContextDone
2017-01-24 15:09:23 +00:00
}
2017-01-26 07:28:34 +00:00
return {%s= emptyRet %}cdp.ErrUnknownResult
2017-01-24 15:09:23 +00:00
}
{% endfunc %}