feat: ABAC support for /api/enforce endpoint (#2660)

This commit is contained in:
Konstantin
2024-01-31 18:14:55 +03:00
committed by GitHub
parent 523186f895
commit c4096788b2
2 changed files with 41 additions and 4 deletions

View File

@ -14,7 +14,10 @@
package util
import "encoding/json"
import (
"encoding/json"
"reflect"
)
func StructToJson(v interface{}) string {
data, err := json.Marshal(v)
@ -37,3 +40,30 @@ func StructToJsonFormatted(v interface{}) string {
func JsonToStruct(data string, v interface{}) error {
return json.Unmarshal([]byte(data), v)
}
func TryJsonToAnonymousStruct(j string) (interface{}, error) {
var data map[string]interface{}
if err := json.Unmarshal([]byte(j), &data); err != nil {
return nil, err
}
// Create a slice of StructFields
fields := make([]reflect.StructField, 0, len(data))
for k, v := range data {
fields = append(fields, reflect.StructField{
Name: k,
Type: reflect.TypeOf(v),
})
}
// Create the struct type
t := reflect.StructOf(fields)
// Unmarshal again, this time to the new struct type
val := reflect.New(t)
i := val.Interface()
if err := json.Unmarshal([]byte(j), &i); err != nil {
return nil, err
}
return i, nil
}