schema: share is a claim with claimType="share"

http://camlistore.org/issue/150

Change-Id: I1dcd1709c886839df507b02beee2e19986783693
This commit is contained in:
mpl 2013-06-21 22:23:37 +02:00
parent 39afc93833
commit febbaf1387
3 changed files with 79 additions and 54 deletions

View File

@ -132,7 +132,7 @@ func (b *Blob) AsShare() (s Share, ok bool) {
if !ok {
return
}
if b.ss.Type == "share" && b.ss.AuthType == ShareHaveRef && b.ss.Target != nil {
if b.ss.ClaimType == claimTypeShare && b.ss.AuthType == ShareHaveRef && b.ss.Target != nil {
return Share{c}, true
}
return
@ -161,17 +161,19 @@ func (b *Blob) StaticSetMembers() []*blobref.BlobRef {
}
func (b *Blob) ShareAuthType() string {
if b.Type() != "share" {
s, ok := b.AsShare()
if !ok {
return ""
}
return b.ss.AuthType
return s.AuthType()
}
func (b *Blob) ShareTarget() *blobref.BlobRef {
if b.Type() != "share" {
s, ok := b.AsShare()
if !ok {
return nil
}
return b.ss.Target
return s.Target()
}
// ModTime returns the "unixMtime" field, or the zero time.
@ -210,9 +212,14 @@ type Share struct {
Claim
}
// AuthType returns the AuthType of the Share.
func (s Share) AuthType() string {
return s.b.ss.AuthType
}
// Target returns the blob referenced by the Share.
func (s Share) Target() *blobref.BlobRef {
return s.b.ShareTarget()
return s.b.ss.Target
}
// IsTransitive returns whether the Share transitively

View File

@ -636,14 +636,6 @@ func newBytes() *Builder {
return base(1, "bytes")
}
func NewShareRef(authType string, target *blobref.BlobRef, transitive bool) *Builder {
bb := base(1, "share")
bb.m["authType"] = authType
bb.m["target"] = target.String()
bb.m["transitive"] = transitive
return bb
}
type ClaimType string
const (
@ -652,14 +644,24 @@ const (
DelAttribute ClaimType = "del-attribute"
)
type ClaimParam struct {
Permanode *blobref.BlobRef // modified permanode
Type ClaimType
Attribute string // required
Value string // optional if Type == DelAttribute
const claimTypeShare = "share"
// claimParam is used to populate a claim map when building a new claim
type claimParam struct {
claimType ClaimType
// Params specific to *Attribute claims:
permanode *blobref.BlobRef // modified permanode
attribute string // required
value string // optional if Type == DelAttribute
// Params specific to "share" claims:
authType string
target *blobref.BlobRef
transitive bool
}
func NewClaim(claims ...*ClaimParam) *Builder {
func NewClaim(claims ...*claimParam) *Builder {
bb := base(1, "claim")
bb.SetClaimDate(time.Now())
if len(claims) == 1 {
@ -678,38 +680,54 @@ func NewClaim(claims ...*ClaimParam) *Builder {
return bb
}
func populateClaimMap(m map[string]interface{}, cp *ClaimParam) {
m["claimType"] = string(cp.Type)
m["attribute"] = cp.Attribute
m["permaNode"] = cp.Permanode.String()
if !(cp.Type == DelAttribute && cp.Value == "") {
m["value"] = cp.Value
func populateClaimMap(m map[string]interface{}, cp *claimParam) {
m["claimType"] = string(cp.claimType)
if cp.claimType != claimTypeShare {
m["permaNode"] = cp.permanode.String()
m["attribute"] = cp.attribute
if !(cp.claimType == DelAttribute && cp.value == "") {
m["value"] = cp.value
}
} else {
m["authType"] = cp.authType
m["target"] = cp.target.String()
m["transitive"] = cp.transitive
}
}
// NewShareRef creates a *Builder for a "share" claim.
func NewShareRef(authType string, target *blobref.BlobRef, transitive bool) *Builder {
return NewClaim(&claimParam{
claimType: claimTypeShare,
authType: authType,
target: target,
transitive: transitive,
})
}
func NewSetAttributeClaim(permaNode *blobref.BlobRef, attr, value string) *Builder {
return NewClaim(&ClaimParam{
Permanode: permaNode,
Type: SetAttribute,
Attribute: attr,
Value: value,
return NewClaim(&claimParam{
permanode: permaNode,
claimType: SetAttribute,
attribute: attr,
value: value,
})
}
func NewAddAttributeClaim(permaNode *blobref.BlobRef, attr, value string) *Builder {
return NewClaim(&ClaimParam{
Permanode: permaNode,
Type: AddAttribute,
Attribute: attr,
Value: value,
return NewClaim(&claimParam{
permanode: permaNode,
claimType: AddAttribute,
attribute: attr,
value: value,
})
}
func NewDelAttributeClaim(permaNode *blobref.BlobRef, attr string) *Builder {
return NewClaim(&ClaimParam{
Permanode: permaNode,
Type: DelAttribute,
Attribute: attr,
return NewClaim(&claimParam{
permanode: permaNode,
claimType: DelAttribute,
attribute: attr,
})
}

View File

@ -170,20 +170,20 @@ func TestAttribute(t *testing.T) {
}`,
},
{
bb: NewClaim(&ClaimParam{
Permanode: br,
Type: SetAttribute,
Attribute: "foo",
Value: "bar",
}, &ClaimParam{
Permanode: br,
Type: DelAttribute,
Attribute: "foo",
Value: "specific-del",
}, &ClaimParam{
Permanode: br,
Type: DelAttribute,
Attribute: "foo",
bb: NewClaim(&claimParam{
permanode: br,
claimType: SetAttribute,
attribute: "foo",
value: "bar",
}, &claimParam{
permanode: br,
claimType: DelAttribute,
attribute: "foo",
value: "specific-del",
}, &claimParam{
permanode: br,
claimType: DelAttribute,
attribute: "foo",
}),
want: `{"camliVersion": 1,
"camliType": "claim",