From 7f7579a65cec2ae2f2a77c6284735484200c71c6 Mon Sep 17 00:00:00 2001 From: Andreas Linde Date: Mon, 28 Oct 2024 17:42:54 +0100 Subject: [PATCH 1/2] JSON RPC2 implementation This is an initial commit to start development of this feature in its own feature branch --- README.md | 9 +++++---- com/README.md | 3 +++ com/jsonrpc2/README.md | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 com/README.md create mode 100644 com/jsonrpc2/README.md diff --git a/README.md b/README.md index 871a5eba..1236c503 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ The supported functionality contains: ## Packages - `api`: global API interface definitions and eebus service configuration +- `com/jsonrpc2`: provides an JSON RPC2 implementation, to communicate with systems not written in Golang - `features/client`: provides feature helpers with the local SPINE feature having the client role and the remote SPINE feature being the server for easy access to commonly used functions - `features/server`: provides feature helpers with the local SPINE feature having the server role for easy access to commonly used functions - `service`: central package which provides access to SHIP and SPINE. Use this to create the EEBUS service, its configuration and connect to remote EEBUS services @@ -42,7 +43,7 @@ Services with implemented use cases will be implemented in different repositorie #### First Run ```sh -go run cmd/hems/main.go 4715 +go run examples/hems/main.go 4715 ``` `4715` is the example server port that this process should use @@ -52,7 +53,7 @@ The certificate and key and the local SKI will be generated and printed. You sho #### General Usage ```sh -Usage: go run cmd/hems/main.go +Usage: go run examples/hems/main.go ``` - `remoteski` is the SKI of the remote device or service you want to connect to @@ -64,7 +65,7 @@ Usage: go run cmd/hems/main.go #### First Run ```sh -go run cmd/hems/main.go 4715 +go run examples/hems/main.go 4715 ``` `4715` is the example server port that this process should use @@ -74,7 +75,7 @@ The certificate and key and the local SKI will be generated and printed. You sho #### General Usage ```sh -Usage: go run cmd/evse/main.go +Usage: go run examples/evse/main.go ``` - `remoteski` is the SKI of the remote device or service you want to connect to diff --git a/com/README.md b/com/README.md new file mode 100644 index 00000000..a998aa31 --- /dev/null +++ b/com/README.md @@ -0,0 +1,3 @@ +# Overview + +This folder contains multiple communication APIs for services that are not written in Golang and need to communicate with a Go service running this stack and use the public API for interaction. diff --git a/com/jsonrpc2/README.md b/com/jsonrpc2/README.md new file mode 100644 index 00000000..03bd8b7f --- /dev/null +++ b/com/jsonrpc2/README.md @@ -0,0 +1 @@ +This is currently a placeholder file as this folder will host the jsonrpc2 API interface and implementation From 052c0559229b4261e848cb55b64912565cd1388a Mon Sep 17 00:00:00 2001 From: Simon Thelen Date: Tue, 29 Oct 2024 15:37:00 +0100 Subject: [PATCH 2/2] Forward error types as their string representation --- examples/remote/util.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/examples/remote/util.go b/examples/remote/util.go index 2a3bb891..5df8a48b 100644 --- a/examples/remote/util.go +++ b/examples/remote/util.go @@ -25,11 +25,23 @@ func isExportedOrBuiltinType(t reflect.Type) bool { return token.IsExported(t.Name()) || t.PkgPath() == "" } +// json.Marshall won't marshall error types, marshall as string +func errorAsJson(v reflect.Value) interface{} { + if v.IsNil() { + // passthrough nil as nil, otherwise nil.(error) will panic + return nil + } else { + return v.Interface().(error).Error() + } +} + func transformReturnValues(values []reflect.Value) []interface{} { result := make([]interface{}, len(values)) for i, e := range values { - switch e.Type() { + valueType := e.Type() + + switch valueType { case reflect.TypeFor[spineapi.DeviceRemoteInterface](): result[i] = e.Interface().(spineapi.DeviceRemoteInterface).Address() case reflect.TypeFor[[]spineapi.DeviceRemoteInterface](): @@ -51,12 +63,18 @@ func transformReturnValues(values []reflect.Value) []interface{} { } result[i] = transformedValues default: - result[i] = e.Interface() + switch { + case valueType.Implements(reflect.TypeFor[error]()): + result[i] = errorAsJson(e) + default: + result[i] = e.Interface() + } } } return result } + func WriteKey(cert tls.Certificate, path string) error { file, err := os.Create(path) if err != nil {