Re: [go-nuts] defining custom struct, confused

2018-09-26 Thread 'Mudasir Mirza' via golang-nuts
Hey,

Thanks allot, that fixed the issue and I really appreciate it.

On Wed, Sep 26, 2018 at 6:43 PM  wrote:

> Two things:
>
> 1) Capitalize users inside allUsers so it can be populated via reflection
> 2) Use an intermediary type to allow for that slice inside slice
>
> https://play.golang.org/p/RvW7PMSOZlq
>
> On Wednesday, 26 September 2018 10:10:41 UTC-4, Mudasir Mirza wrote:
>>
>> Also as per the API documentation of the vendor, I am suppose to get a
>> response in format
>>
>> ListUserResponse {
>> users (Array[V1User], optional),
>> _selfUrl (string, optional)
>> }
>>
>> On Wed, Sep 26, 2018 at 6:03 PM Mudasir Mirza 
>> wrote:
>>
>>> Hi Steven,
>>>
>>> Thanks for your prompt response. I know that, but I can not figure out
>>> how to define that in my struct :(
>>>
>>> On Wed, Sep 26, 2018 at 5:55 PM Steven Hartland 
>>> wrote:
>>>
 If you look closely they have a strange structure:
  "users": [*[*{...},{...}*]*]

 You've created:
 "users":[{...},{...}]

 On 26/09/2018 14:43, 'Mudasir Mirza' via golang-nuts wrote:

 Hey guys,

 I am fairly new to GoLang, still learning.

 I am trying to working on a small utility, which fetches details from a
 third party.
 As per their documentation, I will get response something like

 {
   "users": [
 [
   {
 "firstName": "string ",
 "lastName": "string",
 "username": "string",
 "email": "string",
 "createdAt": "string",
 "passwordLastUpdated": "string",
 "verified": true,
 "_selfUrl": "string"
   },
   {
 "firstName": "string ",
 "lastName": "string",
 "username": "string",
 "email": "string",
 "createdAt": "string",
 "passwordLastUpdated": "string",
 "verified": true,
 "_selfUrl": "string"
   }
 ]
   ]
 }

 For this, I created struct type

 type V1User struct {
  FirstName string  `json:"firstName,omitempty"`
  LastName  string  `json:"lastName,omitempty"`
  UserName  string  `json:"username,omitempty"`
  Email string  `json:"email,omitempty"`
  CreatedAt string  `json:"createdAt,omitempty"`
  PasswordLastUpdated   string  `json:"passwordLastUpdated,omitempty"`
  Verified  bool`json:"verified,omitempty"`
  SelfUrl   string  `json:"_selfUrl,omitempty"`
 }


 type allUsers struct {
  users   []V1User `json:"users,omitempty"`
 }

 I am pretty sure I am missing out on something. I am running below code
 to fetch and decode the data

 func (c *Client) getUsers() (users []allUsers, err error) {
  resp, err := c.request(http.MethodGet, "user", nil)
  if err != nil {
  return
  }
  defer resp.Body.Close()
  var result = struct {
  Users []allUsers
  }{}
  err = json.NewDecoder(resp.Body).Decode()
  return result.Users, err
 }

 and I get error

 fmt.Println(result.Users)  ->  Users:  [{[]}]
 fmt.Println(err)   ->   Error:  json: cannot unmarshal array into Go
 struct field .Users of type main.allUsers

 I am still trying to figure out what's wrong here, any help will be
 appreciated.

 --
 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...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.


 --
 You received this message because you are subscribed to a topic in the
 Google Groups "golang-nuts" group.
 To unsubscribe from this topic, visit
 https://groups.google.com/d/topic/golang-nuts/QAju97M7JHY/unsubscribe.
 To unsubscribe from this group and all its topics, send an email to
 golang-nuts...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.

>>>
>>>
>>> --
>>> Mudasir Mirza
>>> DevOps Manager
>>> Dubizzle
>>>
>>>
>>
>> --
>> Mudasir Mirza
>> DevOps Manager
>> Dubizzle
>>
>> --
> You received this message because you are subscribed to a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/QAju97M7JHY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


-- 
Mudasir Mirza
DevOps Manager
Dubizzle

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

Re: [go-nuts] defining custom struct, confused

2018-09-26 Thread silviucapota
Two things:

1) Capitalize users inside allUsers so it can be populated via reflection
2) Use an intermediary type to allow for that slice inside slice

https://play.golang.org/p/RvW7PMSOZlq

On Wednesday, 26 September 2018 10:10:41 UTC-4, Mudasir Mirza wrote:
>
> Also as per the API documentation of the vendor, I am suppose to get a 
> response in format
>
> ListUserResponse {
> users (Array[V1User], optional),
> _selfUrl (string, optional)
> }
>
> On Wed, Sep 26, 2018 at 6:03 PM Mudasir Mirza  > wrote:
>
>> Hi Steven,
>>
>> Thanks for your prompt response. I know that, but I can not figure out 
>> how to define that in my struct :(
>>
>> On Wed, Sep 26, 2018 at 5:55 PM Steven Hartland > > wrote:
>>
>>> If you look closely they have a strange structure:
>>>  "users": [*[*{...},{...}*]*]
>>>
>>> You've created:
>>> "users":[{...},{...}]
>>>
>>> On 26/09/2018 14:43, 'Mudasir Mirza' via golang-nuts wrote:
>>>
>>> Hey guys, 
>>>
>>> I am fairly new to GoLang, still learning.
>>>
>>> I am trying to working on a small utility, which fetches details from a 
>>> third party.
>>> As per their documentation, I will get response something like
>>>
>>> {
>>>   "users": [
>>> [
>>>   {
>>> "firstName": "string ",
>>> "lastName": "string",
>>> "username": "string",
>>> "email": "string",
>>> "createdAt": "string",
>>> "passwordLastUpdated": "string",
>>> "verified": true,
>>> "_selfUrl": "string"
>>>   },
>>>   {
>>> "firstName": "string ",
>>> "lastName": "string",
>>> "username": "string",
>>> "email": "string",
>>> "createdAt": "string",
>>> "passwordLastUpdated": "string",
>>> "verified": true,
>>> "_selfUrl": "string"
>>>   }
>>> ]
>>>   ]
>>> }
>>>
>>> For this, I created struct type
>>>
>>> type V1User struct {
>>>  FirstName string  `json:"firstName,omitempty"`
>>>  LastName  string  `json:"lastName,omitempty"`
>>>  UserName  string  `json:"username,omitempty"`
>>>  Email string  `json:"email,omitempty"`
>>>  CreatedAt string  `json:"createdAt,omitempty"`
>>>  PasswordLastUpdated   string  `json:"passwordLastUpdated,omitempty"`
>>>  Verified  bool`json:"verified,omitempty"`
>>>  SelfUrl   string  `json:"_selfUrl,omitempty"`
>>> }
>>>
>>>
>>> type allUsers struct {
>>>  users   []V1User `json:"users,omitempty"`
>>> }
>>>
>>> I am pretty sure I am missing out on something. I am running below code 
>>> to fetch and decode the data
>>>
>>> func (c *Client) getUsers() (users []allUsers, err error) {
>>>  resp, err := c.request(http.MethodGet, "user", nil)
>>>  if err != nil {
>>>  return
>>>  }
>>>  defer resp.Body.Close()
>>>  var result = struct {
>>>  Users []allUsers
>>>  }{}
>>>  err = json.NewDecoder(resp.Body).Decode()
>>>  return result.Users, err
>>> }
>>>
>>> and I get error 
>>>
>>> fmt.Println(result.Users)  ->  Users:  [{[]}]
>>> fmt.Println(err)   ->   Error:  json: cannot unmarshal array into Go 
>>> struct field .Users of type main.allUsers
>>>
>>> I am still trying to figure out what's wrong here, any help will be 
>>> appreciated.
>>>
>>> -- 
>>> 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...@googlegroups.com .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>>
>>> -- 
>>> You received this message because you are subscribed to a topic in the 
>>> Google Groups "golang-nuts" group.
>>> To unsubscribe from this topic, visit 
>>> https://groups.google.com/d/topic/golang-nuts/QAju97M7JHY/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to 
>>> golang-nuts...@googlegroups.com .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>> -- 
>> Mudasir Mirza
>> DevOps Manager
>> Dubizzle
>>
>>
>
> -- 
> Mudasir Mirza
> DevOps Manager
> Dubizzle
>
>

-- 
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.


Re: [go-nuts] defining custom struct, confused

2018-09-26 Thread 'Mudasir Mirza' via golang-nuts
Also as per the API documentation of the vendor, I am suppose to get a
response in format

ListUserResponse {
users (Array[V1User], optional),
_selfUrl (string, optional)
}

On Wed, Sep 26, 2018 at 6:03 PM Mudasir Mirza 
wrote:

> Hi Steven,
>
> Thanks for your prompt response. I know that, but I can not figure out how
> to define that in my struct :(
>
> On Wed, Sep 26, 2018 at 5:55 PM Steven Hartland 
> wrote:
>
>> If you look closely they have a strange structure:
>>  "users": [*[*{...},{...}*]*]
>>
>> You've created:
>> "users":[{...},{...}]
>>
>> On 26/09/2018 14:43, 'Mudasir Mirza' via golang-nuts wrote:
>>
>> Hey guys,
>>
>> I am fairly new to GoLang, still learning.
>>
>> I am trying to working on a small utility, which fetches details from a
>> third party.
>> As per their documentation, I will get response something like
>>
>> {
>>   "users": [
>> [
>>   {
>> "firstName": "string ",
>> "lastName": "string",
>> "username": "string",
>> "email": "string",
>> "createdAt": "string",
>> "passwordLastUpdated": "string",
>> "verified": true,
>> "_selfUrl": "string"
>>   },
>>   {
>> "firstName": "string ",
>> "lastName": "string",
>> "username": "string",
>> "email": "string",
>> "createdAt": "string",
>> "passwordLastUpdated": "string",
>> "verified": true,
>> "_selfUrl": "string"
>>   }
>> ]
>>   ]
>> }
>>
>> For this, I created struct type
>>
>> type V1User struct {
>>  FirstName string  `json:"firstName,omitempty"`
>>  LastName  string  `json:"lastName,omitempty"`
>>  UserName  string  `json:"username,omitempty"`
>>  Email string  `json:"email,omitempty"`
>>  CreatedAt string  `json:"createdAt,omitempty"`
>>  PasswordLastUpdated   string  `json:"passwordLastUpdated,omitempty"`
>>  Verified  bool`json:"verified,omitempty"`
>>  SelfUrl   string  `json:"_selfUrl,omitempty"`
>> }
>>
>>
>> type allUsers struct {
>>  users   []V1User `json:"users,omitempty"`
>> }
>>
>> I am pretty sure I am missing out on something. I am running below code
>> to fetch and decode the data
>>
>> func (c *Client) getUsers() (users []allUsers, err error) {
>>  resp, err := c.request(http.MethodGet, "user", nil)
>>  if err != nil {
>>  return
>>  }
>>  defer resp.Body.Close()
>>  var result = struct {
>>  Users []allUsers
>>  }{}
>>  err = json.NewDecoder(resp.Body).Decode()
>>  return result.Users, err
>> }
>>
>> and I get error
>>
>> fmt.Println(result.Users)  ->  Users:  [{[]}]
>> fmt.Println(err)   ->   Error:  json: cannot unmarshal array into Go
>> struct field .Users of type main.allUsers
>>
>> I am still trying to figure out what's wrong here, any help will be
>> appreciated.
>>
>> --
>> 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 a topic in the
>> Google Groups "golang-nuts" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/golang-nuts/QAju97M7JHY/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> golang-nuts+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> --
> Mudasir Mirza
> DevOps Manager
> Dubizzle
>
>

-- 
Mudasir Mirza
DevOps Manager
Dubizzle

-- 
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.


Re: [go-nuts] defining custom struct, confused

2018-09-26 Thread 'Mudasir Mirza' via golang-nuts
Hi Steven,

Thanks for your prompt response. I know that, but I can not figure out how
to define that in my struct :(

On Wed, Sep 26, 2018 at 5:55 PM Steven Hartland 
wrote:

> If you look closely they have a strange structure:
>  "users": [*[*{...},{...}*]*]
>
> You've created:
> "users":[{...},{...}]
>
> On 26/09/2018 14:43, 'Mudasir Mirza' via golang-nuts wrote:
>
> Hey guys,
>
> I am fairly new to GoLang, still learning.
>
> I am trying to working on a small utility, which fetches details from a
> third party.
> As per their documentation, I will get response something like
>
> {
>   "users": [
> [
>   {
> "firstName": "string ",
> "lastName": "string",
> "username": "string",
> "email": "string",
> "createdAt": "string",
> "passwordLastUpdated": "string",
> "verified": true,
> "_selfUrl": "string"
>   },
>   {
> "firstName": "string ",
> "lastName": "string",
> "username": "string",
> "email": "string",
> "createdAt": "string",
> "passwordLastUpdated": "string",
> "verified": true,
> "_selfUrl": "string"
>   }
> ]
>   ]
> }
>
> For this, I created struct type
>
> type V1User struct {
>  FirstName string  `json:"firstName,omitempty"`
>  LastName  string  `json:"lastName,omitempty"`
>  UserName  string  `json:"username,omitempty"`
>  Email string  `json:"email,omitempty"`
>  CreatedAt string  `json:"createdAt,omitempty"`
>  PasswordLastUpdated   string  `json:"passwordLastUpdated,omitempty"`
>  Verified  bool`json:"verified,omitempty"`
>  SelfUrl   string  `json:"_selfUrl,omitempty"`
> }
>
>
> type allUsers struct {
>  users   []V1User `json:"users,omitempty"`
> }
>
> I am pretty sure I am missing out on something. I am running below code to
> fetch and decode the data
>
> func (c *Client) getUsers() (users []allUsers, err error) {
>  resp, err := c.request(http.MethodGet, "user", nil)
>  if err != nil {
>  return
>  }
>  defer resp.Body.Close()
>  var result = struct {
>  Users []allUsers
>  }{}
>  err = json.NewDecoder(resp.Body).Decode()
>  return result.Users, err
> }
>
> and I get error
>
> fmt.Println(result.Users)  ->  Users:  [{[]}]
> fmt.Println(err)   ->   Error:  json: cannot unmarshal array into Go
> struct field .Users of type main.allUsers
>
> I am still trying to figure out what's wrong here, any help will be
> appreciated.
>
> --
> 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 a topic in the
> Google Groups "golang-nuts" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/golang-nuts/QAju97M7JHY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>


-- 
Mudasir Mirza
DevOps Manager
Dubizzle

-- 
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.


Re: [go-nuts] defining custom struct, confused

2018-09-26 Thread Steven Hartland

If you look closely they have a strange structure:
 "users": [*[*{...},{...}*]*]

You've created:
"users":[{...},{...}]

On 26/09/2018 14:43, 'Mudasir Mirza' via golang-nuts wrote:

Hey guys,

I am fairly new to GoLang, still learning.

I am trying to working on a small utility, which fetches details from 
a third party.

As per their documentation, I will get response something like

|
{
"users":[
[
{
"firstName":"string ",
"lastName":"string",
"username":"string",
"email":"string",
"createdAt":"string",
"passwordLastUpdated":"string",
"verified":true,
"_selfUrl":"string"
},
{
"firstName":"string ",
"lastName":"string",
"username":"string",
"email":"string",
"createdAt":"string",
"passwordLastUpdated":"string",
"verified":true,
"_selfUrl":"string"
}
]
]
}
|

For this, I created struct type

|
type V1User struct{
FirstNamestring`json:"firstName,omitempty"`
LastNamestring`json:"lastName,omitempty"`
UserNamestring`json:"username,omitempty"`
Emailstring`json:"email,omitempty"`
CreatedAtstring`json:"createdAt,omitempty"`
PasswordLastUpdatedstring`json:"passwordLastUpdated,omitempty"`
Verifiedbool`json:"verified,omitempty"`
SelfUrlstring`json:"_selfUrl,omitempty"`
}


type allUsers struct{
 users []V1User `json:"users,omitempty"`
}
|

I am pretty sure I am missing out on something. I am running below 
code to fetch and decode the data


|
func (c *Client)getUsers()(users []allUsers,err error){
 resp,err :=c.request(http.MethodGet,"user",nil)
iferr !=nil{
return
}
 defer resp.Body.Close()
varresult =struct{
Users[]allUsers
}{}
 err =json.NewDecoder(resp.Body).Decode()
returnresult.Users,err
}
|

and I get error

|
fmt.Println(result.Users)->Users:[{[]}]
fmt.Println(err)->Error: json:cannot unmarshal array intoGostructfield 
.Usersof type main.allUsers

|

I am still trying to figure out what's wrong here, any help will be 
appreciated.


--
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.