-3

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 ...]}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
BigBug
  • 6,202
  • 23
  • 87
  • 138
  • 2
    It seems like an struct. Please provide [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve) example to produce a problem you are facing. – Himanshu Jun 20 '18 at 12:34
  • @Himanshu what other minimal, complete and verifiable information do I need to add? As mentioned service comes from a library called Goa: https://goa.design/implement/mux/ – BigBug Jun 20 '18 at 12:40
  • 1
    just post the code you have tried and proper output of log and the struct of `service.Mux` – Himanshu Jun 20 '18 at 12:41
  • @Himanshu i have printed how to create a new service, i have printed the println for printing the service and i have printed the output above.... not too sure what i am missing? – BigBug Jun 20 '18 at 12:58
  • Just look up the documentation of Mux and use whatever suitable fileds or methods or functions it offers. – Volker Jun 20 '18 at 14:17
  • @Volker i did, and it's not supported i guess :-( – BigBug Jun 20 '18 at 14:52

2 Answers2

2

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.

mkopriva
  • 35,176
  • 4
  • 57
  • 71
0

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 :-(

BigBug
  • 6,202
  • 23
  • 87
  • 138
  • 1
    You can use the `reflect` package to get to the [map](https://github.com/goadesign/goa/blob/master/mux.go#L48). E.g. https://play.golang.org/p/KiLSqDDTWVA – mkopriva Jun 20 '18 at 15:20
  • @mkopriva can you make this an answer so i can select it? – BigBug Jun 20 '18 at 20:09