| 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 backend |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 |
| 10 "github.com/luci/luci-go/common/errors" |
| 11 ) |
| 12 |
| 13 // Authority is the authority that is requesting configurations. It can be |
| 14 // installed via WithAuthority. |
| 15 // |
| 16 // Authority marshals/unmarshals to/from a compact JSON representation. This is |
| 17 // used by the caching layer. |
| 18 type Authority int |
| 19 |
| 20 const ( |
| 21 // AsAnonymous requests config data as an anonymous user. |
| 22 // |
| 23 // Corresponds to auth.NoAuth. |
| 24 AsAnonymous Authority = iota |
| 25 |
| 26 // AsService requests config data as the currently-running service. |
| 27 // |
| 28 // Corresponds to auth.AsSelf. |
| 29 AsService |
| 30 |
| 31 // AsUser requests config data as the currently logged-in user. |
| 32 // |
| 33 // Corresponds to auth.AsUser. |
| 34 AsUser |
| 35 ) |
| 36 |
| 37 var ( |
| 38 asAnonymousJSON = []byte(`""`) |
| 39 asServiceJSON = []byte(`"S"`) |
| 40 asUserJSON = []byte(`"U"`) |
| 41 ) |
| 42 |
| 43 // MarshalJSON implements encoding/json.Marshaler. |
| 44 // |
| 45 // We use a shorthand notation so that we don't waste space in JSON. |
| 46 func (a Authority) MarshalJSON() ([]byte, error) { |
| 47 switch a { |
| 48 case AsAnonymous: |
| 49 return asAnonymousJSON, nil |
| 50 case AsService: |
| 51 return asServiceJSON, nil |
| 52 case AsUser: |
| 53 return asUserJSON, nil |
| 54 default: |
| 55 return nil, errors.Reason("unknown authority: %(auth)v").D("auth
", a).Err() |
| 56 } |
| 57 } |
| 58 |
| 59 // UnmarshalJSON implements encoding/json.Unmarshaler. |
| 60 func (a *Authority) UnmarshalJSON(d []byte) error { |
| 61 switch { |
| 62 case bytes.Equal(d, asAnonymousJSON): |
| 63 *a = AsAnonymous |
| 64 case bytes.Equal(d, asServiceJSON): |
| 65 *a = AsService |
| 66 case bytes.Equal(d, asUserJSON): |
| 67 *a = AsUser |
| 68 default: |
| 69 return errors.Reason("unknown authority JSON value: %(auth)v").D
("auth", d).Err() |
| 70 } |
| 71 return nil |
| 72 } |
| OLD | NEW |