|
| 1 | +// Copyright 2019 Google Inc. All rights reserved. |
| 2 | +// Use of this source code is governed by the Apache 2.0 |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package cloudpb is a subset of types and functions, copied from cloud.google.com/go/datastore. |
| 6 | +// |
| 7 | +// They are copied here to provide compatibility to decode keys generated by the cloud.google.com/go/datastore package. |
| 8 | +package cloudkey |
| 9 | + |
| 10 | +import ( |
| 11 | + "encoding/base64" |
| 12 | + "errors" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/golang/protobuf/proto" |
| 16 | + cloudpb "google.golang.org/appengine/datastore/internal/cloudpb" |
| 17 | +) |
| 18 | + |
| 19 | +///////////////////////////////////////////////////////////////////// |
| 20 | +// Code below is copied from https://github.com/googleapis/google-cloud-go/blob/master/datastore/datastore.go |
| 21 | +///////////////////////////////////////////////////////////////////// |
| 22 | + |
| 23 | +var ( |
| 24 | + // ErrInvalidKey is returned when an invalid key is presented. |
| 25 | + ErrInvalidKey = errors.New("datastore: invalid key") |
| 26 | +) |
| 27 | + |
| 28 | +///////////////////////////////////////////////////////////////////// |
| 29 | +// Code below is copied from https://github.com/googleapis/google-cloud-go/blob/master/datastore/key.go |
| 30 | +///////////////////////////////////////////////////////////////////// |
| 31 | + |
| 32 | +// Key represents the datastore key for a stored entity. |
| 33 | +type Key struct { |
| 34 | + // Kind cannot be empty. |
| 35 | + Kind string |
| 36 | + // Either ID or Name must be zero for the Key to be valid. |
| 37 | + // If both are zero, the Key is incomplete. |
| 38 | + ID int64 |
| 39 | + Name string |
| 40 | + // Parent must either be a complete Key or nil. |
| 41 | + Parent *Key |
| 42 | + |
| 43 | + // Namespace provides the ability to partition your data for multiple |
| 44 | + // tenants. In most cases, it is not necessary to specify a namespace. |
| 45 | + // See docs on datastore multitenancy for details: |
| 46 | + // https://cloud.google.com/datastore/docs/concepts/multitenancy |
| 47 | + Namespace string |
| 48 | +} |
| 49 | + |
| 50 | +// DecodeKey decodes a key from the opaque representation returned by Encode. |
| 51 | +func DecodeKey(encoded string) (*Key, error) { |
| 52 | + // Re-add padding. |
| 53 | + if m := len(encoded) % 4; m != 0 { |
| 54 | + encoded += strings.Repeat("=", 4-m) |
| 55 | + } |
| 56 | + |
| 57 | + b, err := base64.URLEncoding.DecodeString(encoded) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + |
| 62 | + pKey := new(cloudpb.Key) |
| 63 | + if err := proto.Unmarshal(b, pKey); err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + return protoToKey(pKey) |
| 67 | +} |
| 68 | + |
| 69 | +// valid returns whether the key is valid. |
| 70 | +func (k *Key) valid() bool { |
| 71 | + if k == nil { |
| 72 | + return false |
| 73 | + } |
| 74 | + for ; k != nil; k = k.Parent { |
| 75 | + if k.Kind == "" { |
| 76 | + return false |
| 77 | + } |
| 78 | + if k.Name != "" && k.ID != 0 { |
| 79 | + return false |
| 80 | + } |
| 81 | + if k.Parent != nil { |
| 82 | + if k.Parent.Incomplete() { |
| 83 | + return false |
| 84 | + } |
| 85 | + if k.Parent.Namespace != k.Namespace { |
| 86 | + return false |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return true |
| 91 | +} |
| 92 | + |
| 93 | +// Incomplete reports whether the key does not refer to a stored entity. |
| 94 | +func (k *Key) Incomplete() bool { |
| 95 | + return k.Name == "" && k.ID == 0 |
| 96 | +} |
| 97 | + |
| 98 | +// protoToKey decodes a protocol buffer representation of a key into an |
| 99 | +// equivalent *Key object. If the key is invalid, protoToKey will return the |
| 100 | +// invalid key along with ErrInvalidKey. |
| 101 | +func protoToKey(p *cloudpb.Key) (*Key, error) { |
| 102 | + var key *Key |
| 103 | + var namespace string |
| 104 | + if partition := p.PartitionId; partition != nil { |
| 105 | + namespace = partition.NamespaceId |
| 106 | + } |
| 107 | + for _, el := range p.Path { |
| 108 | + key = &Key{ |
| 109 | + Namespace: namespace, |
| 110 | + Kind: el.Kind, |
| 111 | + ID: el.GetId(), |
| 112 | + Name: el.GetName(), |
| 113 | + Parent: key, |
| 114 | + } |
| 115 | + } |
| 116 | + if !key.valid() { // Also detects key == nil. |
| 117 | + return key, ErrInvalidKey |
| 118 | + } |
| 119 | + return key, nil |
| 120 | +} |
0 commit comments