Passionate Engineer
Passionate Engineer

Reputation: 10422

How to manipulate decoded XML with Struct passed in as interface parameter

Is there a way to manipulate decoded XML with struct passed in as interface parameter?

I'm passing struct as a parameter which has interface type but after decoding it with xml.Decode I cannot specify its fields to retrieve its fields as structs. I think go is complaining that it doesn't know which struct to look for unless it's specifically mentioned.

Below is an example function:

func UpdateEntity(response *restful.Response, xml_template string, dataStruct interface{}, respStruct interface{}) (*restful.Response, interface{}) {
    payload := renderCachedTemplate(response, xml_template, dataStruct)
    resp, err := http.Post(url, "application/xml", payload)
    if err != nil {
        response.WriteErrorString(http.StatusInternalServerError, err.Error())
        return nil, nil
    }

    defer resp.Body.Close()
    dec := xml.NewDecoder(resp.Body)

    if err = dec.Decode(respStruct); err != nil {
        fmt.Printf("error: %v", err)
        return nil, nil
    }
    return response, respStruct
}

func main() {
   resp, respStruct := UpdateEntity(response, "template.xml", LoginStruct{}, UserStruct{})
   fmt.Println(respStruct.Username)
}

This line fmt.Println(respStruct.Username) complains Username is not defined as interface{} has no field or method Username.

I'm passing UserStruct as its respStruct parameter which has Username field for example.

Is there a way I can use interface{} return variable and use pre-defined Struct fields?

Upvotes: 1

Views: 85

Answers (1)

Thundercat
Thundercat

Reputation: 121209

Change the function to expect a pointer to a value instead of returning the value:

func UpdateEntity(response *restful.Response, xml_template string, dataStruct interface{}, respStruct interface{}) *restful.Response {
    payload := renderCachedTemplate(response, xml_template, dataStruct)
    resp, err := http.Post(url, "application/xml", payload)
    if err != nil {
        response.WriteErrorString(http.StatusInternalServerError, err.Error())
        return nil
    }

    defer resp.Body.Close()
    dec := xml.NewDecoder(resp.Body)

    if err = dec.Decode(respStruct); err != nil {
        fmt.Printf("error: %v", err)
        return nil
    }
    return response
}

Pass a pointer to your value to the function and then use the updated value after the function call:

func main() {
   var respStruct UserStruct
   resp := UpdateEntity(response, "template.xml", LoginStruct{}, &respStruct)
   fmt.Println(respStruct.Username)
}

Because the Decode method expects a pointer to a value, this change also fixes an error in the original code.

Upvotes: 2

Related Questions