From 36d2bd12b1d06700a604a1f6227c36a4c0a6de03 Mon Sep 17 00:00:00 2001 From: Igor Ignatyev Date: Mon, 11 Aug 2025 13:53:48 +0300 Subject: [PATCH] key-value item, allow to store any --- keyring.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/keyring.go b/keyring.go index 26078e6..8ae45db 100644 --- a/keyring.go +++ b/keyring.go @@ -2,6 +2,7 @@ package keyring import ( "errors" + "reflect" "github.com/launchrctl/launchr" ) @@ -36,11 +37,32 @@ func (i CredentialsItem) isEmpty() bool { // KeyValueItem stores key-value pair. type KeyValueItem struct { Key string `yaml:"key"` - Value string `yaml:"value"` + Value any `yaml:"value"` } func (i KeyValueItem) isEmpty() bool { - return i.Key == "" || i.Value == "" + if i.Key == "" { + return true + } + + if i.Value == nil { + return true + } + + // Use reflection to check if the value is its zero value + v := reflect.ValueOf(i.Value) + + switch v.Kind() { + case reflect.String: // also handles type alias for string. + return v.String() == "" + case reflect.Slice, reflect.Map, reflect.Array: + return v.Len() == 0 + case reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func: + return v.IsNil() + default: + // For other types, check if it's the zero value + return v.IsZero() + } } // DataStore provides password storage functionality. @@ -191,7 +213,9 @@ func (k *keyringService) maskItem(item SecretItem) { case CredentialsItem: k.mask.AddString(dataItem.Password) case KeyValueItem: - k.mask.AddString(dataItem.Value) + if v, ok := dataItem.Value.(string); ok { + k.mask.AddString(v) + } default: } }