OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 ephelper | |
6 | |
7 import ( | |
8 "errors" | |
9 "fmt" | |
10 | |
11 "github.com/GoogleCloudPlatform/go-endpoints/endpoints" | |
12 ) | |
13 | |
14 const errPrefix = "endpoints.Register: " | |
15 | |
16 var ( | |
17 // ErrServerNil is returned if you pass a nil server to Register. Don't do | |
18 // that. | |
19 ErrServerNil = errors.New(errPrefix + "server is nil") | |
Vadim Sh.
2015/06/03 18:13:39
just let it die with nil-dereference panic. Are yo
| |
20 | |
21 // ErrServiceNil is returned if you pass a nil service to Register. Don' t do | |
22 // that. | |
23 ErrServiceNil = errors.New(errPrefix + "service is nil") | |
24 ) | |
25 | |
26 // MethodInfoMap is the common registry for an endpoints service. It's | |
27 // used by infra/libs/endpoints_client to populate its API. | |
Vadim Sh.
2015/06/03 18:13:39
update infra/libs/endpoints_client
| |
28 type MethodInfoMap map[string]*endpoints.MethodInfo | |
29 | |
30 // Register adds an endpoints.RegisterService-compatible service object using | |
31 // the MethodInfoMap to look up the MethodInfo objects by methodName. It is | |
32 // intended to be called at init()-time of an app or client which relies on | |
33 // these endpoints. | |
34 // | |
35 // service should be an instance of your service type (as if you were passing | |
36 // it to "go-endpoints/endpoints".RegisterService). | |
37 func Register(server *endpoints.Server, service interface{}, si *endpoints.Servi ceInfo, mi MethodInfoMap) error { | |
38 if server == nil { | |
39 return ErrServerNil | |
40 } | |
41 if service == nil { | |
42 return ErrServiceNil | |
43 } | |
44 if si == nil { | |
45 si = &endpoints.ServiceInfo{Default: true} | |
46 } | |
47 | |
48 api, err := server.RegisterService(service, si.Name, si.Version, | |
49 si.Description, si.Default) | |
Vadim Sh.
2015/06/03 18:13:39
nit: imho line break here is unnecessary
| |
50 if err != nil { | |
51 return err | |
52 } | |
53 | |
54 for methodName, info := range mi { | |
55 method := api.MethodByName(methodName) | |
56 if method == nil { | |
57 return fmt.Errorf( | |
58 errPrefix+"no method %q (did you forget to expor t it?)", methodName) | |
59 } | |
60 curInfo := method.Info() | |
61 // These three are set automatically based on reflection, so onl y override | |
62 // them if the info object contains something new. | |
63 if info.Name == "" { | |
64 info.Name = curInfo.Name | |
65 } | |
66 if info.Path == "" { | |
67 info.Path = curInfo.Path | |
68 } | |
69 if info.HTTPMethod == "" { | |
70 info.HTTPMethod = curInfo.HTTPMethod | |
71 } | |
72 *curInfo = *info | |
73 mi[methodName] = curInfo // So that we can observe the merged re sult | |
Vadim Sh.
2015/06/03 18:13:39
is it ok to modify map while iterating over it?
| |
74 } | |
75 | |
76 return nil | |
77 } | |
OLD | NEW |