dtslobe4694 2015-08-18 13:20
浏览 34

Golang-将数据从一个复杂的结构映射到另一个

I'm using Go structs to represent two structurally different XML documents. One XML document is the source of the data being processed, and the other XML document is the target that the source should map into.

I have both XML documents mapped out in Go structs already, and both know how to marshal/unmarshal an XML structure.

So currently I'm typing out each path in each struct, to map fields from the source, into fields in the target.

Is there a quicker way to perhaps initialize a new Target instance, using some sort of shorthand to ease the mapping of data? Keeping in mind these are complex nested structures, so a pattern I can follow "to the depths" would be excellent.

EDIT: Adding sample XML data structures

The Go structs that represent these XML structures are pretty much a one-to-one representation of the XML structure, where each level of nesting is a new struct type. The structs were generated programmatically and are quite spread out, so it would take excessive effort to represent the data structures here (I'm short on time).

SOURCE data file - reaches up to 8 nested elements deep. There can be multiple elements, each of which will generate a new separate "TARGET" instance.

<orders>
  <order order=no="111222333">
    <customer>
      <customer-no>555444333</customer-no>
      <customer-email>test@test.com</customer-email>
      <billing-address>
        <first-name>Test</first-name>
        <last-name>User</last-name>
      </billing-address>
    </customer>
    <product-lineitems>
      <product-lineitem>
        <product-id>SomeProductSku</product-id>
        <product-name>SomeProductName</product-name>
        <base-price>40.00</base-price>
        <quantity>1</quantity>
      </product-lineitem>
    </product-lineitems>
  </order>
</orders>

TARGET data file - reaches up to 10 nested elements deep

<OrderCreateRequest orderType="SALES" requestId="bcHcsiaagLwM6aaadnXz7ifXZL">
    <Order customerOrderId="111222333" levelOfService="REGULAR">
        <Customer customerId="555444333">
            <Name>
                <LastName>User</LastName>
                <FirstName>Test</FirstName>
            </Name>
            <EmailAddress>test@test.com</EmailAddress>
        </Customer>
        <OrderItems>
            <OrderItem id="item_0_1" webLineId="1">
                <ItemId>SomeProductSku</ItemId>
                <Quantity>1</Quantity>
                <Description>
                    <Description>SomeProductName</Description>
                </Description>
                <Pricing>
                    <Merchandise>
                        <UnitPrice>40.00</UnitPrice>
                    </Merchandise>
                </Pricing>
            </OrderItem>
        </OrderItems>
    </Order>
</OrderCreateRequest>

EDIT 2: I'm also trying to solve for type mappings not matching up. Currently I'm casting each value type as I assign it, which makes me thing I'm really going about this the wrong way. The XML<->struct representations are already built, so if it's just a matter of me needing to somehow initialize the Target struct using something like map[string]{interface{}} or whatever, let me know. Been quite awhile since I last looked at Go, so I'm certain I'm missing some better solution here

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 模型在y分布之外的数据上预测能力不好如何解决
    • ¥15 processing提取音乐节奏
    • ¥15 python进程启动打包问题
    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题