jagregory / halgo

HAL implementation in Go
MIT License
33 stars 9 forks source link

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

Open philipliou opened 10 years ago

philipliou commented 10 years ago

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 commented 10 years ago

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

student.Links = halgo.Links{}.Self("/students/123")
philipliou commented 10 years ago

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")
xogeny commented 9 years ago

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 commented 9 years ago

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