Files
bjoern-m 601ffaae92 Introduce Flowpilot - integration (#1532)
This pull request introduces the new Flowpilot system along with several new features and various improvements. The key enhancements include configurable authorization, registration, and profile flows, as well as the ability to enable and disable user identifiers (e.g., email addresses and usernames) and login methods.

---------

Co-authored-by: Frederic Jahn <frederic.jahn@hanko.io>
Co-authored-by: Lennart Fleischmann <lennart.fleischmann@hanko.io>
Co-authored-by: lfleischmann <67686424+lfleischmann@users.noreply.github.com>
Co-authored-by: merlindru <hello@merlindru.com>
2024-08-06 16:07:29 +02:00

85 lines
2.6 KiB
Go

package jsonmanager
import (
"errors"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// ReadJSONManager is the interface that allows read operations.
type ReadJSONManager interface {
Get(path string) gjson.Result // Get retrieves the value at the specified path in the JSON data.
String() string // String returns the JSON data as a string.
Unmarshal() interface{} // Unmarshal parses the JSON data and returns it as an interface{}.
}
// JSONManager is the interface that defines methods for reading, writing, and deleting JSON data.
type JSONManager interface {
ReadJSONManager
Set(path string, value interface{}) error // Set updates the JSON data at the specified path with the provided value.
Delete(path string) error // Delete removes a value from the JSON data at the specified path.
}
// ReadOnlyJSONManager is the interface that allows only read operations.
type ReadOnlyJSONManager interface {
ReadJSONManager
}
// DefaultJSONManager is the default implementation of the JSONManager interface.
type DefaultJSONManager struct {
data string // The JSON data stored as a string.
}
// NewJSONManager creates a new instance of DefaultJSONManager with empty JSON data.
func NewJSONManager() JSONManager {
return &DefaultJSONManager{data: "{}"}
}
// NewJSONManagerFromString creates a new instance of DefaultJSONManager with the given JSON data.
// It checks if the provided data is valid JSON before creating the instance.
func NewJSONManagerFromString(data string) (JSONManager, error) {
if !gjson.Valid(data) {
return nil, errors.New("invalid json")
}
return &DefaultJSONManager{data: data}, nil
}
// Get retrieves the value at the specified path in the JSON data.
func (jm *DefaultJSONManager) Get(path string) gjson.Result {
return gjson.Get(jm.data, path)
}
// Set updates the JSON data at the specified path with the provided value.
func (jm *DefaultJSONManager) Set(path string, value interface{}) error {
newData, err := sjson.Set(jm.data, path, value)
if err != nil {
return err
}
jm.data = newData
return nil
}
// Delete removes a value from the JSON data at the specified path.
func (jm *DefaultJSONManager) Delete(path string) error {
newData, err := sjson.Delete(jm.data, string(path))
if err != nil {
return err
}
jm.data = newData
return nil
}
// String returns the JSON data as a string.
func (jm *DefaultJSONManager) String() string {
return jm.data
}
// Unmarshal parses the JSON data and returns it as an interface{}.
func (jm *DefaultJSONManager) Unmarshal() interface{} {
m, ok := gjson.Parse(jm.data).Value().(interface{})
if !ok {
return nil
}
return m
}