Object containing a map?

89 views Asked by At

I'm trying to get a list of all of our endpoints.

We use Goa.

I noticed that we add all of our endpoints to a service (goa.New("service_name")). I also realized that if I print service.Mux I can see all the endpoints. However, the endpoints look like they are in a Map, which is contained by an object. When printing service.Mux, I see memory addresses as well. How do I get just the endpoints alone?

fmt.Println("Service Mux: ", service.Mux)

&{0xc42092c640 map[OPTIONS/api/my/endpoint/:endpointID/relationships/links:0x77d370 ...]}
2

There are 2 answers

0
mkopriva On BEST ANSWER

You could use the reflect and unsafe packages to get to the underlying and unexported map value, which is defined here (https://github.com/goadesign/goa/blob/master/mux.go#L48).

Something like this:

rv := reflect.ValueOf(service.Mux).Elem()
rf := rv.FieldByName("handles")
rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()

handles := rf.Interface().(map[string]goa.MuxHandler)
for k, h := range handles {
    fmt.Println(k, h)
}

But do note, that with this approach your code depends on an implementation detail as opposed to a public API, and therefore you cannot rely on its stability.

2
BigBug On

Goa library currently does not support this. Mux is an interface that has very few options available. Among the options available there is not one for retrieving that map unfortunately :-(