spec

package
v0.1.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package spec loads commands.json, the single definition of every Droidline command, and normalises raw requests against it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckQuery added in v0.1.1

func CheckQuery(v any) (map[string]any, error)

CheckQuery validates a query object and returns a copy with bounds as int64.

func DescribeQuery added in v0.1.1

func DescribeQuery(q map[string]any) string

DescribeQuery is the target text used in NOT_FOUND and friends.

func MatchField added in v0.1.1

func MatchField(by, value string, n *Node) bool

MatchField applies one simple selector (by, value) to a node.

func QueryKeys added in v0.1.1

func QueryKeys() []string

QueryKeys lists every key a query accepts, for error messages and docs.

func Raw

func Raw() []byte

Raw returns the embedded commands.json bytes.

Types

type Alias

type Alias struct {
	Name string `json:"name"`
	By   string `json:"by"`
}

type ArgError

type ArgError struct {
	Cmd     string
	Reason  string
	Unknown bool
	Similar []string
}

ArgError is reported to clients as BAD_ARGS (or UNKNOWN_CMD when Unknown is set).

func (*ArgError) Error

func (e *ArgError) Error() string

type Command

type Command struct {
	Name        string   `json:"name"`
	Group       string   `json:"group"`
	Phase       int      `json:"phase"`
	Scope       string   `json:"scope"`
	Requires    []string `json:"requires,omitempty"`
	Condition   bool     `json:"condition,omitempty"`
	CutsNetwork *Cut     `json:"cuts_network,omitempty"`
	Summary     I18n     `json:"summary"`
	Notes       I18n     `json:"notes,omitempty"`
	Params      []Param  `json:"params"`
	Returns     Returns  `json:"returns"`
	Errors      []string `json:"errors,omitempty"`
	Aliases     []Alias  `json:"aliases,omitempty"`
	SDKSave     string   `json:"sdk_save,omitempty"`
	SDKWrap     string   `json:"sdk_wrap,omitempty"`
	SDKManual   bool     `json:"sdk_manual,omitempty"`
	// RunsOn "server" marks a phone command the PC carries out itself, using screenshots and taps.
	RunsOn string `json:"runs_on,omitempty"`
	MCP    *bool  `json:"mcp,omitempty"`
	// MCPOptIn keeps a tool out of the MCP server unless --tools names it.
	MCPOptIn bool     `json:"mcp_opt_in,omitempty"`
	MinSDK   int      `json:"min_sdk,omitempty"`
	Example  string   `json:"example,omitempty"`
	Examples []string `json:"examples,omitempty"`
}

func (*Command) InMCP

func (c *Command) InMCP() bool

InMCP reports whether the command is exposed as an MCP tool.

func (*Command) Param

func (c *Command) Param(name string) *Param

Param returns the parameter called name, or nil.

type Cut

type Cut struct {
	Param string `json:"param"`
	When  bool   `json:"when"`
}

type ErrorDef

type ErrorDef struct {
	Code      string `json:"code"`
	HTTP      int    `json:"http"`
	Retryable bool   `json:"retryable"`
	Msg       I18n   `json:"msg"`
	Doc       I18n   `json:"doc"`
}

type Field

type Field struct {
	Name string `json:"name"`
	Type string `json:"type"`
}

type I18n

type I18n map[string]string

func (I18n) In

func (t I18n) In(lang string) string

In returns the text for lang, falling back to English.

type Node added in v0.1.1

type Node struct {
	Fields   map[string]any
	Parent   *Node
	Children []*Node
}

Node is one element of a dump tree, linked to its parent.

func FindAll added in v0.1.1

func FindAll(roots []*Node, by any, value string) []*Node

FindAll returns the nodes a selector matches: by is a selector name with a value, or a query object. Directional queries come back nearest first.

func NewTree added in v0.1.1

func NewTree(m map[string]any) *Node

NewTree links a dump tree (the "tree" field of dump) into Nodes.

func RowOf added in v0.1.1

func RowOf(n *Node) *Node

RowOf is the row the row key compares against; the inspector uses it to suggest selectors.

func (*Node) Bool added in v0.1.1

func (n *Node) Bool(k string) bool

func (*Node) Bounds added in v0.1.1

func (n *Node) Bounds() [4]int

Bounds returns left, top, right, bottom.

func (*Node) Str added in v0.1.1

func (n *Node) Str(k string) string

func (*Node) Visible added in v0.1.1

func (n *Node) Visible() bool

Visible reports whether the element is on screen. Phones that predate the field send none; those count as visible.

func (*Node) Walk added in v0.1.1

func (n *Node) Walk(fn func(*Node))

Walk visits the node and its descendants in document order.

type Param

type Param struct {
	Ref        string   `json:"ref,omitempty"`
	Name       string   `json:"name"`
	Type       string   `json:"type"`
	Required   bool     `json:"required,omitempty"`
	Default    any      `json:"default,omitempty"`
	Enum       []string `json:"enum,omitempty"`
	Unit       string   `json:"unit,omitempty"`
	ClientOnly bool     `json:"client_only,omitempty"`
	Doc        I18n     `json:"doc,omitempty"`
}

type QueryDef added in v0.1.1

type QueryDef struct {
	Doc  I18n `json:"doc"`
	Keys []struct {
		Key  string `json:"key"`
		Type string `json:"type"`
		Doc  I18n   `json:"doc"`
	} `json:"keys"`
}

QueryDef documents the keys a query object accepts; spec/query.go implements them.

type Returns

type Returns struct {
	Kind   string  `json:"kind"`
	Type   string  `json:"type,omitempty"`
	Fields []Field `json:"fields,omitempty"`
}

type Selector

type Selector struct {
	By        string `json:"by"`
	Field     string `json:"field"`
	Match     string `json:"match"`
	Doc       I18n   `json:"doc"`
	Shorthand string `json:"shorthand,omitempty"`
}

type Spec

type Spec struct {
	Proto        int                `json:"proto"`
	Selectors    []Selector         `json:"selectors"`
	Query        QueryDef           `json:"query"`
	CommonParams map[string]Param   `json:"common_params"`
	Errors       []ErrorDef         `json:"errors"`
	Commands     []*Command         `json:"commands"`
	BatchOnly    []*Command         `json:"batch_only"`
	Types        map[string]TypeDef `json:"types"`
	// contains filtered or unexported fields
}

func Load

func Load() (*Spec, error)

Load parses the embedded commands.json and resolves parameter references.

func MustLoad

func MustLoad() *Spec

MustLoad is Load for package init paths where a broken embed is a build bug.

func Parse

func Parse(data []byte) (*Spec, error)

func (*Spec) Command

func (s *Spec) Command(name string) (c *Command, by string)

Command looks up a command by name or alias. by is non-empty for aliases.

func (*Spec) CutsNetwork

func (s *Spec) CutsNetwork(c *Command, params map[string]any) bool

CutsNetwork reports whether this normalised call will drop the phone's link.

func (*Spec) Error

func (s *Spec) Error(code string) *ErrorDef

func (*Spec) IsSelector

func (s *Spec) IsSelector(by string) bool

func (*Spec) Names

func (s *Spec) Names() []string

Names returns all command names (not aliases) in definition order.

func (*Spec) Normalize

func (s *Spec) Normalize(name string, raw map[string]any) (*Command, map[string]any, error)

Normalize checks raw request fields against the command definition and returns the parameters the agent (or server handler) receives: aliases resolved, positional args mapped, values coerced, defaults filled, client-only params dropped.

func (*Spec) ParseArg

func (s *Spec) ParseArg(p *Param, raw string) (any, error)

ParseArg converts one command-line string into the JSON value a param expects.

func (*Spec) RenderError

func (s *Spec) RenderError(code, lang string, fields map[string]any) string

RenderError fills the message template of code in lang. Fields the template needs but the caller did not supply render as "?".

func (*Spec) SelectorNames

func (s *Spec) SelectorNames() []string

func (*Spec) Similar

func (s *Spec) Similar(name string, n int) []string

Similar returns up to n command or alias names close to name, for "did you mean".

func (*Spec) SortedErrorCodes

func (s *Spec) SortedErrorCodes() []string

SortedErrorCodes is used by generators and docs that want a stable order.

type TypeDef

type TypeDef struct {
	Doc    I18n     `json:"doc,omitempty"`
	Fields []string `json:"fields"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL