Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: luci_config/server/cfgclient/backend/caching/codec.go

Issue 2963503003: [errors] Greatly simplify common/errors package. (Closed)
Patch Set: fix nits Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The LUCI Authors. All rights reserved. 1 // Copyright 2015 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0 2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file. 3 // that can be found in the LICENSE file.
4 4
5 package caching 5 package caching
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "compress/zlib" 9 "compress/zlib"
10 "crypto/sha256" 10 "crypto/sha256"
11 "encoding/binary" 11 "encoding/binary"
12 "encoding/json" 12 "encoding/json"
13 "strings" 13 "strings"
14 14
15 "github.com/luci/luci-go/common/errors" 15 "github.com/luci/luci-go/common/errors"
16 ) 16 )
17 17
18 // Encode is a convenience method for generating a ZLIB-compressed JSON-encoded 18 // Encode is a convenience method for generating a ZLIB-compressed JSON-encoded
19 // object. 19 // object.
20 func Encode(v interface{}) ([]byte, error) { 20 func Encode(v interface{}) ([]byte, error) {
21 var buf bytes.Buffer 21 var buf bytes.Buffer
22 zw := zlib.NewWriter(&buf) 22 zw := zlib.NewWriter(&buf)
23 enc := json.NewEncoder(zw) 23 enc := json.NewEncoder(zw)
24 24
25 if err := enc.Encode(v); err != nil { 25 if err := enc.Encode(v); err != nil {
26 zw.Close() 26 zw.Close()
27 » » return nil, errors.Annotate(err).Reason("failed to JSON-encode") .Err() 27 » » return nil, errors.Annotate(err, "failed to JSON-encode").Err()
28 } 28 }
29 if err := zw.Close(); err != nil { 29 if err := zw.Close(); err != nil {
30 » » return nil, errors.Annotate(err).Reason("failed to Close zlib Wr iter").Err() 30 » » return nil, errors.Annotate(err, "failed to Close zlib Writer"). Err()
31 } 31 }
32 return buf.Bytes(), nil 32 return buf.Bytes(), nil
33 } 33 }
34 34
35 // Decode is a convenience method for decoding a ZLIB-compressed JSON-encoded 35 // Decode is a convenience method for decoding a ZLIB-compressed JSON-encoded
36 // object encoded by Encode. 36 // object encoded by Encode.
37 func Decode(d []byte, v interface{}) error { 37 func Decode(d []byte, v interface{}) error {
38 zr, err := zlib.NewReader(bytes.NewReader(d)) 38 zr, err := zlib.NewReader(bytes.NewReader(d))
39 if err != nil { 39 if err != nil {
40 » » return errors.Annotate(err).Reason("failed to create zlib Reader ").Err() 40 » » return errors.Annotate(err, "failed to create zlib Reader").Err( )
41 } 41 }
42 defer zr.Close() 42 defer zr.Close()
43 43
44 if err := json.NewDecoder(zr).Decode(v); err != nil { 44 if err := json.NewDecoder(zr).Decode(v); err != nil {
45 » » return errors.Annotate(err).Reason("failed to JSON-decode").Err( ) 45 » » return errors.Annotate(err, "failed to JSON-decode").Err()
46 } 46 }
47 return nil 47 return nil
48 } 48 }
49 49
50 // HashParams is a convenience method for hashing a series of strings into a 50 // HashParams is a convenience method for hashing a series of strings into a
51 // unique hash key for that series. 51 // unique hash key for that series.
52 // 52 //
53 // The output is fixed-size sha256.Size (32) bytes. 53 // The output is fixed-size sha256.Size (32) bytes.
54 func HashParams(params ...string) []byte { 54 func HashParams(params ...string) []byte {
55 size := 0 55 size := 0
(...skipping 20 matching lines...) Expand all
76 _, _ = buf.Write(bytes.Replace([]byte(params[i]), []byte {0x00}, []byte{0x00, 0x00}, -1)) 76 _, _ = buf.Write(bytes.Replace([]byte(params[i]), []byte {0x00}, []byte{0x00, 0x00}, -1))
77 } else { 77 } else {
78 _, _ = buf.WriteString(s) 78 _, _ = buf.WriteString(s)
79 } 79 }
80 buf.WriteByte(0x00) 80 buf.WriteByte(0x00)
81 } 81 }
82 82
83 hash := sha256.Sum256(buf.Bytes()) 83 hash := sha256.Sum256(buf.Bytes())
84 return hash[:] 84 return hash[:]
85 } 85 }
OLDNEW
« no previous file with comments | « luci_config/server/cfgclient/backend/authority.go ('k') | luci_config/server/cfgclient/backend/caching/config.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698