> On 31 May 2021, at 6:08 pm, [email protected] <[email protected]> 
> wrote:
> 
> ok, I think I understand how this library works, and posting this for future 
> reference in case someone needs it.
> 
> The library uses this other library: 
> https://github.com/mitchellh/mapstructure 
> <https://github.com/mitchellh/mapstructure>
> 
> So now I can do this and vim go will helpfully fill in the struct fields for 
> me.
> 
> client := foreman.Client("foreman.l.example.org 
> <http://foreman.l.example.org/>", "admin", "whatever", false, "")
> 
> resp, err := client.Get("hosts")
> if err != nil {
>     fmt.Println(err)
>  }
> 
> var host foreman.Host
> err = mapstructure.Decode(resp, &host)
> if err != nil {
>     fmt.Println(err)
>  }
>  fmt.Println(host.Capabilities...)
>                       Capabilities                      m []interface{}
>  }                    Architecture_id                   m int
> ~                        Build                             m bool
> ~                        Comment                           m string
> ~                        Compute_attributes                m 
> foreman.Compute_attributes
> ~                        Compute_profile_id                m int
> ~                        Compute_resource_id               m int
> ~                        Domain_id                         m int
> ~                        Enabled                           m bool
> ~                        Environment_id                    m int
> ~                        Host_parameters_attributes_map    m 
> map[string]foreman.Params_archetype
> ~                        Hostgroup_id                      m int
> ~                        Image_id                          m int
> ~                        Interfaces_attributes_array       m 
> []foreman.Interfaces_attributes
> ~                        Ip                                m string
> ~                        Location_id                       m int
> ~                        Mac                               m string
> ~                        Managed                           m bool
> ~                        Medium_id                         m int
> ~                        Model_id                          m int
> ~                        Name                              m string
> ~                        Operatingsystem_id                m int
> ~                        Organization_id                   m int
> ~                        Owner_id                          m int
> ~                        Owner_type                        m string
> ~                        Progress_report_id                m string
> ~                        Provision_method                  m string
> ~                        Ptable_id                         m int
> ~                        Puppet_ca_proxy_id                m int
> ~                        Puppet_proxy_id                   m int
> ~                        Puppetclass_ids                   m []int
> ~                        Realm_id                          m int
> ~                        Root_pass                         m string
> ~                        Subnet_id                         m int
> ~                        Compute_attributes.Corespersocket m string
> ~                        Compute_attributes.Cpus           m string
> ~                        Compute_attributes.Start          m string
> 
> Thanks for your pointer.

In case you think this is a bit overkill for you and you may choose to do 
something “simpler", you can call the foreman API yourself and then you could 
use something like https://mholt.github.io/json-to-go/ 
<https://mholt.github.io/json-to-go/> to parse the JSON yourself in your 
program.

> 
> Regards,
> Natxo
> On Monday, May 31, 2021 at 6:45:36 AM UTC+2 [email protected] 
> <http://gmail.com/> wrote:
> hi,
> 
> I got privately an answer to get the type of variable, so I tried this:
> 
> client := foreman.Client("foreman.l.example.org 
> <http://foreman.l.example.org/>", "admin", "whatever", false, "")
>       resp, err := client.Get("hosts")
>       if err != nil {
>           fmt.Println(err)
>       }
>   
>       fmt.Printf("%T\n", resp)
> 
> and the result is: 
> map[string]interface {}
> 
> so this is my problem, I don't know what for type in resp is :?.
> 
> I'll take a look a the external library..
> 
> Thanks!
> On Monday, May 31, 2021 at 12:47:36 AM UTC+2 [email protected] 
> <applewebdata://340FEBF0-098B-4303-8D1B-48295F0B0D40> wrote:
> hi,
> 
> I am struggling to understand how to use this map[string]interface{}.
> 
> snippet:
> 
> package main
> 
> import "fmt"
> import "github.com/mattwilmott/go-foreman 
> <http://github.com/mattwilmott/go-foreman>"
> 
> func main() {
> 
>     client := foreman.Client("foreman.l.example.org 
> <http://foreman.l.example.org/>", "admin", "whatever", false, "")
>     fmt.Println(client)
>     resp, err := client.Get("hosts")
>     if err != nil {
>         fmt.Println(err)
>     }
> 
>     fmt.Println(resp)
> 
> }
> Foreman GET: map[page:1 per_page:20 results:[map[architecture_id:1 
> architecture_name:x86_64 build:false capabilities:[build] 
> certname:foreman.l.example.org <http://foreman.l.example.org/> comment:<nil> 
> compute_profile_id:<nil> compute_profile_name:<nil> compute_resource_id:<nil> 
> compute_resource_name:<nil> configuration_status:0 
> configuration_status_label:Out of sync created_at:2021-05-30 20:54:20 UTC 
> disk:<nil> domain_id:1 domain_name:l.example.org <http://l.example.org/> 
> enabled:true environment_id:1 environment_name:production global_status:0 
> global_status_label:Warning hostgroup_id:<nil> hostgroup_name:<nil> 
> hostgroup_title:<nil> id:1 image_file: image_id:<nil> image_name:<nil> 
> installed_at:<nil> ip:<nil> ip6:2001:470:7b79:100::44e last_compile:<nil> 
> last_report:2021-05-30 20:54:15 UTC location_id:2 location_name:Default 
> Location mac:ba:a6:69:6e:31:6c managed:false medium_id:<nil> 
> medium_name:<nil> model_id:1 model_name:Latitude E7250 
> name:foreman.l.example.org <http://foreman.l.example.org/> 
> operatingsystem_id:1 operatingsystem_name:CentOS 7.9.2009 organization_id:1 
> organization_name:Default Organization owner_id:<nil> owner_name:<nil> 
> owner_type:<nil> provision_method:build ptable_id:<nil> ptable_name:<nil> 
> puppet_ca_proxy:map[id:1 name:foreman.l.example.org 
> <http://foreman.l.example.org/> url:https://foreman.l.example.org:8443 
> <https://foreman.l.example.org:8443/>] puppet_ca_proxy_id:1 
> puppet_ca_proxy_name:foreman.l.example.org <http://foreman.l.example.org/> 
> puppet_proxy:map[id:1 name:foreman.l.example.org 
> <http://foreman.l.example.org/>url:https://foreman.l.example.org:8443 
> <https://foreman.l.example.org:8443/>] puppet_proxy_id:1 
> puppet_proxy_name:foreman.l.example.org 
> <http://foreman.l.example.org/>puppet_status:0 pxe_loader:<nil> 
> realm_id:<nil> realm_name:<nil> sp_ip:<nil> sp_mac:<nil> sp_name:<nil> 
> sp_subnet_id:<nil> subnet6_id:<nil> subnet6_name:<nil> subnet_id:<nil> 
> subnet_name:<nil> updated_at:2021-05-30 20:54:23 UTC uptime_seconds:11692 
> use_image:<nil> uuid:<nil>]] search:<nil> sort:map[by:<nil> order:<nil>] 
> subtotal:1 total:1]
> map[page:1 per_page:20 results:[map[architecture_id:1 
> architecture_name:x86_64 build:false capabilities:[build] 
> certname:foreman.l.example.org <http://foreman.l.example.org/> comment:<nil> 
> compute_profile_id:<nil> compute_profile_name:<nil> compute_resource_id:<nil> 
> compute_resource_name:<nil> configuration_status:0 
> configuration_status_label:Out of sync created_at:2021-05-30 20:54:20 UTC 
> disk:<nil> domain_id:1 domain_name:l.example.org <http://l.example.org/> 
> enabled:true environment_id:1 environment_name:production global_status:0 
> global_status_label:Warning hostgroup_id:<nil> hostgroup_name:<nil> 
> hostgroup_title:<nil> id:1 image_file: image_id:<nil> image_name:<nil> 
> installed_at:<nil> ip:<nil> ip6:2001:470:7b79:100::44e last_compile:<nil> 
> last_report:2021-05-30 20:54:15 UTC location_id:2 location_name:Default 
> Location mac:ba:a6:69:6e:31:6c managed:false medium_id:<nil> 
> medium_name:<nil> model_id:1 model_name:Latitude E7250 
> name:foreman.l.example.org <http://foreman.l.example.org/> 
> operatingsystem_id:1 operatingsystem_name:CentOS 7.9.2009 organization_id:1 
> organization_name:Default Organization owner_id:<nil> owner_name:<nil> 
> owner_type:<nil> provision_method:build ptable_id:<nil> ptable_name:<nil> 
> puppet_ca_proxy:map[id:1 name:foreman.l.example.org 
> <http://foreman.l.example.org/> url:https://foreman.l.example.org:8443 
> <https://foreman.l.example.org:8443/>] puppet_ca_proxy_id:1 
> puppet_ca_proxy_name:foreman.l.example.org <http://foreman.l.example.org/> 
> puppet_proxy:map[id:1 name:foreman.l.example.org 
> <http://foreman.l.example.org/>url:https://foreman.l.example.org:8443 
> <https://foreman.l.example.org:8443/>] puppet_proxy_id:1 
> puppet_proxy_name:foreman.l.example.org 
> <http://foreman.l.example.org/>puppet_status:0 pxe_loader:<nil> 
> realm_id:<nil> realm_name:<nil> sp_ip:<nil> sp_mac:<nil> sp_name:<nil> 
> sp_subnet_id:<nil> subnet6_id:<nil> subnet6_name:<nil> subnet_id:<nil> 
> subnet_name:<nil> updated_at:2021-05-30 20:54:23 UTC uptime_seconds:11692 
> use_image:<nil> uuid:<nil>]] search:<nil> sort:map[by:<nil> order:<nil>] 
> subtotal:1 total:1]
> 
> 
> 
> I understand this map[string]interface{} allows me to not have to define 
> every json struct beforehand, but how can I parse it?
> 
> Thanks in advance.
> Regards,
> Natxo
> 
> -- 
> 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 [email protected] 
> <mailto:[email protected]>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/59b83f8e-555e-4924-a3f2-84105083253dn%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/59b83f8e-555e-4924-a3f2-84105083253dn%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/366043C6-4A58-4EBE-B8CE-144B561CA870%40gmail.com.

Reply via email to