From c9eb4f4ff01a467887a32eb6c4d313dfaf5ae099 Mon Sep 17 00:00:00 2001 From: Christian Ege Date: Wed, 6 Dec 2023 08:50:07 +0100 Subject: [PATCH] feat!: add support for JSON pointers in getSchema 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 --- cmd/ovp8xx/cmd/getSchema.go | 55 +++++++++++++++++++++++++++++++++++-- pkg/ovp8xx/rpc.go | 8 ++++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/cmd/ovp8xx/cmd/getSchema.go b/cmd/ovp8xx/cmd/getSchema.go index 0553410..1a09ee9 100644 --- a/cmd/ovp8xx/cmd/getSchema.go +++ b/cmd/ovp8xx/cmd/getSchema.go @@ -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 @@ -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) @@ -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") } diff --git a/pkg/ovp8xx/rpc.go b/pkg/ovp8xx/rpc.go index db12bcf..87fb969 100644 --- a/pkg/ovp8xx/rpc.go +++ b/pkg/ovp8xx/rpc.go @@ -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 @@ -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