0

I am using the labstack/echo webserver and gofight for unit testing. In learning go would like to know if there is a go idiom for accessing state outside of the (embedded) echo structure. For example:

type WebPlusDB struct {
    web *echo.Echo
    db  *databaseInterface
}

func NewEngine() *echo.Echo {
    e := echo.New()
    e.GET("/hello", route_hello)
    return e    
}

func NewWebPlusDb() {
    e := NewEngine()
    db := database.New()   
    return WebPlusDB{e,db}
}

// for use in unit tests
func NewFakeEngine() *WebPlusDB {
    e := NewEngine()
    db := fakeDatabase.New()   
    return WebPlusDB{e,db}
}    

func route_hello(c echo.Context) error {
    log.Printf("Hello world\n")

    // how do I access WebPlusDB.db from here?

    return c.String(http.StatusOK, "hello world")
}

Then in the test code I use:

import (
    "github.com/labstack/echo"
    "github.com/appleboy/gofight"
    "github.com/stretchr/testify/assert"
    "testing"
)

func TestHelloWorld(t *testing.T) {
    r := gofight.New()

    r.GET("/hello").
          Run(NewFakeEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
        assert.Equal(t, http.StatusOK, r.Code)
        assert.Equal(t, "hello world", r.Body.String())
        // test database access
    })

}

The easiest solution is to have to use a global variable instead of embedding echo in "WebPlusDB" and adding the state there. I would like better encapsulation. I think I should be using something like the WebPlusDB structure not an echo.Echo plus global state. This perhaps doesn't matter so much for unit testing but in the greater scheme of doing things right in go (in this case avoiding globals) I would like to know.

Is there a solution or is this a weakness in the design of echo? It has extension points for middleware but a database backend is not really middleware as defined here.

Note: I am using a database here to illustrate the common case but It could be anything (I am actually using amqp)

It looks like you can extend the context interface but where is it created? This looks like it uses a kind of downcast:

e.GET("/", func(c echo.Context) error {
    cc := c.(*CustomContext)
}

I think (perhaps incorrectly) that this is only allowed on interface and echo.Context.Echo() returns a type not an interface.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Bruce Adams
  • 4,953
  • 4
  • 48
  • 111
  • 1
    Go doesn't have global variables, so there's nothing to avoid. – Jonathan Hall Jun 13 '19 at 15:21
  • 1
    Using a method instead of a function. Something like this: https://play.golang.com/p/1tv_Kn2BYmA – mkopriva Jun 13 '19 at 15:27
  • @Flimzy I think you may be arguing semantics. The scope may be limited to a package or module rather than being truly global. The go documentation refers to global variables here however: https://www.tutorialspoint.com/go/go_scope_rules.htm – Bruce Adams Jun 13 '19 at 15:29
  • @Flimzy exported package variables *are* global variables by another name. Making this comment every time someone mentions global variables isn't helpful. – Adrian Jun 13 '19 at 15:29
  • 1
    @BruceAdams although I agree with your semantics argument, that's not the official documentation however. See "global" in [here](https://golang.org/ref/spec)? The again, the word "global" is used [here](https://golang.org/doc/effective_go.html). – mkopriva Jun 13 '19 at 15:30
  • 1
    ["In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed."](https://en.wikipedia.org/wiki/Global_variable). That definition seems sound to me, and would include exported package variables. – Adrian Jun 13 '19 at 15:32

1 Answers1

6

You can pass a method of an instance as a function value, which is probably the most straightforward way to handle this:

type WebPlusDB struct {
    web *echo.Echo
    db  *databaseInterface
}

func (w WebPlusDB) route_hello(c echo.Context) error {
    log.Printf("Hello world\n")

    // do whatever with w

    return c.String(http.StatusOK, "hello world")
}

func NewEngine() *echo.Echo {
    e := echo.New()
    w := NewWebPlusDb()
    e.GET("/hello", w.route_hello)
    return e    
}
Adrian
  • 42,911
  • 6
  • 107
  • 99