OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package model |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "math" |
| 10 |
| 11 "golang.org/x/net/context" |
| 12 |
| 13 "github.com/luci/gae/service/datastore" |
| 14 dm "github.com/luci/luci-go/common/api/dm/service/v1" |
| 15 ) |
| 16 |
| 17 // QuestKeyFromID makes a datastore.Key given the QuestID. |
| 18 func QuestKeyFromID(c context.Context, qid string) *datastore.Key { |
| 19 return datastore.Get(c).MakeKey("Quest", qid) |
| 20 } |
| 21 |
| 22 // QuestFromID produces an empty Quest model from the QuestID. |
| 23 func QuestFromID(qid string) *Quest { |
| 24 return &Quest{ID: qid} |
| 25 } |
| 26 |
| 27 // QuestIDFromKey makes a QuestID from the given datastore.Key. It panics if the |
| 28 // Key does not point to a Quest. |
| 29 func QuestIDFromKey(k *datastore.Key) string { |
| 30 if k.Kind() != "Quest" || k.Parent() != nil { |
| 31 panic(fmt.Errorf("invalid Quest key: %s", k)) |
| 32 } |
| 33 return k.StringID() |
| 34 } |
| 35 |
| 36 // AttemptKeyFromID makes a datastore.Key given the AttemptID. |
| 37 func AttemptKeyFromID(c context.Context, aid *dm.Attempt_ID) *datastore.Key { |
| 38 return datastore.Get(c).MakeKey("Attempt", aid.DMEncoded()) |
| 39 } |
| 40 |
| 41 // AttemptFromID produces an empty Attempt model from the AttemptID. |
| 42 func AttemptFromID(aid *dm.Attempt_ID) *Attempt { |
| 43 ret := &Attempt{} |
| 44 ret.ID = *aid |
| 45 return ret |
| 46 } |
| 47 |
| 48 // AttemptIDFromKey makes a AttemptID from the given datastore.Key. It panics if
the |
| 49 // Key does not point to a Attempt. |
| 50 func AttemptIDFromKey(k *datastore.Key) *dm.Attempt_ID { |
| 51 if k.Kind() != "Attempt" || k.Parent() != nil { |
| 52 panic(fmt.Errorf("invalid Attempt key: %s", k)) |
| 53 } |
| 54 ret := &dm.Attempt_ID{} |
| 55 if err := ret.SetDMEncoded(k.StringID()); err != nil { |
| 56 panic(fmt.Errorf("invalid Attempt key: %s: %s", k, err)) |
| 57 } |
| 58 return ret |
| 59 } |
| 60 |
| 61 // ExecutionKeyFromID makes a datastore.Key given the ExecutionID. |
| 62 func ExecutionKeyFromID(c context.Context, eid *dm.Execution_ID) *datastore.Key
{ |
| 63 return datastore.Get(c).MakeKey("Attempt", eid.AttemptID().DMEncoded(),
"Execution", eid.Id) |
| 64 } |
| 65 |
| 66 // ExecutionFromID produces an empty Execution model from the ExecutionID. |
| 67 func ExecutionFromID(c context.Context, eid *dm.Execution_ID) *Execution { |
| 68 ret := &Execution{} |
| 69 ret.ID = eid.Id |
| 70 ret.Attempt = AttemptKeyFromID(c, eid.AttemptID()) |
| 71 return ret |
| 72 } |
| 73 |
| 74 // ExecutionIDFromKey makes a ExecutionID from the given datastore.Key. It panic
s if the |
| 75 // Key does not point to a Execution. |
| 76 func ExecutionIDFromKey(k *datastore.Key) *dm.Execution_ID { |
| 77 if k.Kind() != "Execution" || k.Parent() == nil { |
| 78 panic(fmt.Errorf("invalid Execution key: %s", k)) |
| 79 } |
| 80 id := k.IntID() |
| 81 if id <= 0 || id > math.MaxUint32 { |
| 82 panic(fmt.Errorf("invalid Execution key: %s", k)) |
| 83 } |
| 84 atmpt := AttemptIDFromKey(k.Parent()) |
| 85 return &dm.Execution_ID{Quest: atmpt.Quest, Attempt: atmpt.Id, Id: uint3
2(id)} |
| 86 } |
OLD | NEW |