{% import ( "github.com/knq/chromedp/cmd/chromedp-gen/internal" ) %} // DomainTemplate is the template for a single domain. {% func DomainTemplate(d *internal.Domain, domains []*internal.Domain) %} {%s= FileImportTemplate(map[string]string{ *internal.FlagPkg: "cdp", }) %} {% for _, c := range d.Commands %} {%s= CommandTemplate(c, d, domains) %} {% endfor %} {% endfunc %} // CommandTemplate is the general command template. {% func CommandTemplate(c *internal.Type, d *internal.Domain, domains []*internal.Domain) %} {% code /* add *Param type */ %} {%s= TypeTemplate(c, internal.CommandTypePrefix, internal.CommandTypeSuffix, d, domains, nil, false, true) %} {% 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 %} {%s= CommandOptionFuncTemplate(p, c, d, domains) %} {% endfor %}{% endif %} {% code /* add *Returns type */ %} {% if len(c.Returns) != 0 %} {%s= TypeTemplate(&internal.Type{ ID: c.Name, Type: internal.TypeObject, Description: "Return values.", Properties: c.Returns, }, internal.CommandReturnsPrefix, internal.CommandReturnsSuffix, d, domains, nil, false, false) %} {% endif %} {% code /* add CommandParams.Do func */ %} {%s= CommandDoFuncTemplate(c, d, domains) %} {% endfunc %} // CommandFuncTemplate is the command func template. {% func CommandFuncTemplate(c *internal.Type, d *internal.Domain, domains []*internal.Domain) %}{% code cmdName := c.CamelName() typ := c.CommandType() %} {%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 %} 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. {% func CommandOptionFuncTemplate(t *internal.Type, c *internal.Type, d *internal.Domain, domains []*internal.Domain) %}{% code n := t.GoName(false) optName := internal.OptionFuncPrefix+n+internal.OptionFuncSuffix typ := c.CommandType() v := t.GoName(true) %} {%s= formatComment(t.GetDescription(), "", optName + " ") %} 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. {% func CommandDoFuncTemplate(c *internal.Type, d *internal.Domain, domains[]*internal.Domain) %}{% code 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) if retValueList != "" { retValueList += ", " } b64ret := c.Base64EncodedRetParam() // determine if there's a conditional that indicates whether or not the // returned value is b64 encoded. b64cond := false for _, p := range c.Returns { if p.Name == internal.Base64EncodedParamName { b64cond = true break } } pval := "p" if hasEmptyParams { pval = "nil" } %} // Do executes {%s= c.ProtoName(d) %} against the provided context and // target handler.{% if !hasEmptyRet %} // // returns:{% for _, p := range c.Returns %}{% if p.Name == internal.Base64EncodedParamName %}{% continue %}{% endif %} // {%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) 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 %} } {% endfunc %}