Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | |
|
iannucci
2017/01/07 20:12:15
maybe 'luci-config' top level folder and maybe 'se
| |
| 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 config | |
| 6 | |
| 7 import ( | |
| 8 "encoding/json" | |
| 9 "fmt" | |
| 10 | |
| 11 "github.com/luci/luci-go/common/errors" | |
| 12 "github.com/luci/luci-go/server/auth" | |
| 13 ) | |
| 14 | |
| 15 // Authority is the authority that is requesting configurations. It can be | |
| 16 // installed via WithAuthority. | |
| 17 // | |
| 18 // Authority marshals/unmarshals to/from a compact JSON representation. This is | |
| 19 // used by the caching layer. | |
| 20 type Authority int | |
| 21 | |
| 22 const ( | |
| 23 // AsAnonymous requests config data as an anonymous user. | |
| 24 // | |
| 25 // Corresponds to auth.NoAuth. | |
| 26 AsAnonymous Authority = iota | |
| 27 | |
| 28 // AsService requests config data as the currently-running service. | |
| 29 // | |
| 30 // Corresponds to auth.AsSelf. | |
| 31 AsService | |
| 32 | |
| 33 // AsUser requests config data as the currently logged-in user. | |
| 34 // | |
| 35 // Corresponds to auth.AsUser. | |
| 36 AsUser | |
| 37 ) | |
| 38 | |
| 39 // String implements fmt.Stringer. | |
| 40 func (a Authority) String() string { | |
|
iannucci
2017/01/07 20:12:15
use stringer tool? https://godoc.org/golang.org/x/
dnj
2017/01/10 03:25:57
Done.
| |
| 41 switch a { | |
| 42 case AsAnonymous: | |
| 43 return "AsAnonymous" | |
| 44 case AsService: | |
| 45 return "AsService" | |
| 46 case AsUser: | |
| 47 return "AsUser" | |
| 48 default: | |
| 49 return fmt.Sprintf("Unknown(%d)", a) | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 // rpcAuthorityKind returns the RPC authority associated with this authority | |
| 54 // level. | |
| 55 func (a Authority) rpcAuthorityKind() auth.RPCAuthorityKind { | |
| 56 switch a { | |
| 57 case AsAnonymous: | |
| 58 return auth.NoAuth | |
| 59 case AsService: | |
| 60 return auth.AsSelf | |
| 61 case AsUser: | |
| 62 return auth.AsUser | |
| 63 default: | |
| 64 panic(fmt.Errorf("unknown config Authority (%d)", a)) | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 // MarshalJSON implements encoding/json.Marshaler. | |
| 69 func (a Authority) MarshalJSON() ([]byte, error) { | |
| 70 switch a { | |
| 71 case AsAnonymous: | |
| 72 return json.Marshal("") | |
| 73 case AsService: | |
| 74 return json.Marshal("S") | |
| 75 case AsUser: | |
| 76 return json.Marshal("U") | |
|
iannucci
2017/01/07 20:12:15
not sure it's worth calling json.Marshal here, con
dnj
2017/01/10 03:25:57
It would actually be []byte(`"U"`), which is a mis
| |
| 77 default: | |
| 78 return nil, errors.Reason("unknown authority: %(auth)v").D("auth ", a).Err() | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 // UnmarshalJSON implements encoding/json.Unmarshaler. | |
| 83 func (a *Authority) UnmarshalJSON(d []byte) error { | |
|
iannucci
2017/01/07 20:12:15
with constants mentioned above this becomes
swit
dnj
2017/01/10 03:25:57
Done.
| |
| 84 var v string | |
| 85 if err := json.Unmarshal(d, &v); err != nil { | |
| 86 return errors.Annotate(err).Reason("failed to unmarshal JSON: %( value)v").D("value", d).Err() | |
| 87 } | |
| 88 | |
| 89 switch v { | |
| 90 case "": | |
| 91 *a = AsAnonymous | |
| 92 return nil | |
| 93 case "S": | |
| 94 *a = AsService | |
| 95 return nil | |
| 96 case "U": | |
| 97 *a = AsUser | |
| 98 return nil | |
| 99 default: | |
| 100 return errors.Reason("unknown authority JSON value: %(auth)v").D ("auth", v).Err() | |
| 101 } | |
| 102 } | |
| OLD | NEW |