From 55a967318945690d07b92c52a40c08174d500271 Mon Sep 17 00:00:00 2001 From: Fedot-Compot Date: Thu, 28 Nov 2024 16:08:36 +0000 Subject: [PATCH] feat: quality of life string conversions for FgaObject --- model_fga_object.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/model_fga_object.go b/model_fga_object.go index 2b5e24d..acd5fba 100644 --- a/model_fga_object.go +++ b/model_fga_object.go @@ -14,6 +14,8 @@ package openfga import ( "bytes" + "fmt" + "strings" "encoding/json" ) @@ -105,6 +107,23 @@ func (o FgaObject) MarshalJSON() ([]byte, error) { return b.Bytes(), nil } +func (o FgaObject) String() string { + return fmt.Sprintf("%s:%s", o.Type, o.Id) +} + +func FgaObjectFromString(objectString string) (*FgaObject, error) { + if objectString == "" { + return nil, fmt.Errorf("failed parsing FgaObject, cannot build FgaObject from empty string") + } + objectTokens := strings.Split(objectString, ":") + if len(objectTokens) != 2 { + return nil, fmt.Errorf("failed parsing FgaObject, invalid FgaObject string") + } + objectType := objectTokens[0] + objectId := objectTokens[1] + return NewFgaObject(objectType, objectId), nil +} + type NullableFgaObject struct { value *FgaObject isSet bool