// Valid determines if a key is valid, according to a couple rules: // - k is not nil // - every token of k: // - (if !allowSpecial) token's kind doesn't start with '__' // - token's kind and appid are non-blank // - token is not incomplete // - all tokens have the same namespace and appid func Valid(k ds.Key, allowSpecial bool, aid, ns string) bool { if k == nil { return false } if aid != k.AppID() || ns != k.Namespace() { return false } for ; k != nil; k = k.Parent() { if !allowSpecial && len(k.Kind()) >= 2 && k.Kind()[:2] == "__" { return false } if k.Kind() == "" || k.AppID() == "" { return false } if k.StringID() != "" && k.IntID() != 0 { return false } if k.Parent() != nil { if k.Parent().Incomplete() { return false } if k.Parent().AppID() != k.AppID() || k.Parent().Namespace() != k.Namespace() { return false } } } return true }
// Equal returns true iff the two keys represent identical key values. func Equal(a, b ds.Key) (ret bool) { ret = (a.Kind() == b.Kind() && a.StringID() == b.StringID() && a.IntID() == b.IntID() && a.AppID() == b.AppID() && a.Namespace() == b.Namespace()) if !ret { return } ap, bp := a.Parent(), b.Parent() return (ap == nil && bp == nil) || Equal(ap, bp) }
func marshalDSKey(b *bytes.Buffer, k ds.Key) { if k.Parent() != nil { marshalDSKey(b, k.Parent()) } b.WriteByte('/') b.WriteString(k.Kind()) b.WriteByte(',') if k.StringID() != "" { b.WriteString(k.StringID()) } else { b.WriteString(strconv.FormatInt(k.IntID(), 10)) } }
// Incomplete returns true iff k doesn't have an id yet. func Incomplete(k ds.Key) bool { return k != nil && k.StringID() == "" && k.IntID() == 0 }