OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package dm | |
6 | |
7 import ( | |
8 "crypto/sha256" | |
9 "encoding/base64" | |
10 "fmt" | |
11 | |
12 "github.com/golang/protobuf/proto" | |
13 "github.com/luci/luci-go/common/api/template" | |
14 ) | |
15 | |
16 // IsEmpty returns true if this metadata retry message only contains | |
17 // zero-values. | |
18 func (q *Quest_Desc_Meta_Retry) IsEmpty() bool { | |
19 return q.Crashed == 0 && q.Expired == 0 && q.Failed == 0 && q.TimedOut = = 0 | |
20 } | |
21 | |
22 // IsEmpty returns true if this metadata only contains zero-values. | |
23 func (q *Quest_Desc_Meta) IsEmpty() bool { | |
24 return q.AsAccount == "" && q.Retry.IsEmpty() | |
25 } | |
26 | |
27 var ( | |
28 // QuestIDLength is the number of encoded bytes to use. It removes the | |
29 // single padding character. | |
30 QuestIDLength = base64.URLEncoding.EncodedLen(sha256.Size) - 1 | |
31 ) | |
32 | |
33 // QuestDescPayloadMaxLength is the maximum length (in bytes) of an | |
34 // un-normalized Quest payload. | |
35 const QuestDescPayloadMaxLength = 256 * 1024 | |
36 | |
37 // Normalize returns an error iff the Quest_Desc is invalid. | |
38 func (q *Quest_Desc) Normalize() error { | |
39 if q.Meta == nil { | |
40 q.Meta = &Quest_Desc_Meta{Retry: &Quest_Desc_Meta_Retry{}} | |
41 } else if q.Meta.Retry == nil { | |
42 q.Meta.Retry = &Quest_Desc_Meta_Retry{} | |
43 } | |
44 | |
45 if len(q.JsonPayload) > QuestDescPayloadMaxLength { | |
46 return fmt.Errorf("quest payload is too large: %d > %d", | |
47 len(q.JsonPayload), QuestDescPayloadMaxLength) | |
48 } | |
49 normed, err := template.NormalizeJSON(q.JsonPayload, true) | |
50 if err != nil { | |
51 return fmt.Errorf("failed to normalize payload: %s", err) | |
52 } | |
53 q.JsonPayload = normed | |
54 return nil | |
55 } | |
56 | |
57 // QuestID computes the DM compatible quest ID for this Quest_Desc. The | |
58 // Quest_Desc should already be Normalize()'d. | |
59 func (q *Quest_Desc) QuestID() string { | |
60 data, err := proto.Marshal(q) | |
61 if err != nil { | |
62 panic(err) | |
63 } | |
64 h := sha256.Sum256(data) | |
65 return base64.URLEncoding.EncodeToString(h[:])[:QuestIDLength] | |
dnj (Google)
2016/06/09 18:00:57
Note that if we ever change QuestIDLength, data co
iannucci
2016/06/15 00:46:02
? We do, we're just chopping off the extra, consta
| |
66 } | |
OLD | NEW |