-
Notifications
You must be signed in to change notification settings - Fork 19
types: Add MarshalBinary and UnmarshalBinary methods to EntityUID #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6772b71
199384b
4ddef61
60b4b94
6f35f02
6b16a01
d0c6fa8
69d8817
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,13 @@ package types | |
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "hash/fnv" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/cedar-policy/cedar-go/internal/mapset" | ||
| "github.com/cedar-policy/cedar-go/internal/rust" | ||
| ) | ||
|
|
||
| // Path is a series of idents separated by :: | ||
|
|
@@ -46,6 +49,37 @@ func (e EntityUID) MarshalCedar() []byte { | |
| return []byte(e.String()) | ||
| } | ||
|
|
||
| var errInvalidUID = errors.New("invalid EntityUID") | ||
|
|
||
| // UnmarshalCedar parses a Cedar language representation of an EntityUID. | ||
| func (e *EntityUID) UnmarshalCedar(data []byte) error { | ||
| // NB: In a perfect world we'd use the full parsing from internal/parser, but | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for writing this up. It's good to document our warts. |
||
| // today that imports cedar-go/types (this pkg) which means we'd need to carve | ||
| // it out to reuse it. Given that NewEntityUID(.,.) does zero validation | ||
| // itself, the juice is not worth the squeeze today. | ||
| s := string(data) | ||
| idx := strings.Index(s, "::\"") | ||
| if idx <= 0 { | ||
| // If idx == 0, the entity has no type, which is invalid. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tiny nit, Alternatively, just remove the comment :)
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I actually was only referring to the |
||
| return errInvalidUID | ||
| } | ||
|
|
||
| typ := EntityType(s[:idx]) | ||
| quoted := s[idx+2:] // include the leading `"` | ||
|
|
||
| if len(quoted) < 2 || quoted[0] != '"' || quoted[len(quoted)-1] != '"' { | ||
| return errInvalidUID | ||
| } | ||
|
|
||
| id, _, err := rust.Unquote([]byte(quoted[1:len(quoted)-1]), false) | ||
| if err != nil { | ||
| return errInvalidUID | ||
| } | ||
|
|
||
| *e = NewEntityUID(typ, String(id)) | ||
| return nil | ||
| } | ||
|
|
||
| func (e *EntityUID) UnmarshalJSON(b []byte) error { | ||
| // TODO: review after adding support for schemas | ||
| var res entityValueJSON | ||
|
|
@@ -74,6 +108,14 @@ func (e EntityUID) MarshalJSON() ([]byte, error) { | |
| }) | ||
| } | ||
|
|
||
| func (e EntityUID) MarshalBinary() ([]byte, error) { | ||
| return e.MarshalCedar(), nil | ||
| } | ||
|
|
||
| func (e *EntityUID) UnmarshalBinary(data []byte) error { | ||
| return e.UnmarshalCedar(data) | ||
| } | ||
|
|
||
| func (e EntityUID) hash() uint64 { | ||
| h := fnv.New64() | ||
| _, _ = h.Write([]byte(e.Type)) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.