Skip to content

Commit

Permalink
use pointer for optional number that default value is not 0
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Apr 8, 2022
1 parent 7c35efb commit 19a615b
Show file tree
Hide file tree
Showing 42 changed files with 188 additions and 274 deletions.
4 changes: 2 additions & 2 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (el *Element) SetFiles(paths []string) error {
// is fired all NodeID on the page will be reassigned to another value)
// we don't recommend using the NodeID, instead, use the BackendNodeID to identify the element.
func (el *Element) Describe(depth int, pierce bool) (*proto.DOMNode, error) {
val, err := proto.DOMDescribeNode{ObjectID: el.id(), Depth: depth, Pierce: pierce}.Call(el)
val, err := proto.DOMDescribeNode{ObjectID: el.id(), Depth: gson.Int(depth), Pierce: pierce}.Call(el)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -651,7 +651,7 @@ func (el *Element) Screenshot(format proto.PageCaptureScreenshotFormat, quality
}

opts := &proto.PageCaptureScreenshot{
Quality: quality,
Quality: gson.Int(quality),
Format: format,
}

Expand Down
7 changes: 4 additions & 3 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/gson"
)

// This example opens https://github.com/, searches for "git",
Expand Down Expand Up @@ -213,7 +214,7 @@ func Example_page_screenshot() {
// customization version
img, _ := page.Screenshot(true, &proto.PageCaptureScreenshot{
Format: proto.PageCaptureScreenshotFormatJpeg,
Quality: 90,
Quality: gson.Int(90),
Clip: &proto.PageViewport{
X: 0,
Y: 0,
Expand All @@ -234,8 +235,8 @@ func Example_page_pdf() {

// customized version
pdf, _ := page.PDF(&proto.PagePrintToPDF{
PaperWidth: 8.5,
PaperHeight: 11,
PaperWidth: gson.Num(8.5),
PaperHeight: gson.Num(11),
PageRanges: "1-3",
})
_ = utils.OutputFile("my.pdf", pdf)
Expand Down
9 changes: 5 additions & 4 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
"github.com/ysmood/gson"
)

// Keyboard represents the keyboard on a page, it's always related the main frame
Expand Down Expand Up @@ -134,7 +135,7 @@ func (m *Mouse) Move(x, y float64, steps int) error {
X: toX,
Y: toY,
Button: button,
Buttons: buttons,
Buttons: gson.Int(buttons),
Modifiers: m.page.Keyboard.getModifiers(),
}.Call(m.page)
if err != nil {
Expand Down Expand Up @@ -179,7 +180,7 @@ func (m *Mouse) Scroll(offsetX, offsetY float64, steps int) error {
X: m.x,
Y: m.y,
Button: button,
Buttons: buttons,
Buttons: gson.Int(buttons),
Modifiers: m.page.Keyboard.getModifiers(),
DeltaX: stepX,
DeltaY: stepY,
Expand All @@ -204,7 +205,7 @@ func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error {
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMousePressed,
Button: button,
Buttons: buttons,
Buttons: gson.Int(buttons),
ClickCount: clicks,
Modifiers: m.page.Keyboard.getModifiers(),
X: m.x,
Expand Down Expand Up @@ -235,7 +236,7 @@ func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error {
err := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseReleased,
Button: button,
Buttons: buttons,
Buttons: gson.Int(buttons),
ClickCount: clicks,
Modifiers: m.page.Keyboard.getModifiers(),
X: m.x,
Expand Down
3 changes: 2 additions & 1 deletion lib/devices/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package devices

import (
"github.com/go-rod/rod/lib/proto"
"github.com/ysmood/gson"
)

// Device represents a emulated device.
Expand Down Expand Up @@ -77,7 +78,7 @@ func (device Device) TouchEmulation() *proto.EmulationSetTouchEmulationEnabled {

return &proto.EmulationSetTouchEmulationEnabled{
Enabled: has(device.Capabilities, "touch"),
MaxTouchPoints: 5,
MaxTouchPoints: gson.Int(5),
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/examples/compare-chromedp/screenshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/proto"
"github.com/ysmood/gson"
)

// This example demonstrates how to take a screenshot of a specific element and
Expand All @@ -19,7 +20,7 @@ func main() {
// capture entire browser viewport, returning jpg with quality=90
buf, err := browser.MustPage("https://brank.as/").Screenshot(true, &proto.PageCaptureScreenshot{
Format: proto.PageCaptureScreenshotFormatJpeg,
Quality: 90,
Quality: gson.Int(90),
})
if err != nil {
panic(err)
Expand Down
20 changes: 0 additions & 20 deletions lib/proto/a_interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,26 +73,6 @@ func (t T) TimeCodec() {
t.Eq(raw, data)
}

func (t T) NormalizeInputDispatchMouseEvent() {
e := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseWheel,
}

data, err := json.Marshal(e)
t.E(err)

t.Eq(`{"type":"mouseWheel","x":0,"y":0,"deltaX":0,"deltaY":0}`, string(data))

ee := proto.InputDispatchMouseEvent{
Type: proto.InputDispatchMouseEventTypeMouseMoved,
}

data, err = json.Marshal(ee)
t.E(err)

t.Eq(`{"type":"mouseMoved","x":0,"y":0}`, string(data))
}

func (t T) Rect() {
rect := proto.DOMQuad{
336, 382, 361, 382, 361, 421, 336, 412,
Expand Down
87 changes: 0 additions & 87 deletions lib/proto/a_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package proto

import (
"encoding/json"
"time"
)

Expand Down Expand Up @@ -38,92 +37,6 @@ func (t MonotonicTime) String() string {
return t.Duration().String()
}

type inputDispatchMouseEvent struct {
Type InputDispatchMouseEventType `json:"type"`
X float64 `json:"x"`
Y float64 `json:"y"`
Modifiers int `json:"modifiers,omitempty"`
Timestamp TimeSinceEpoch `json:"timestamp,omitempty"`
Button InputMouseButton `json:"button,omitempty"`
Buttons int `json:"buttons,omitempty"`
ClickCount int `json:"clickCount,omitempty"`
Force float64 `json:"force,omitempty"`
TangentialPressure float64 `json:"tangentialPressure,omitempty"`
TiltX int `json:"tiltX,omitempty"`
TiltY int `json:"tiltY,omitempty"`
Twist int `json:"twist,omitempty"`
DeltaX float64 `json:"deltaX,omitempty"`
DeltaY float64 `json:"deltaY,omitempty"`
PointerType InputDispatchMouseEventPointerType `json:"pointerType,omitempty"`
}

type inputDispatchMouseWheelEvent struct {
Type InputDispatchMouseEventType `json:"type"`
X float64 `json:"x"`
Y float64 `json:"y"`
Modifiers int `json:"modifiers,omitempty"`
Timestamp TimeSinceEpoch `json:"timestamp,omitempty"`
Button InputMouseButton `json:"button,omitempty"`
Buttons int `json:"buttons,omitempty"`
ClickCount int `json:"clickCount,omitempty"`
Force float64 `json:"force,omitempty"`
TangentialPressure float64 `json:"tangentialPressure,omitempty"`
TiltX int `json:"tiltX,omitempty"`
TiltY int `json:"tiltY,omitempty"`
Twist int `json:"twist,omitempty"`
DeltaX float64 `json:"deltaX"`
DeltaY float64 `json:"deltaY"`
PointerType InputDispatchMouseEventPointerType `json:"pointerType,omitempty"`
}

// MarshalJSON interface
// TODO: make sure deltaX and deltaY are never omitted. Or it will cause a browser bug.
func (e InputDispatchMouseEvent) MarshalJSON() ([]byte, error) {
var ee interface{}

if e.Type == InputDispatchMouseEventTypeMouseWheel {
ee = &inputDispatchMouseWheelEvent{
Type: e.Type,
X: e.X,
Y: e.Y,
Modifiers: e.Modifiers,
Timestamp: e.Timestamp,
Button: e.Button,
Buttons: e.Buttons,
ClickCount: e.ClickCount,
Force: e.Force,
TangentialPressure: e.TangentialPressure,
TiltX: e.TiltX,
TiltY: e.TiltY,
Twist: e.Twist,
DeltaX: e.DeltaX,
DeltaY: e.DeltaY,
PointerType: e.PointerType,
}
} else {
ee = &inputDispatchMouseEvent{
Type: e.Type,
X: e.X,
Y: e.Y,
Modifiers: e.Modifiers,
Timestamp: e.Timestamp,
Button: e.Button,
Buttons: e.Buttons,
ClickCount: e.ClickCount,
Force: e.Force,
TangentialPressure: e.TangentialPressure,
TiltX: e.TiltX,
TiltY: e.TiltY,
Twist: e.Twist,
DeltaX: e.DeltaX,
DeltaY: e.DeltaY,
PointerType: e.PointerType,
}
}

return json.Marshal(ee)
}

// Point from the origin (0, 0)
type Point struct {
X float64 `json:"x"`
Expand Down
2 changes: 1 addition & 1 deletion lib/proto/accessibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ type AccessibilityGetFullAXTree struct {

// Depth (optional) The maximum depth at which descendants of the root node should be retrieved.
// If omitted, the full tree is returned.
Depth int `json:"depth,omitempty"`
Depth *int `json:"depth,omitempty"`

// FrameID (optional) The frame for whose document the AX tree should be retrieved.
// If omitted, the root frame is used.
Expand Down
4 changes: 2 additions & 2 deletions lib/proto/audits.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ type AuditsTrustedWebActivityIssueDetails struct {
ViolationType AuditsTwaQualityEnforcementViolationType `json:"violationType"`

// HTTPStatusCode (optional) ...
HTTPStatusCode int `json:"httpStatusCode,omitempty"`
HTTPStatusCode *int `json:"httpStatusCode,omitempty"`

// PackageName (optional) The package name of the Trusted Web Activity client app. This field is
// only used when violation type is kDigitalAssetLinks.
Expand Down Expand Up @@ -904,7 +904,7 @@ type AuditsGetEncodedResponse struct {
Encoding AuditsGetEncodedResponseEncoding `json:"encoding"`

// Quality (optional) The quality of the encoding (0-1). (defaults to 1)
Quality float64 `json:"quality,omitempty"`
Quality *float64 `json:"quality,omitempty"`

// SizeOnly (optional) Whether to only return the size information (defaults to false).
SizeOnly bool `json:"sizeOnly,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions lib/proto/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ const (
type BrowserBounds struct {

// Left (optional) The offset from the left edge of the screen to the window in pixels.
Left int `json:"left,omitempty"`
Left *int `json:"left,omitempty"`

// Top (optional) The offset from the top edge of the screen to the window in pixels.
Top int `json:"top,omitempty"`
Top *int `json:"top,omitempty"`

// Width (optional) The window width in pixels.
Width int `json:"width,omitempty"`
Width *int `json:"width,omitempty"`

// Height (optional) The window height in pixels.
Height int `json:"height,omitempty"`
Height *int `json:"height,omitempty"`

// WindowState (optional) The window state. Default to normal.
WindowState BrowserWindowState `json:"windowState,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions lib/proto/cache_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ type CacheStorageRequestEntries struct {
CacheID CacheStorageCacheID `json:"cacheId"`

// SkipCount (optional) Number of records to skip.
SkipCount int `json:"skipCount,omitempty"`
SkipCount *int `json:"skipCount,omitempty"`

// PageSize (optional) Number of records to fetch.
PageSize int `json:"pageSize,omitempty"`
PageSize *int `json:"pageSize,omitempty"`

// PathFilter (optional) If present, only return the entries containing this substring in the path
PathFilter string `json:"pathFilter,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions lib/proto/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ type ConsoleConsoleMessage struct {
URL string `json:"url,omitempty"`

// Line (optional) Line number in the resource that generated this message (1-based).
Line int `json:"line,omitempty"`
Line *int `json:"line,omitempty"`

// Column (optional) Column number in the resource that generated this message (1-based).
Column int `json:"column,omitempty"`
Column *int `json:"column,omitempty"`
}

// ConsoleClearMessages Does nothing.
Expand Down
2 changes: 1 addition & 1 deletion lib/proto/css.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ type CSSMediaQueryExpression struct {
ValueRange *CSSSourceRange `json:"valueRange,omitempty"`

// ComputedLength (optional) Computed length of media query expression (if applicable).
ComputedLength float64 `json:"computedLength,omitempty"`
ComputedLength *float64 `json:"computedLength,omitempty"`
}

// CSSCSSContainerQuery (experimental) CSS container query rule descriptor.
Expand Down
Loading

0 comments on commit 19a615b

Please sign in to comment.