| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // adapted from github.com/golang/appengine/datastore | 5 // adapted from github.com/golang/appengine/datastore |
| 6 | 6 |
| 7 package rawdatastore | 7 package rawdatastore |
| 8 | 8 |
| 9 import ( | 9 import ( |
| 10 "bytes" | 10 "bytes" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 return | 113 return |
| 114 } | 114 } |
| 115 | 115 |
| 116 // KeyIncomplete returns true iff k doesn't have an id yet. | 116 // KeyIncomplete returns true iff k doesn't have an id yet. |
| 117 func KeyIncomplete(k Key) bool { | 117 func KeyIncomplete(k Key) bool { |
| 118 return k != nil && k.StringID() == "" && k.IntID() == 0 | 118 return k != nil && k.StringID() == "" && k.IntID() == 0 |
| 119 } | 119 } |
| 120 | 120 |
| 121 // KeyValid determines if a key is valid, according to a couple rules: | 121 // KeyValid determines if a key is valid, according to a couple rules: |
| 122 // - k is not nil | 122 // - k is not nil |
| 123 // - k's namespace matches ns | |
| 124 // - every token of k: | 123 // - every token of k: |
| 125 // - (if !allowSpecial) token's kind doesn't start with '__' | 124 // - (if !allowSpecial) token's kind doesn't start with '__' |
| 126 // - token's kind and appid are non-blank | 125 // - token's kind and appid are non-blank |
| 127 // - token is not incomplete | 126 // - token is not incomplete |
| 128 // - all tokens have the same namespace and appid | 127 // - all tokens have the same namespace and appid |
| 129 func KeyValid(k Key, ns string, allowSpecial bool) bool { | 128 func KeyValid(k Key, allowSpecial bool, aid, ns string) bool { |
| 130 if k == nil { | 129 if k == nil { |
| 131 return false | 130 return false |
| 132 } | 131 } |
| 133 » // since we do "client-side" validation of namespaces in local | 132 » if aid != k.AppID() || ns != k.Namespace() { |
| 134 » // implementations, it's convenient to check this here. | |
| 135 » if k.Namespace() != ns { | |
| 136 return false | 133 return false |
| 137 } | 134 } |
| 138 for ; k != nil; k = k.Parent() { | 135 for ; k != nil; k = k.Parent() { |
| 139 if !allowSpecial && len(k.Kind()) >= 2 && k.Kind()[:2] == "__" { | 136 if !allowSpecial && len(k.Kind()) >= 2 && k.Kind()[:2] == "__" { |
| 140 return false | 137 return false |
| 141 } | 138 } |
| 142 if k.Kind() == "" || k.AppID() == "" { | 139 if k.Kind() == "" || k.AppID() == "" { |
| 143 return false | 140 return false |
| 144 } | 141 } |
| 145 if k.StringID() != "" && k.IntID() != 0 { | 142 if k.StringID() != "" && k.IntID() != 0 { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 for i := k; i != nil; i = i.Parent() { | 223 for i := k; i != nil; i = i.Parent() { |
| 227 n-- | 224 n-- |
| 228 toks[n].IntID = i.IntID() | 225 toks[n].IntID = i.IntID() |
| 229 toks[n].StringID = i.StringID() | 226 toks[n].StringID = i.StringID() |
| 230 toks[n].Kind = i.Kind() | 227 toks[n].Kind = i.Kind() |
| 231 } | 228 } |
| 232 appID = k.AppID() | 229 appID = k.AppID() |
| 233 namespace = k.Namespace() | 230 namespace = k.Namespace() |
| 234 return | 231 return |
| 235 } | 232 } |
| OLD | NEW |