This is what I would do:

type jsonRepository struct {
  Name string
  URI string
}

func (r *Repository) UnmarshalJSON(in []byte) error {
    var m jsonRepository
    err:=json.Unmarshal(in,&m)
    if err!=nil {
    return err
  }
  parsed, err:=url.Parse(m.URI)
  if err!=nil {
    return err
  }
  *r=Repository{Name:m.Name,URI:parsed}
  return nil
}

On Mon, Sep 17, 2018 at 6:31 AM Mirko Friedenhagen <mfriedenha...@gmx.de> wrote:
>
> Hello,
>
> I like to work with stronger types so I try to avoid strings.
> Given I have some JSON like
>
> ---
> const _test_data = `
> {
>   "name" : "myname",
>   "uri" : "https://example.com/mycontext";
> }
> `
> ---
>
> What I really would like to have is something like:
>
> ---
>
> type Repository struct {
>    Name string
>    Uri  url.URL // replaced with MyURL
> }
>
> ---
>
> I ended up creating a custom type, however the conversion feels a little bit 
> clumsy, see: https://play.golang.org/p/3gs8UK26ZfV. And MyURL is now missing 
> all the nice methods of url.URL.
>
> Thanks for hints :-)
> Mirko
>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to