shogg / edifact

Read edifact into your Go data structures.
MIT License
18 stars 7 forks source link

Possible to read values for multiple elements #6

Closed nOBITa3001 closed 2 years ago

nOBITa3001 commented 2 years ago

Hello @shogg

Is it possible to read values from multiple elements for a composite I saw the matching logic supports 1 element value here - https://github.com/shogg/edifact/blob/1efb937895bd29fa85d2e43e568c3cb31e804b76/build/segmentselector.go#L94

For example, I have this segment EQD+CN+CSNU1487660+22G1:102:5++2+5' and would like to get this 22G1:102:5 value with 1 field. I could not find a document for edifact mapping definition so I just map 1 field on each element

ISO1            string `edifact:"SG2/SG3/EQD+CN+?+?"`
ISO2            string `edifact:"SG2/SG3/EQD+CN+?+?:?"`
ISO3            string `edifact:"SG2/SG3/EQD+CN+?+?:?:?"`

Is there a way to map all elements to 1 field solution or any document that I can find on how to define the edifact definition for this. Expected something like a wildcard:

ISO            string `edifact:"SG2/SG3/EQD+CN+?+*"`
shogg commented 2 years ago

You can implement edifact.Unmarshaller for the type of a struct field. A basic example is in unmarshal_orders_test.go:58. When leaving out the ? placeholder completely UnmarshalEdifact will be called with the entire segment.

Something like this:

type Container struct {
    ISO ISO `edifact:"SG2/SG3/EQD+CN"` // tag has no '?'
}

type ISO string

func (iso *ISO) UnmarshalEdifact(data []byte) error {
    // parse iso from data
}

Edit: A * to return a composite is a good idea. Could be useful together with edifact.Unmarshaller or on its own.

ISO            string `edifact:"SG2/SG3/EQD+CN++*"`
nOBITa3001 commented 2 years ago

Hello @shogg

I can make it works based on the guidance

Thank you!

shogg commented 2 years ago

@nOBITa3001 I just pushed a new version with '*' placeholder support.

nOBITa3001 commented 2 years ago

@shogg

pulled the new version and applied a change. It works like a charm!

Thank you very much