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

Side by Side Diff: go/src/infra/gae/libs/ephelper/ephelper.go

Issue 1153473008: A client/server helper wrapper for endpoints in Go. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: rm newline Created 5 years, 6 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
(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")
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.
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)
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
74 }
75
76 return nil
77 }
OLDNEW
« no previous file with comments | « go/src/infra/gae/epservice/example/service_list.go ('k') | go/src/infra/gae/libs/ephelper/ephelper_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698