From f01d29fc25af48abd7f1b8a9a8729be6f9c94b95 Mon Sep 17 00:00:00 2001 From: Jacalz Date: Fri, 9 Aug 2024 12:21:38 +0200 Subject: [PATCH] settings: Let ReadOne fall back to Read on error --- settings/all.go | 28 ----------------------- settings/one.go | 26 --------------------- settings/read.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 54 deletions(-) delete mode 100644 settings/all.go delete mode 100644 settings/one.go create mode 100644 settings/read.go diff --git a/settings/all.go b/settings/all.go deleted file mode 100644 index 6cd415a..0000000 --- a/settings/all.go +++ /dev/null @@ -1,28 +0,0 @@ -package settings - -import ( - "github.com/godbus/dbus/v5" - "github.com/rymdport/portal/internal/apis" -) - -const readAllCallPath = settingsCallPath + ".ReadAll" - -// ReadAll returns all values for the corresponding namespaces passed. -// If namespaces is an empty array or contains an empty string it matches all. -// Globbing is supported but only for trailing sections, e.g. “org.example.*”. -func ReadAll(namespaces []string) (map[string](map[string]dbus.Variant), error) { - conn, err := dbus.SessionBus() // Shared connection, don't close. - if err != nil { - return nil, err - } - - obj := conn.Object(apis.ObjectName, apis.ObjectPath) - call := obj.Call(readAllCallPath, 0, namespaces) - if call.Err != nil { - return nil, call.Err - } - - var value map[string](map[string]dbus.Variant) - err = call.Store(&value) - return value, err -} diff --git a/settings/one.go b/settings/one.go deleted file mode 100644 index 022f3af..0000000 --- a/settings/one.go +++ /dev/null @@ -1,26 +0,0 @@ -package settings - -import ( - "github.com/godbus/dbus/v5" - "github.com/rymdport/portal/internal/apis" -) - -const readOneCallPath = settingsCallPath + ".ReadOne" - -// ReadOne reads a single value which may be any valid DBus type. Returns an error on any unknown namespace or key. -func ReadOne(namespace, key string) (any, error) { - conn, err := dbus.SessionBus() // Shared connection, don't close. - if err != nil { - return nil, err - } - - obj := conn.Object(apis.ObjectName, apis.ObjectPath) - call := obj.Call(readOneCallPath, 0, namespace, key) - if call.Err != nil { - return nil, call.Err - } - - var value any - err = call.Store(&value) - return value, err -} diff --git a/settings/read.go b/settings/read.go new file mode 100644 index 0000000..58d5dea --- /dev/null +++ b/settings/read.go @@ -0,0 +1,59 @@ +package settings + +import ( + "github.com/godbus/dbus/v5" + "github.com/rymdport/portal/internal/apis" +) + +const ( + readCallPath = settingsCallPath + ".Read" + readOneCallPath = settingsCallPath + ".ReadOne" + readAllCallPath = settingsCallPath + ".ReadAll" +) + +// ReadAll returns all values for the corresponding namespaces passed. +// If namespaces is an empty array or contains an empty string it matches all. +// Globbing is supported but only for trailing sections, e.g. “org.example.*”. +func ReadAll(namespaces []string) (map[string](map[string]dbus.Variant), error) { + conn, err := dbus.SessionBus() // Shared connection, don't close. + if err != nil { + return nil, err + } + + obj := conn.Object(apis.ObjectName, apis.ObjectPath) + call := obj.Call(readAllCallPath, 0, namespaces) + if call.Err != nil { + return nil, call.Err + } + + var value map[string](map[string]dbus.Variant) + err = call.Store(&value) + return value, err +} + +// ReadOne reads a single value which may be any valid DBus type. Returns an error on any unknown namespace or key. +func ReadOne(namespace, key string) (any, error) { + value, err := read(readOneCallPath, namespace, key) + if err != nil { + return read(readCallPath, namespace, key) // Use deprecated fallback if new interface does not exist. + } + + return value, err +} + +func read(callPath, namespace, key string) (any, error) { + conn, err := dbus.SessionBus() // Shared connection, don't close. + if err != nil { + return nil, err + } + + obj := conn.Object(apis.ObjectName, apis.ObjectPath) + call := obj.Call(callPath, 0, namespace, key) + if call.Err != nil { + return nil, call.Err + } + + var value any + err = call.Store(&value) + return value, err +}