Skip to content

Commit

Permalink
feat!: add support for JSON pointers in getSchema
Browse files Browse the repository at this point in the history
This alows a fine grained access to the getSchema call. But breaks the
API on the other hand since Go does not support multiple signatures of
the same receiving function aka method.

Signed-off-by: Christian Ege <[email protected]>
  • Loading branch information
graugans committed Dec 6, 2023
1 parent 71817e1 commit c9eb4f4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
55 changes: 53 additions & 2 deletions cmd/ovp8xx/cmd/getSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (

func getSchemaCommand(cmd *cobra.Command, args []string) error {

pointers, err := cmd.Flags().GetStringSlice("pointer")
if err != nil {
return err
}

host, err := rootCmd.PersistentFlags().GetString("ip")
if err != nil {
return err
Expand All @@ -21,7 +26,7 @@ func getSchemaCommand(cmd *cobra.Command, args []string) error {
ovp8xx.WithHost(host),
)

if result, err := o3r.GetSchema(); err != nil {
if result, err := o3r.GetSchema(pointers); err != nil {
return err
} else {
fmt.Printf("%s\n", result)
Expand All @@ -33,9 +38,55 @@ func getSchemaCommand(cmd *cobra.Command, args []string) error {
var getSchemaCmd = &cobra.Command{
Use: "getSchema",
Short: "Retrieve the currently used JSON schema from the device",
RunE: getSchemaCommand,
Long: `The OVP8xx getSchema command accepts a list of JSON pointers.
The JSON schema provides details about multiple aspects of a paramter. It
contains information like the type of a parameter and its defaults. It also
provides information weather a parameter is readOnly or not.
Due to the fact the schema can schema can grow quite big it is possible to
limit the scope of the query with a query string which is similar to a JSON pointer.
The pointer '/device/swVersion/diagnostics' for example provides this information
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"device": {
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"swVersion": {
"additionalProperties": false,
"description": "version of software components",
"properties": {
"diagnostics": {
"readOnly": true,
"type": "string"
}
},
"required": [
"kernel",
"l4t",
"firmware"
],
"type": "object"
}
},
"title": "O3R device configuration",
"type": "object"
}
},
"title": "O3R configuration",
"type": "object"
}
When no query is provided the complete schema is returend.
`,
RunE: getSchemaCommand,
}

func init() {
rootCmd.AddCommand(getSchemaCmd)
getSchemaCmd.Flags().StringSliceP("pointer", "p", []string{""}, "A JSON pointer to be queried. This can be used multiple times")
}
8 changes: 5 additions & 3 deletions pkg/ovp8xx/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (device *Client) FactoryReset(keepNetworkSettings bool) error {
return client.Call("factoryReset", arg, nil)
}

func (device *Client) GetSchema() (string, error) {
func (device *Client) GetSchema(pointers []string) (string, error) {
client, err := xmlrpc.NewClient(device.url)
if err != nil {
return "", err
Expand All @@ -105,8 +105,10 @@ func (device *Client) GetSchema() (string, error) {
result := &struct {
JSON string
}{}

if err := client.Call("getSchema", nil, result); err != nil {
arg := &struct {
Pointers []string
}{Pointers: pointers}
if err := client.Call("getSchema", arg, result); err != nil {
return "", err
}
return result.JSON, nil
Expand Down

0 comments on commit c9eb4f4

Please sign in to comment.