65 lines
1.3 KiB
Go
Raw Normal View History

package vault
import "encoding/json"
type SecretDataTypes interface {
GetMountPath() string
}
type SecretData[T SecretDataTypes] struct {
UserID string `json:"user_id"`
UserSourceID string `json:"user_source_id"`
Secret T `json:"secret"`
}
type E621 struct {
APIKey string `json:"api_key"`
}
func (e E621) GetMountPath() string {
return "e621.net"
}
type FurAffinity struct {
CookieA string `json:"cookie_a"`
CookieB string `json:"cookie_b"`
}
func (e FurAffinity) GetMountPath() string {
return "furaffinity.net"
}
func SecretToMap[T SecretDataTypes](secretData SecretData[T]) (map[string]interface{}, error) {
// Convert the Secret field to JSON
secretJSON, err := json.Marshal(secretData.Secret)
if err != nil {
return nil, err
}
// Unmarshal the JSON into a map
var secretMap map[string]interface{}
err = json.Unmarshal(secretJSON, &secretMap)
if err != nil {
return nil, err
}
return secretMap, nil
}
func MapToSecret[T SecretDataTypes](secretMap map[string]interface{}) (T, error) {
// Convert the map to JSON
secretJSON, err := json.Marshal(secretMap)
if err != nil {
return *new(T), err
}
// Unmarshal the JSON into an object of type T
var secretData T
err = json.Unmarshal(secretJSON, &secretData)
if err != nil {
return *new(T), err
}
return secretData, nil
}