halgo icon indicating copy to clipboard operation
halgo copied to clipboard

How would I generate the links after the resource has been created?

Open philipliou opened this issue 11 years ago • 4 comments

For example, I want to "generate" the HAL links after I retrieve the resource from my database and convert it to a golang struct. I tried something like:

type Student struct {
  halgo.Links
  Id
  Name
  School
}

Now, when I get the Student instance from my database, only the Id, Name, and School fields are populated. But now I want to generate the halgo.Links before I convert to JSON and send it back to client in the response. How would I do that?

I tried something like this:

//studentA is a Student
studentA.Links := Links{}.Self("/students/123")

But I'm getting these errors:

non-name student.Links on left side of :=
undefined: Links gom:  exit status 2

philipliou avatar May 07 '14 21:05 philipliou

Ah nevermind it was a newb Go mistake. Imported halgo and did it like this worked:

student.Links = halgo.Links{}.Self("/students/123")

philipliou avatar May 07 '14 21:05 philipliou

Actually, is there any way to do this:

studentA.Links = halgo.Links{}
studentA.Links.Self("/students/123")
studentA.Links.Link("school", "/schools/456")

It doesn't seem to generate any links. It only works if I chain at least one method after the initializer, halgo.Links{}, like

halgo.links{}.Link("school", "/school/456")

philipliou avatar May 07 '14 22:05 philipliou

Just a comment. If you perform a POST and a new resource is created, the POST response should include a Location header to the new resource. That is the standard way to pick up new URLs for new resources (HAL or otherwise). I've submitted a pull request (#6) that adds a new method, Location, that allows you to construct a new navigator instance from a http.Response that includes a Location header.

xogeny avatar Jan 11 '15 16:01 xogeny

@philipliou...since my pull request was accepted, should this be closed? Is it sufficient for you?

xogeny avatar Apr 08 '15 20:04 xogeny