Skip to content

Conversation

@Stashchenko
Copy link
Contributor

@Stashchenko Stashchenko commented Sep 11, 2025

Feature for this #383

Feature: Introduce composite subfields handling with SubfieldAccessor interface

Problem

The current implementation of Unset/Set for subfields works only when the field is a *field.Composite.
This makes it hard to support more complex composite specs that wrap or embed a Composite.

For example, we faced this situation:

type CompositeDE48Field struct {
    TCC *field.String
    *field.Composite
}

var (
    _ field.Field      = (*CompositeDE48Field)(nil)
    _ json.Marshaler   = (*CompositeDE48Field)(nil)
    _ json.Unmarshaler = (*CompositeDE48Field)(nil)
)

In this case, Unset and Set won't work because of the strict type check against *Composite.


Solution

Introduce an interface SubfieldAccessor which abstracts composite-like fields.

type SubfieldAccessor interface {
    SetSubfields(idPath string, value string) error
    SetSubfield(id string, value string) error
    UnsetSubfields(idPaths ...string) error
    UnsetSubfield(id string)
}

Then, instead of requiring *Composite, check against this interface:

composite, ok := f.(SubfieldAccessor)
if !ok {
    return fmt.Errorf("field %s is not a composite field and its subfields %s cannot be unset", id, path)
}

Benefits

  • Makes composite handling more universal.
  • Supports both *field.Composite and custom composite wrappers like CompositeDE48Field.
  • Keeps the API backward compatible.
  • Provides flexibility for more complex composite field definitions in the future.

@Stashchenko Stashchenko requested a review from alovak as a code owner September 11, 2025 13:50
@Stashchenko Stashchenko force-pushed the feature/383-add-set-field branch from 2c91648 to b75968e Compare September 12, 2025 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant