main
1package gid
2
3import (
4 "net/url"
5 "strings"
6
7 "github.com/cedar-policy/cedar-go"
8)
9
10func NewEntityUID(globalID string) cedar.EntityUID {
11 if !strings.HasPrefix(globalID, "gid://") {
12 return DefaultEntityUID(globalID)
13 }
14
15 url, err := url.Parse(globalID)
16 if err != nil {
17 return DefaultEntityUID(globalID)
18 }
19 items := strings.SplitN(url.Path, "/", 3)
20 if len(items) != 3 {
21 return DefaultEntityUID(globalID)
22 }
23
24 return cedar.NewEntityUID(
25 cedar.EntityType(items[1]),
26 cedar.String(items[2]),
27 )
28}
29
30func DefaultEntityUID(id string) cedar.EntityUID {
31 return cedar.NewEntityUID("User", cedar.String(id))
32}
33
34func ZeroEntityUID() cedar.EntityUID {
35 return cedar.NewEntityUID("", "")
36}