Fixing issue with missing target_type values in client package

This commit is contained in:
Kenneth Shaw 2017-12-22 10:01:21 +07:00
parent dd8119a9c5
commit 69e069d131
5 changed files with 232 additions and 51 deletions

View File

@ -2,7 +2,7 @@ package client
import "fmt" import "fmt"
//go:generate easyjson -omit_empty -pkg -output_filename easyjson.go //go:generate easyjson -omit_empty -output_filename easyjson.go chrome.go
// Chrome holds connection information for a Chrome, Edge, or Safari target. // Chrome holds connection information for a Chrome, Edge, or Safari target.
// //

View File

@ -2,6 +2,8 @@
// and related funcs. // and related funcs.
package client package client
//go:generate go run gen.go
import ( import (
"context" "context"
"encoding/json" "encoding/json"
@ -35,6 +37,14 @@ var (
ErrUnsupportedProtocolVersion = errors.New("unsupported protocol version") ErrUnsupportedProtocolVersion = errors.New("unsupported protocol version")
) )
// Target is the common interface for a Chrome Debugging Protocol target.
type Target interface {
String() string
GetID() string
GetType() TargetType
GetWebsocketURL() string
}
// Client is a Chrome Debugging Protocol client. // Client is a Chrome Debugging Protocol client.
type Client struct { type Client struct {
url string url string

View File

@ -67,67 +67,83 @@ func easyjsonC5a4559bEncodeGithubComKnqChromedpClient(out *jwriter.Writer, in Ch
first := true first := true
_ = first _ = first
if in.Description != "" { if in.Description != "" {
if !first { const prefix string = ",\"description\":"
out.RawByte(',') if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
} }
first = false
out.RawString("\"description\":")
out.String(string(in.Description)) out.String(string(in.Description))
} }
if in.DevtoolsURL != "" { if in.DevtoolsURL != "" {
if !first { const prefix string = ",\"devtoolsFrontendUrl\":"
out.RawByte(',') if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
} }
first = false
out.RawString("\"devtoolsFrontendUrl\":")
out.String(string(in.DevtoolsURL)) out.String(string(in.DevtoolsURL))
} }
if in.ID != "" { if in.ID != "" {
if !first { const prefix string = ",\"id\":"
out.RawByte(',') if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
} }
first = false
out.RawString("\"id\":")
out.String(string(in.ID)) out.String(string(in.ID))
} }
if in.Title != "" { if in.Title != "" {
if !first { const prefix string = ",\"title\":"
out.RawByte(',') if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
} }
first = false
out.RawString("\"title\":")
out.String(string(in.Title)) out.String(string(in.Title))
} }
if in.Type != "" { if in.Type != "" {
if !first { const prefix string = ",\"type\":"
out.RawByte(',') if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
} }
first = false
out.RawString("\"type\":")
(in.Type).MarshalEasyJSON(out) (in.Type).MarshalEasyJSON(out)
} }
if in.URL != "" { if in.URL != "" {
if !first { const prefix string = ",\"url\":"
out.RawByte(',') if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
} }
first = false
out.RawString("\"url\":")
out.String(string(in.URL)) out.String(string(in.URL))
} }
if in.WebsocketURL != "" { if in.WebsocketURL != "" {
if !first { const prefix string = ",\"webSocketDebuggerUrl\":"
out.RawByte(',') if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
} }
first = false
out.RawString("\"webSocketDebuggerUrl\":")
out.String(string(in.WebsocketURL)) out.String(string(in.WebsocketURL))
} }
if in.FaviconURL != "" { if in.FaviconURL != "" {
if !first { const prefix string = ",\"faviconURL\":"
out.RawByte(',') if first {
first = false
out.RawString(prefix[1:])
} else {
out.RawString(prefix)
} }
first = false
out.RawString("\"faviconURL\":")
out.String(string(in.FaviconURL)) out.String(string(in.FaviconURL))
} }
out.RawByte('}') out.RawByte('}')

142
client/gen.go Normal file
View File

@ -0,0 +1,142 @@
// +build ignore
package main
import (
"encoding/base64"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
"regexp"
"sort"
"github.com/knq/snaker"
)
const (
// chromiumSrc is the base chromium source repo location
chromiumSrc = "https://chromium.googlesource.com/chromium/src"
// devtoolsHTTPClientCc contains the target_type names.
devtoolsHTTPClientCc = chromiumSrc + "/+/master/chrome/test/chromedriver/chrome/devtools_http_client.cc?format=TEXT"
)
var (
flagOut = flag.String("out", "targettype.go", "out file")
typeAsStringRE = regexp.MustCompile(`type_as_string\s+==\s+"([^"]+)"`)
)
func main() {
flag.Parse()
// grab source
buf, err := grab(devtoolsHTTPClientCc)
if err != nil {
log.Fatal(err)
}
// find names
matches := typeAsStringRE.FindAllStringSubmatch(string(buf), -1)
names := make([]string, len(matches))
for i, m := range matches {
names[i] = m[1]
}
sort.Strings(names)
// process names
var constVals, decodeVals string
for _, n := range names {
name := snaker.SnakeToCamelIdentifier(n)
constVals += fmt.Sprintf("%s TargetType = \"%s\"\n", name, n)
decodeVals += fmt.Sprintf("case %s:\n*tt=%s\n", name, name)
}
err = ioutil.WriteFile(*flagOut, []byte(fmt.Sprintf(targetTypeSrc, constVals, decodeVals)), 0644)
if err != nil {
log.Fatal(err)
}
err = exec.Command("gofmt", "-w", "-s", *flagOut).Run()
if err != nil {
log.Fatal(err)
}
}
// grab retrieves a file from the chromium source code.
func grab(path string) ([]byte, error) {
res, err := http.Get(path)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
buf, err := base64.StdEncoding.DecodeString(string(body))
if err != nil {
return nil, err
}
return buf, nil
}
const (
targetTypeSrc = `package client
// Code generated by gen.go. DO NOT EDIT.
import (
// "errors"
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter"
)
// TargetType are the types of targets available in Chrome.
type TargetType string
// TargetType values.
const (
%s
)
// String satisfies stringer.
func (tt TargetType) String() string {
return string(tt)
}
// MarshalEasyJSON satisfies easyjson.Marshaler.
func (tt TargetType) MarshalEasyJSON(out *jwriter.Writer) {
out.String(string(tt))
}
// MarshalJSON satisfies json.Marshaler.
func (tt TargetType) MarshalJSON() ([]byte, error) {
return easyjson.Marshal(tt)
}
// UnmarshalEasyJSON satisfies easyjson.Unmarshaler.
func (tt *TargetType) UnmarshalEasyJSON(in *jlexer.Lexer) {
z := TargetType(in.String())
switch z {
%s
default:
// in.AddError(errors.New("unknown TargetType"))
*tt = z
}
}
// UnmarshalJSON satisfies json.Unmarshaler.
func (tt *TargetType) UnmarshalJSON(buf []byte) error {
return easyjson.Unmarshal(buf, tt)
}
`
)

View File

@ -1,31 +1,30 @@
package client package client
// Code generated by gen.go. DO NOT EDIT.
import ( import (
"errors" // "errors"
easyjson "github.com/mailru/easyjson"
"github.com/mailru/easyjson" jlexer "github.com/mailru/easyjson/jlexer"
"github.com/mailru/easyjson/jlexer" jwriter "github.com/mailru/easyjson/jwriter"
"github.com/mailru/easyjson/jwriter"
) )
// Target is the common interface for a Chrome Debugging Protocol target.
type Target interface {
String() string
GetID() string
GetType() TargetType
GetWebsocketURL() string
}
// TargetType are the types of targets available in Chrome. // TargetType are the types of targets available in Chrome.
type TargetType string type TargetType string
// TargetType values. // TargetType values.
const ( const (
App TargetType = "app"
BackgroundPage TargetType = "background_page" BackgroundPage TargetType = "background_page"
Browser TargetType = "browser"
External TargetType = "external"
Iframe TargetType = "iframe"
Other TargetType = "other" Other TargetType = "other"
Page TargetType = "page" Page TargetType = "page"
ServiceWorker TargetType = "service_worker" ServiceWorker TargetType = "service_worker"
Node TargetType = "node" SharedWorker TargetType = "shared_worker"
Webview TargetType = "webview"
Worker TargetType = "worker"
) )
// String satisfies stringer. // String satisfies stringer.
@ -45,20 +44,34 @@ func (tt TargetType) MarshalJSON() ([]byte, error) {
// UnmarshalEasyJSON satisfies easyjson.Unmarshaler. // UnmarshalEasyJSON satisfies easyjson.Unmarshaler.
func (tt *TargetType) UnmarshalEasyJSON(in *jlexer.Lexer) { func (tt *TargetType) UnmarshalEasyJSON(in *jlexer.Lexer) {
switch TargetType(in.String()) { z := TargetType(in.String())
switch z {
case App:
*tt = App
case BackgroundPage: case BackgroundPage:
*tt = BackgroundPage *tt = BackgroundPage
case Browser:
*tt = Browser
case External:
*tt = External
case Iframe:
*tt = Iframe
case Other: case Other:
*tt = Other *tt = Other
case Page: case Page:
*tt = Page *tt = Page
case ServiceWorker: case ServiceWorker:
*tt = ServiceWorker *tt = ServiceWorker
case Node: case SharedWorker:
*tt = Node *tt = SharedWorker
case Webview:
*tt = Webview
case Worker:
*tt = Worker
default: default:
in.AddError(errors.New("unknown TargetType")) // in.AddError(errors.New("unknown TargetType"))
*tt = z
} }
} }