Skip to content

Feature Request: Support setting composite fields (SetField) #383

@Stashchenko

Description

@Stashchenko

Current Behavior

The current method:

func (m *Message) Field(id int, val string)

only works for simple field types. There is no straightforward way to set values for composite fields that have subfields.


Proposal

Introduce a new method like:

func (m *Message) SetField(path string, val string) error
  • path: a string representing the field and optional subfield, e.g., "3.01" for field 3, subfield 01.
  • The method should automatically detect whether the target is a simple field or a composite field and set the value appropriately.

Example Usage

func TestMessage_SetField(t *testing.T) {
    // Define field 3 as a composite field, it's the hack but just for example 
    Spec87.Fields[3] = field.NewComposite(&field.Spec{
        Length:      6,
        Description: "Processing Code",
        Pref:        prefix.ASCII.Fixed,
        Tag: &field.TagSpec{ Sort: sort.Strings },
        Subfields: map[string]field.Field{
            "01": field.NewString(&field.Spec{
                Length:      2,
                Description: "Cardholder Transaction Type Code",
                Enc:         encoding.ASCII,
                Pref:        prefix.ASCII.Fixed,
            }),
            "02": field.NewString(&field.Spec{
                Length:      2,
                Description: "Cardholder “From Account” Type Code",
                Enc:         encoding.ASCII,
                Pref:        prefix.ASCII.Fixed,
            }),
            "03": field.NewString(&field.Spec{
                Length:      2,
                Description: "Cardholder “To Account” Type Code",
                Enc:         encoding.ASCII,
                Pref:        prefix.ASCII.Fixed,
            }),
        },
    })

    message := iso8583.NewMessage(Spec87)
    // unpack and all the check on real data
    // _ = message.Unpack(someData)
   
    // Set a value into a subfield to modify the data
    err := message.SetField("3.01", "10")
    require.NoError(t, err)
}

Benefits

  • Makes it easy to work with composite fields without manually drilling into subfields.
  • Keeps a clean and consistent API for both simple and composite fields.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions