How can I alter struct XML tags in go?











up vote
0
down vote

favorite
1












I have 2 very similar pieces of XML retrieved from Amazon.



<?xml version="1.0"?><GetLowestPricedOffersForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestPricedOffersForASINResult MarketplaceID="A1F83G8C2ARO7P" ItemCondition="New" ASIN="0195019199" status="Success">
<Identifier>
<MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
<ASIN>0195019199</ASIN>
<ItemCondition>New</ItemCondition>
<TimeOfOfferChange>2018-11-07T02:05:14.342Z</TimeOfOfferChange>
</Identifier>
<Summary>
<TotalOfferCount>45</TotalOfferCount>
<NumberOfOffers>
<OfferCount condition="used" fulfillmentChannel="Merchant">14</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Amazon">1</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Merchant">30</OfferCount>
</NumberOfOff........ etc xml continues

</GetLowestPricedOffersForASINResult>
<ResponseMetadata>
<RequestId>fef8c86d-c563-4373-81c9-78dcf691283c</RequestId>
</ResponseMetadata>
</GetLowestPricedOffersForASINResponse>


I currently unmarshal this using custom types and custom unmarshal into a struct which looks like this:



type LowestPricedPricedOffers struct {

Error AmazonError `xml:"Error"`
All struct {

/*The only way I found to retrieve 'status' from the GetLowestPricedOffersForASINResult element was to wrap in the struct 'All'.
This is the only reason the All struct exists. Ideally would like to remove*/
Status string `xml:"status,attr"`

ASIN string `xml:"Identifier>ASIN"`
ItemCondition string `xml:"Identifier>ItemCondition"`
TimeOfOfferChange string `xml:"Identifier>TimeOfOfferChange"`
TotalOfferCount int `xml:"Summary>TotalOfferCount"`
ListPrice float32 `xml:"Summary>ListPrice>Amount"`
OfferCount offerCount `xml:"Summary>NumberOfOffers"`

//Want to take Currency code from LowestPrices below but cannot think of a way to achieve this against the lowestPrices type
//CurrencyCode string `xml:"CurrencyCode"`

BuyBoxPrices buyBoxPrices `xml:"Summary>BuyBoxPrices"`
LowestPrices lowestPrices `xml:"Summary>LowestPrices"`
BuyBoxEligibleOffers buyBoxEligibleOffers `xml:"Summary>BuyBoxEligibleOffers"`

Offers struct {
SubCondition string `xml:"SubCondition"`
SellerPositiveFeedbackRating float32 `xml:"SellerFeedbackRating>SellerPositiveFeedbackRating"`
FeedbackCount int `xml:"SellerFeedbackRating>FeedbackCount"`
ShippingTime struct {
MinimumHours int `xml:"minimumHours,attr"`
MaximumHours int `xml:"maximumHours,attr"`
AvailabilityType string `xml:"availabilityType,attr"`
}
ListingPrice float32 `xml:"ListingPrice>Amount"`
Shipping float32 `xml:"Shipping>Amount"`
ShipsFrom string `xml:"ShipsFrom>Country"`
IsFulfilledByAmazon bool `xml:"IsFulfilledByAmazon"`
IsBuyBoxWinner bool `xml:"IsBuyBoxWinner"`
IsFeaturedMerchant bool `xml:"IsFeaturedMerchant"` //true if the seller of the item is eligible to win the Buy Box.
} `xml:"Offers>Offer"`
} `xml:"GetLowestPricedOffersForASINResult"`

}


I also have some more XML data whose structure is the same except the element 'GetLowestPricedOffersForASINResult' is called 'GetLowestPricedOffersForSKUResult'.



If I alter manually alter the tag xml:"GetLowestPricedOffersForASINResult" to xml:"GetLowestPricedOffersForSKUResult" then the code will happily process the other XML.



I would like to make this generic, if you like, my options seem to be:




  1. Copy the code and have 2 large blocks which are fundamentally the same. This would be easy, but seems to me to be the wrong approach.


  2. Bodge it by doing a search and replace on the XML raw string and simply replace GetLowestPricedOffersForSKUResult with GetLowestPricedOffersForASINResult, the code would then process the data happily, again this seems wrong.


  3. Alter the struct tag on the fly possibly using reflection. Is this possible?



Does anyone have any suggestions for accomplishing 3 or other ideas/approaches for dealing with this in go?



Code for the GetLowestPricedOffersForASINResult result set is on the go playground (not complete but works)










share|improve this question


















  • 2




    Declare a named type for the struct that you need in multiple places and reuse it. play.golang.org/p/JOzgpjqDwAn
    – mkopriva
    Nov 8 at 9:35












  • The above approach works, thanks to @mkopriva
    – Lee
    Nov 8 at 13:56















up vote
0
down vote

favorite
1












I have 2 very similar pieces of XML retrieved from Amazon.



<?xml version="1.0"?><GetLowestPricedOffersForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestPricedOffersForASINResult MarketplaceID="A1F83G8C2ARO7P" ItemCondition="New" ASIN="0195019199" status="Success">
<Identifier>
<MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
<ASIN>0195019199</ASIN>
<ItemCondition>New</ItemCondition>
<TimeOfOfferChange>2018-11-07T02:05:14.342Z</TimeOfOfferChange>
</Identifier>
<Summary>
<TotalOfferCount>45</TotalOfferCount>
<NumberOfOffers>
<OfferCount condition="used" fulfillmentChannel="Merchant">14</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Amazon">1</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Merchant">30</OfferCount>
</NumberOfOff........ etc xml continues

</GetLowestPricedOffersForASINResult>
<ResponseMetadata>
<RequestId>fef8c86d-c563-4373-81c9-78dcf691283c</RequestId>
</ResponseMetadata>
</GetLowestPricedOffersForASINResponse>


I currently unmarshal this using custom types and custom unmarshal into a struct which looks like this:



type LowestPricedPricedOffers struct {

Error AmazonError `xml:"Error"`
All struct {

/*The only way I found to retrieve 'status' from the GetLowestPricedOffersForASINResult element was to wrap in the struct 'All'.
This is the only reason the All struct exists. Ideally would like to remove*/
Status string `xml:"status,attr"`

ASIN string `xml:"Identifier>ASIN"`
ItemCondition string `xml:"Identifier>ItemCondition"`
TimeOfOfferChange string `xml:"Identifier>TimeOfOfferChange"`
TotalOfferCount int `xml:"Summary>TotalOfferCount"`
ListPrice float32 `xml:"Summary>ListPrice>Amount"`
OfferCount offerCount `xml:"Summary>NumberOfOffers"`

//Want to take Currency code from LowestPrices below but cannot think of a way to achieve this against the lowestPrices type
//CurrencyCode string `xml:"CurrencyCode"`

BuyBoxPrices buyBoxPrices `xml:"Summary>BuyBoxPrices"`
LowestPrices lowestPrices `xml:"Summary>LowestPrices"`
BuyBoxEligibleOffers buyBoxEligibleOffers `xml:"Summary>BuyBoxEligibleOffers"`

Offers struct {
SubCondition string `xml:"SubCondition"`
SellerPositiveFeedbackRating float32 `xml:"SellerFeedbackRating>SellerPositiveFeedbackRating"`
FeedbackCount int `xml:"SellerFeedbackRating>FeedbackCount"`
ShippingTime struct {
MinimumHours int `xml:"minimumHours,attr"`
MaximumHours int `xml:"maximumHours,attr"`
AvailabilityType string `xml:"availabilityType,attr"`
}
ListingPrice float32 `xml:"ListingPrice>Amount"`
Shipping float32 `xml:"Shipping>Amount"`
ShipsFrom string `xml:"ShipsFrom>Country"`
IsFulfilledByAmazon bool `xml:"IsFulfilledByAmazon"`
IsBuyBoxWinner bool `xml:"IsBuyBoxWinner"`
IsFeaturedMerchant bool `xml:"IsFeaturedMerchant"` //true if the seller of the item is eligible to win the Buy Box.
} `xml:"Offers>Offer"`
} `xml:"GetLowestPricedOffersForASINResult"`

}


I also have some more XML data whose structure is the same except the element 'GetLowestPricedOffersForASINResult' is called 'GetLowestPricedOffersForSKUResult'.



If I alter manually alter the tag xml:"GetLowestPricedOffersForASINResult" to xml:"GetLowestPricedOffersForSKUResult" then the code will happily process the other XML.



I would like to make this generic, if you like, my options seem to be:




  1. Copy the code and have 2 large blocks which are fundamentally the same. This would be easy, but seems to me to be the wrong approach.


  2. Bodge it by doing a search and replace on the XML raw string and simply replace GetLowestPricedOffersForSKUResult with GetLowestPricedOffersForASINResult, the code would then process the data happily, again this seems wrong.


  3. Alter the struct tag on the fly possibly using reflection. Is this possible?



Does anyone have any suggestions for accomplishing 3 or other ideas/approaches for dealing with this in go?



Code for the GetLowestPricedOffersForASINResult result set is on the go playground (not complete but works)










share|improve this question


















  • 2




    Declare a named type for the struct that you need in multiple places and reuse it. play.golang.org/p/JOzgpjqDwAn
    – mkopriva
    Nov 8 at 9:35












  • The above approach works, thanks to @mkopriva
    – Lee
    Nov 8 at 13:56













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have 2 very similar pieces of XML retrieved from Amazon.



<?xml version="1.0"?><GetLowestPricedOffersForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestPricedOffersForASINResult MarketplaceID="A1F83G8C2ARO7P" ItemCondition="New" ASIN="0195019199" status="Success">
<Identifier>
<MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
<ASIN>0195019199</ASIN>
<ItemCondition>New</ItemCondition>
<TimeOfOfferChange>2018-11-07T02:05:14.342Z</TimeOfOfferChange>
</Identifier>
<Summary>
<TotalOfferCount>45</TotalOfferCount>
<NumberOfOffers>
<OfferCount condition="used" fulfillmentChannel="Merchant">14</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Amazon">1</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Merchant">30</OfferCount>
</NumberOfOff........ etc xml continues

</GetLowestPricedOffersForASINResult>
<ResponseMetadata>
<RequestId>fef8c86d-c563-4373-81c9-78dcf691283c</RequestId>
</ResponseMetadata>
</GetLowestPricedOffersForASINResponse>


I currently unmarshal this using custom types and custom unmarshal into a struct which looks like this:



type LowestPricedPricedOffers struct {

Error AmazonError `xml:"Error"`
All struct {

/*The only way I found to retrieve 'status' from the GetLowestPricedOffersForASINResult element was to wrap in the struct 'All'.
This is the only reason the All struct exists. Ideally would like to remove*/
Status string `xml:"status,attr"`

ASIN string `xml:"Identifier>ASIN"`
ItemCondition string `xml:"Identifier>ItemCondition"`
TimeOfOfferChange string `xml:"Identifier>TimeOfOfferChange"`
TotalOfferCount int `xml:"Summary>TotalOfferCount"`
ListPrice float32 `xml:"Summary>ListPrice>Amount"`
OfferCount offerCount `xml:"Summary>NumberOfOffers"`

//Want to take Currency code from LowestPrices below but cannot think of a way to achieve this against the lowestPrices type
//CurrencyCode string `xml:"CurrencyCode"`

BuyBoxPrices buyBoxPrices `xml:"Summary>BuyBoxPrices"`
LowestPrices lowestPrices `xml:"Summary>LowestPrices"`
BuyBoxEligibleOffers buyBoxEligibleOffers `xml:"Summary>BuyBoxEligibleOffers"`

Offers struct {
SubCondition string `xml:"SubCondition"`
SellerPositiveFeedbackRating float32 `xml:"SellerFeedbackRating>SellerPositiveFeedbackRating"`
FeedbackCount int `xml:"SellerFeedbackRating>FeedbackCount"`
ShippingTime struct {
MinimumHours int `xml:"minimumHours,attr"`
MaximumHours int `xml:"maximumHours,attr"`
AvailabilityType string `xml:"availabilityType,attr"`
}
ListingPrice float32 `xml:"ListingPrice>Amount"`
Shipping float32 `xml:"Shipping>Amount"`
ShipsFrom string `xml:"ShipsFrom>Country"`
IsFulfilledByAmazon bool `xml:"IsFulfilledByAmazon"`
IsBuyBoxWinner bool `xml:"IsBuyBoxWinner"`
IsFeaturedMerchant bool `xml:"IsFeaturedMerchant"` //true if the seller of the item is eligible to win the Buy Box.
} `xml:"Offers>Offer"`
} `xml:"GetLowestPricedOffersForASINResult"`

}


I also have some more XML data whose structure is the same except the element 'GetLowestPricedOffersForASINResult' is called 'GetLowestPricedOffersForSKUResult'.



If I alter manually alter the tag xml:"GetLowestPricedOffersForASINResult" to xml:"GetLowestPricedOffersForSKUResult" then the code will happily process the other XML.



I would like to make this generic, if you like, my options seem to be:




  1. Copy the code and have 2 large blocks which are fundamentally the same. This would be easy, but seems to me to be the wrong approach.


  2. Bodge it by doing a search and replace on the XML raw string and simply replace GetLowestPricedOffersForSKUResult with GetLowestPricedOffersForASINResult, the code would then process the data happily, again this seems wrong.


  3. Alter the struct tag on the fly possibly using reflection. Is this possible?



Does anyone have any suggestions for accomplishing 3 or other ideas/approaches for dealing with this in go?



Code for the GetLowestPricedOffersForASINResult result set is on the go playground (not complete but works)










share|improve this question













I have 2 very similar pieces of XML retrieved from Amazon.



<?xml version="1.0"?><GetLowestPricedOffersForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestPricedOffersForASINResult MarketplaceID="A1F83G8C2ARO7P" ItemCondition="New" ASIN="0195019199" status="Success">
<Identifier>
<MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
<ASIN>0195019199</ASIN>
<ItemCondition>New</ItemCondition>
<TimeOfOfferChange>2018-11-07T02:05:14.342Z</TimeOfOfferChange>
</Identifier>
<Summary>
<TotalOfferCount>45</TotalOfferCount>
<NumberOfOffers>
<OfferCount condition="used" fulfillmentChannel="Merchant">14</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Amazon">1</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Merchant">30</OfferCount>
</NumberOfOff........ etc xml continues

</GetLowestPricedOffersForASINResult>
<ResponseMetadata>
<RequestId>fef8c86d-c563-4373-81c9-78dcf691283c</RequestId>
</ResponseMetadata>
</GetLowestPricedOffersForASINResponse>


I currently unmarshal this using custom types and custom unmarshal into a struct which looks like this:



type LowestPricedPricedOffers struct {

Error AmazonError `xml:"Error"`
All struct {

/*The only way I found to retrieve 'status' from the GetLowestPricedOffersForASINResult element was to wrap in the struct 'All'.
This is the only reason the All struct exists. Ideally would like to remove*/
Status string `xml:"status,attr"`

ASIN string `xml:"Identifier>ASIN"`
ItemCondition string `xml:"Identifier>ItemCondition"`
TimeOfOfferChange string `xml:"Identifier>TimeOfOfferChange"`
TotalOfferCount int `xml:"Summary>TotalOfferCount"`
ListPrice float32 `xml:"Summary>ListPrice>Amount"`
OfferCount offerCount `xml:"Summary>NumberOfOffers"`

//Want to take Currency code from LowestPrices below but cannot think of a way to achieve this against the lowestPrices type
//CurrencyCode string `xml:"CurrencyCode"`

BuyBoxPrices buyBoxPrices `xml:"Summary>BuyBoxPrices"`
LowestPrices lowestPrices `xml:"Summary>LowestPrices"`
BuyBoxEligibleOffers buyBoxEligibleOffers `xml:"Summary>BuyBoxEligibleOffers"`

Offers struct {
SubCondition string `xml:"SubCondition"`
SellerPositiveFeedbackRating float32 `xml:"SellerFeedbackRating>SellerPositiveFeedbackRating"`
FeedbackCount int `xml:"SellerFeedbackRating>FeedbackCount"`
ShippingTime struct {
MinimumHours int `xml:"minimumHours,attr"`
MaximumHours int `xml:"maximumHours,attr"`
AvailabilityType string `xml:"availabilityType,attr"`
}
ListingPrice float32 `xml:"ListingPrice>Amount"`
Shipping float32 `xml:"Shipping>Amount"`
ShipsFrom string `xml:"ShipsFrom>Country"`
IsFulfilledByAmazon bool `xml:"IsFulfilledByAmazon"`
IsBuyBoxWinner bool `xml:"IsBuyBoxWinner"`
IsFeaturedMerchant bool `xml:"IsFeaturedMerchant"` //true if the seller of the item is eligible to win the Buy Box.
} `xml:"Offers>Offer"`
} `xml:"GetLowestPricedOffersForASINResult"`

}


I also have some more XML data whose structure is the same except the element 'GetLowestPricedOffersForASINResult' is called 'GetLowestPricedOffersForSKUResult'.



If I alter manually alter the tag xml:"GetLowestPricedOffersForASINResult" to xml:"GetLowestPricedOffersForSKUResult" then the code will happily process the other XML.



I would like to make this generic, if you like, my options seem to be:




  1. Copy the code and have 2 large blocks which are fundamentally the same. This would be easy, but seems to me to be the wrong approach.


  2. Bodge it by doing a search and replace on the XML raw string and simply replace GetLowestPricedOffersForSKUResult with GetLowestPricedOffersForASINResult, the code would then process the data happily, again this seems wrong.


  3. Alter the struct tag on the fly possibly using reflection. Is this possible?



Does anyone have any suggestions for accomplishing 3 or other ideas/approaches for dealing with this in go?



Code for the GetLowestPricedOffersForASINResult result set is on the go playground (not complete but works)







xml go






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 9:24









Lee

145




145








  • 2




    Declare a named type for the struct that you need in multiple places and reuse it. play.golang.org/p/JOzgpjqDwAn
    – mkopriva
    Nov 8 at 9:35












  • The above approach works, thanks to @mkopriva
    – Lee
    Nov 8 at 13:56














  • 2




    Declare a named type for the struct that you need in multiple places and reuse it. play.golang.org/p/JOzgpjqDwAn
    – mkopriva
    Nov 8 at 9:35












  • The above approach works, thanks to @mkopriva
    – Lee
    Nov 8 at 13:56








2




2




Declare a named type for the struct that you need in multiple places and reuse it. play.golang.org/p/JOzgpjqDwAn
– mkopriva
Nov 8 at 9:35






Declare a named type for the struct that you need in multiple places and reuse it. play.golang.org/p/JOzgpjqDwAn
– mkopriva
Nov 8 at 9:35














The above approach works, thanks to @mkopriva
– Lee
Nov 8 at 13:56




The above approach works, thanks to @mkopriva
– Lee
Nov 8 at 13:56












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Using the suggestion provided by @mkopriva I have produced an updated version of my go playground script.



I am not sure if my changes are perfect or reflect exactly what he was thinking, but for the main it seems to fulfil the main points of the problem.



If anyone has any improvements they would be welcome.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204754%2fhow-can-i-alter-struct-xml-tags-in-go%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Using the suggestion provided by @mkopriva I have produced an updated version of my go playground script.



    I am not sure if my changes are perfect or reflect exactly what he was thinking, but for the main it seems to fulfil the main points of the problem.



    If anyone has any improvements they would be welcome.






    share|improve this answer

























      up vote
      0
      down vote













      Using the suggestion provided by @mkopriva I have produced an updated version of my go playground script.



      I am not sure if my changes are perfect or reflect exactly what he was thinking, but for the main it seems to fulfil the main points of the problem.



      If anyone has any improvements they would be welcome.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Using the suggestion provided by @mkopriva I have produced an updated version of my go playground script.



        I am not sure if my changes are perfect or reflect exactly what he was thinking, but for the main it seems to fulfil the main points of the problem.



        If anyone has any improvements they would be welcome.






        share|improve this answer












        Using the suggestion provided by @mkopriva I have produced an updated version of my go playground script.



        I am not sure if my changes are perfect or reflect exactly what he was thinking, but for the main it seems to fulfil the main points of the problem.



        If anyone has any improvements they would be welcome.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 14:04









        Lee

        145




        145






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204754%2fhow-can-i-alter-struct-xml-tags-in-go%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)