Azure / azure-sdk-for-go

This repository is for active development of the Azure SDK for Go. For consumers of the SDK we recommend visiting our public developer docs at:
https://docs.microsoft.com/azure/developer/go/
MIT License
1.64k stars 844 forks source link

how does one consume armresourcegraph QueryResponse #21004

Closed unmarshall closed 1 year ago

unmarshall commented 1 year ago

Bug Report

I have checked the documentation links and each and every example is incomplete. Example (the list is not comprehensive):

All the examples fall short of how to extract values from Data. You cannot range over it because its not an iterable. You cannot cast it to a slice as that is not allowed and is also incorrect. I even tried to change the options via:

Options: &armresourcegraph.QueryRequestOptions{
    ResultFormat: to.Ptr(armresourcegraph.ResultFormatObjectArray),
},

But since type of Data is any it becomes difficult to consume it.

unmarshall commented 1 year ago

Ok finally figured out the way to do this: Using ObjectArray result format

Options: &armresourcegraph.QueryRequestOptions{
    ResultFormat: to.Ptr(armresourcegraph.ResultFormatObjectArray),
},

Code to parse:

if m, ok := resp.Data.([]interface{}); ok {
   for _, r := range m {
    items := r.(map[string]interface{})
    for k, v := range items {
        fmt.Printf("k: %s, v: %+v\n", k, v)
    }
  }
}

If you see the above usage of the response you will immediately see the following:

In general this is a design flaw where it expects each consumer to interpret any type for response.Data.

unmarshall commented 1 year ago

My general recommendation:

raych1 commented 1 year ago

Thanks @unmarshall for reporting this issue.

I want to mention that SDK code is generated automatically, and the examples are all generated from the REST api examples. I would ask if service team could consider adding more REST API examples.

Actually, the example you referenced here includes the response example and how does the data look like.

// If the HTTP response code is 200 as defined in example definition, your response structure would look as follows. Please pay attention that all the values in the output are fake values for just demo purposes.
    // res.QueryResponse = armresourcegraph.QueryResponse{
    //  Count: to.Ptr[int64](3),
    //  Data: []any{
    //      map[string]any{
    //          "name": "myNetworkInterface",
    //          "type": "microsoft.network/networkinterfaces",
    //          "id": "/subscriptions/cfbbd179-59d2-4052-aa06-9270a38aa9d6/resourceGroups/RG1/providers/Microsoft.Network/networkInterfaces/myNetworkInterface",
    //          "location": "centralus",
    //          "tags":map[string]any{
    //              "tag1": "Value1",
    //          },
    //      },
       //...
unmarshall commented 1 year ago

I would ask if service team could consider adding more REST API examples.

It would be nice if you could give idiomatic ways to consume the response using Go as this repository is specifically for Go based SDK. Also in comment i have pointed out some additional problems (lack of ease for consumers).

raych1 commented 1 year ago

It would be nice if you could give idiomatic ways to consume the response using Go as this repository is specifically for Go based SDK. Also in comment i have pointed out some additional problems (lack of ease for consumers).

@unmarshall , sdk code is generated automatically based on the swagger. Generated code has no idea of unmarshalling the any type. The design change request needs the service team's response.

github-actions[bot] commented 1 year ago

Hi @unmarshall. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

github-actions[bot] commented 1 year ago

Hi @unmarshall, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!

unmarshall commented 1 year ago

The design change request needs the service team's response.

I thought this was more a question/comment for the design team and only an information for me. The generated code in this case has created consumption difficult. API responses should be typed or sufficient metadata information should be provided allowing the consumers to do proper casts (If a Cast is required then its a sign of design smell anyways).

For now I have figured out a way to consume the response (even though it does not look pretty). I will leave it to your judgement on how you see your own response type. If you do not see any issue then feel free to close the ticket.

raych1 commented 1 year ago

I close this issue as you figured out the right solution. Thanks again for reporting this.