Closed unmarshall closed 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.
My general recommendation:
Data
whose type is any
)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",
// },
// },
//...
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).
It would be nice if you could give idiomatic ways to consume the response using
Go
as this repository is specifically forGo
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.
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.
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!
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.
I close this issue as you figured out the right solution. Thanks again for reporting this.
Bug Report
ClientResourcesResponse.Data
but if i now want to iterate over rows/JsonObjectArray then i am currently not able to find a way or an example to do just that. The data type ofData
isany
.I have checked the documentation links and each and every example is incomplete. Example (the list is not comprehensive):
Data
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:But since type of
Data
isany
it becomes difficult to consume it.What did you expect or want to happen? At least give one complete example on how to iterate either over table rows (if ResultFormat is set to
ResultFormatTable
) or consumeResultFormatObjectArray
.How can we reproduce it? Just use the resource graph query to return a result which has more than 1 rows.
Anything we should know about your environment.