Skip to content

Commit

Permalink
object: fix acl and project team json marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
visualage committed Feb 13, 2021
1 parent 44ec0f7 commit 42689d8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
101 changes: 94 additions & 7 deletions fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ import (

var errInvalidGeneration = errors.New("invalid generation ID")

// ACLRule is an alias of storage.ACLRule to have custom JSON marshal
type ACLRule storage.ACLRule

// Object represents the object that is stored within the fake server.
type Object struct {
BucketName string `json:"-"`
BucketName string `json:"bucket"`
Name string `json:"name"`
ContentType string `json:"contentType"`
ContentEncoding string `json:"contentEncoding"`
Content []byte `json:"-"`
// Crc32c checksum of Content. calculated by server when it's upload methods are used.
Crc32c string `json:"crc32c,omitempty"`
Md5Hash string `json:"md5Hash,omitempty"`
ACL []storage.ACLRule `json:"acl,omitempty"`
Crc32c string `json:"crc32c,omitempty"`
Md5Hash string `json:"md5Hash,omitempty"`
ACL []ACLRule `json:"acl,omitempty"`
// Dates and generation can be manually injected, so you can do assertions on them,
// or let us fill these fields for you
Created time.Time `json:"created,omitempty"`
Expand All @@ -42,6 +45,74 @@ type Object struct {
Metadata map[string]string `json:"metadata,omitempty"`
}

// ProjectTeam is an alias of storage.ProjectTeam to have custom JSON marshal
type ProjectTeam storage.ProjectTeam

// MarshalJSON for ACLRule to customize field names
func (acl ACLRule) MarshalJSON() ([]byte, error) {
temp := struct {
Entity storage.ACLEntity `json:"entity"`
EntityID string `json:"entityId"`
Role storage.ACLRole `json:"role"`
Domain string `json:"domain"`
Email string `json:"email"`
ProjectTeam *ProjectTeam `json:"projectTeam"`
}{}
temp.Entity = acl.Entity
temp.EntityID = acl.EntityID
temp.Role = acl.Role
temp.Domain = acl.Domain
temp.Email = acl.Email
temp.ProjectTeam = (*ProjectTeam)(acl.ProjectTeam)
return json.Marshal(temp)
}

// UnmarshalJSON for ACLRule to customize field names
func (acl *ACLRule) UnmarshalJSON(data []byte) error {
temp := struct {
Entity storage.ACLEntity `json:"entity"`
EntityID string `json:"entityId"`
Role storage.ACLRole `json:"role"`
Domain string `json:"domain"`
Email string `json:"email"`
ProjectTeam *ProjectTeam `json:"projectTeam"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
acl.Entity = temp.Entity
acl.EntityID = temp.EntityID
acl.Role = temp.Role
acl.Domain = temp.Domain
acl.Email = temp.Email
acl.ProjectTeam = (*storage.ProjectTeam)(temp.ProjectTeam)
return nil
}

// MarshalJSON for ProjectTeam to customize field names
func (team ProjectTeam) MarshalJSON() ([]byte, error) {
temp := struct {
ProjectNumber string `json:"projectNumber"`
Team string `json:"team"`
}{}
temp.ProjectNumber = team.ProjectNumber
temp.Team = team.Team
return json.Marshal(temp)
}

// UnmarshalJSON for ProjectTeam to customize field names
func (team *ProjectTeam) UnmarshalJSON(data []byte) error {
temp := struct {
ProjectNumber string `json:"projectNumber"`
Team string `json:"team"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
team.ProjectNumber = temp.ProjectNumber
team.Team = temp.Team
return nil
}
func (o *Object) id() string {
return o.BucketName + "/" + o.Name
}
Expand Down Expand Up @@ -119,6 +190,22 @@ func getCurrentIfZero(date time.Time) time.Time {
return date
}

func toBackendACLRule(acls []ACLRule) []storage.ACLRule {
backendAcls := make([]storage.ACLRule, len(acls))
for i, acl := range acls {
backendAcls[i] = storage.ACLRule(acl)
}
return backendAcls
}

func fromBackendACLRule(backendAcls []storage.ACLRule) []ACLRule {
acls := make([]ACLRule, len(backendAcls))
for i, acl := range backendAcls {
acls[i] = ACLRule(acl)
}
return acls
}

func toBackendObjects(objects []Object) []backend.Object {
backendObjects := []backend.Object{}
for _, o := range objects {
Expand All @@ -130,7 +217,7 @@ func toBackendObjects(objects []Object) []backend.Object {
ContentEncoding: o.ContentEncoding,
Crc32c: o.Crc32c,
Md5Hash: o.Md5Hash,
ACL: o.ACL,
ACL: toBackendACLRule(o.ACL),
Created: getCurrentIfZero(o.Created).Format(timestampFormat),
Deleted: o.Deleted.Format(timestampFormat),
Updated: getCurrentIfZero(o.Updated).Format(timestampFormat),
Expand All @@ -152,7 +239,7 @@ func fromBackendObjects(objects []backend.Object) []Object {
ContentEncoding: o.ContentEncoding,
Crc32c: o.Crc32c,
Md5Hash: o.Md5Hash,
ACL: o.ACL,
ACL: fromBackendACLRule(o.ACL),
Created: convertTimeWithoutError(o.Created),
Deleted: convertTimeWithoutError(o.Deleted),
Updated: convertTimeWithoutError(o.Updated),
Expand Down Expand Up @@ -291,7 +378,7 @@ func (s *Server) setObjectACL(w http.ResponseWriter, r *http.Request) {

entity := storage.ACLEntity(data.Entity)
role := storage.ACLRole(data.Role)
obj.ACL = []storage.ACLRule{{
obj.ACL = []ACLRule{{
Entity: entity,
Role: role,
}}
Expand Down
7 changes: 3 additions & 4 deletions fakestorage/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"strconv"
"strings"

"cloud.google.com/go/storage"
"github.com/gorilla/mux"
)

Expand Down Expand Up @@ -179,17 +178,17 @@ func (s *Server) signedUpload(bucketName string, w http.ResponseWriter, r *http.
json.NewEncoder(w).Encode(obj)
}

func getObjectACL(predefinedACL string) []storage.ACLRule {
func getObjectACL(predefinedACL string) []ACLRule {
if predefinedACL == "publicRead" {
return []storage.ACLRule{
return []ACLRule{
{
Entity: "allUsers",
Role: "READER",
},
}
}

return []storage.ACLRule{
return []ACLRule{
{
Entity: "projectOwner",
Role: "OWNER",
Expand Down

0 comments on commit 42689d8

Please sign in to comment.