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

Unified 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: rename to better package Created 5 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: go/src/infra/gae/libs/ephelper/ephelper.go
diff --git a/go/src/infra/gae/libs/ephelper/ephelper.go b/go/src/infra/gae/libs/ephelper/ephelper.go
new file mode 100644
index 0000000000000000000000000000000000000000..1a16c638d2ace593c9fc5d96c31a506820604de4
--- /dev/null
+++ b/go/src/infra/gae/libs/ephelper/ephelper.go
@@ -0,0 +1,77 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package ephelper
+
+import (
+ "errors"
+ "fmt"
+
+ "github.com/GoogleCloudPlatform/go-endpoints/endpoints"
+)
+
+const errPrefix = "endpoints.Register: "
+
+var (
+ // ErrServerNil is returned if you pass a nil server to Register. Don't do
+ // that.
+ 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
+
+ // ErrServiceNil is returned if you pass a nil service to Register. Don't do
+ // that.
+ ErrServiceNil = errors.New(errPrefix + "service is nil")
+)
+
+// MethodInfoMap is the common registry for an endpoints service. It's
+// used by infra/libs/endpoints_client to populate its API.
Vadim Sh. 2015/06/03 18:13:39 update infra/libs/endpoints_client
+type MethodInfoMap map[string]*endpoints.MethodInfo
+
+// Register adds an endpoints.RegisterService-compatible service object using
+// the MethodInfoMap to look up the MethodInfo objects by methodName. It is
+// intended to be called at init()-time of an app or client which relies on
+// these endpoints.
+//
+// service should be an instance of your service type (as if you were passing
+// it to "go-endpoints/endpoints".RegisterService).
+func Register(server *endpoints.Server, service interface{}, si *endpoints.ServiceInfo, mi MethodInfoMap) error {
+ if server == nil {
+ return ErrServerNil
+ }
+ if service == nil {
+ return ErrServiceNil
+ }
+ if si == nil {
+ si = &endpoints.ServiceInfo{Default: true}
+ }
+
+ api, err := server.RegisterService(service, si.Name, si.Version,
+ si.Description, si.Default)
Vadim Sh. 2015/06/03 18:13:39 nit: imho line break here is unnecessary
+ if err != nil {
+ return err
+ }
+
+ for methodName, info := range mi {
+ method := api.MethodByName(methodName)
+ if method == nil {
+ return fmt.Errorf(
+ errPrefix+"no method %q (did you forget to export it?)", methodName)
+ }
+ curInfo := method.Info()
+ // These three are set automatically based on reflection, so only override
+ // them if the info object contains something new.
+ if info.Name == "" {
+ info.Name = curInfo.Name
+ }
+ if info.Path == "" {
+ info.Path = curInfo.Path
+ }
+ if info.HTTPMethod == "" {
+ info.HTTPMethod = curInfo.HTTPMethod
+ }
+ *curInfo = *info
+ mi[methodName] = curInfo // So that we can observe the merged result
Vadim Sh. 2015/06/03 18:13:39 is it ok to modify map while iterating over it?
+ }
+
+ return nil
+}

Powered by Google App Engine
This is Rietveld 408576698