go.mercari.io/datastore@v1.8.2/clouddatastore/key.go (about)

     1  package clouddatastore
     2  
     3  import (
     4  	"context"
     5  
     6  	"cloud.google.com/go/datastore"
     7  	w "go.mercari.io/datastore"
     8  )
     9  
    10  var _ w.Key = (*keyImpl)(nil)
    11  var _ w.PendingKey = (*pendingKeyImpl)(nil)
    12  
    13  type keyImpl struct {
    14  	kind      string
    15  	id        int64
    16  	name      string
    17  	parent    *keyImpl
    18  	namespace string
    19  }
    20  
    21  type pendingKeyImpl struct {
    22  	pendingKey *datastore.PendingKey
    23  }
    24  
    25  type contextPendingKey struct{}
    26  
    27  func (k *keyImpl) Kind() string {
    28  	if k == nil {
    29  		panic("k is nil")
    30  	}
    31  	return k.kind
    32  }
    33  
    34  func (k *keyImpl) ID() int64 {
    35  	return k.id
    36  }
    37  
    38  func (k *keyImpl) Name() string {
    39  	return k.name
    40  }
    41  
    42  func (k *keyImpl) ParentKey() w.Key {
    43  	if k.parent == nil {
    44  		return nil
    45  	}
    46  	return k.parent
    47  }
    48  
    49  func (k *keyImpl) Namespace() string {
    50  	return k.namespace
    51  }
    52  
    53  func (k *keyImpl) SetNamespace(namespace string) {
    54  	k.namespace = namespace
    55  }
    56  
    57  func (k *keyImpl) String() string {
    58  	return toOriginalKey(k).String()
    59  }
    60  
    61  func (k *keyImpl) GobEncode() ([]byte, error) {
    62  	return toOriginalKey(k).GobEncode()
    63  }
    64  
    65  func (k *keyImpl) GobDecode(buf []byte) error {
    66  	origKey := &datastore.Key{}
    67  	err := origKey.GobDecode(buf)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	k.kind = origKey.Kind
    73  	k.id = origKey.ID
    74  	k.name = origKey.Name
    75  	k.parent = toWrapperKey(origKey.Parent)
    76  	k.namespace = origKey.Namespace
    77  
    78  	return nil
    79  }
    80  
    81  func (k *keyImpl) MarshalJSON() ([]byte, error) {
    82  	return toOriginalKey(k).MarshalJSON()
    83  }
    84  
    85  func (k *keyImpl) UnmarshalJSON(buf []byte) error {
    86  	origKey := &datastore.Key{}
    87  	err := origKey.UnmarshalJSON(buf)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	k.kind = origKey.Kind
    93  	k.id = origKey.ID
    94  	k.name = origKey.Name
    95  	k.parent = toWrapperKey(origKey.Parent)
    96  	k.namespace = origKey.Namespace
    97  
    98  	return nil
    99  }
   100  
   101  func (k *keyImpl) Encode() string {
   102  	return toOriginalKey(k).Encode()
   103  }
   104  
   105  func (k *keyImpl) Equal(o w.Key) bool {
   106  
   107  	var a w.Key = k
   108  	var b = o
   109  	for {
   110  		if a == nil && b == nil {
   111  			return true
   112  		} else if a != nil && b == nil {
   113  			return false
   114  		} else if a == nil && b != nil {
   115  			return false
   116  		}
   117  		if a.Kind() != b.Kind() || a.Name() != b.Name() || a.ID() != b.ID() || a.Namespace() != b.Namespace() {
   118  			return false
   119  		}
   120  
   121  		a = a.ParentKey()
   122  		b = b.ParentKey()
   123  	}
   124  }
   125  
   126  func (k *keyImpl) Incomplete() bool {
   127  	return k.Name() == "" && k.ID() == 0
   128  }
   129  
   130  func (p *pendingKeyImpl) StoredContext() context.Context {
   131  	return context.WithValue(context.Background(), contextPendingKey{}, p)
   132  }