Skip to content

feat(parser): adds IsEmpty() method for OptNil* generated wrappers#1667

Merged
ernado merged 4 commits into
ogen-go:mainfrom
MikeKirillov:ogen_OptNil_IsEmpty
May 4, 2026
Merged

feat(parser): adds IsEmpty() method for OptNil* generated wrappers#1667
ernado merged 4 commits into
ogen-go:mainfrom
MikeKirillov:ogen_OptNil_IsEmpty

Conversation

@MikeKirillov
Copy link
Copy Markdown
Contributor

Summary

When generating schemas with optional, nullable fields, it is often necessary to distinguish between a value being explicitly set to null versus being entirely omitted from the input (e.g., the "Zero" test case in the included examples).

for _, tc := range []struct {
Name string
Value api.OptNilString
Result string
}{
{
Name: "Zero",
Value: api.OptNilString{},
Result: "{}",
},
{
Name: "Set",
Value: api.NewOptNilString("foo"),
Result: `{"key":"foo"}`,
},
{
Name: "Nil",
Value: api.OptNilString{Null: true, Set: true},
Result: `{"key":null}`,
},
} {

To simplify this check, this PR introduces the IsEmpty() method for OptNil*** wrappers, providing a clean and efficient way to verify if a field was provided.

Usage Example

Consider the following struct:

type OrderDto struct {
	OrderUUID uuid.UUID `json:"order_uuid"`
	HullUUID uuid.UUID `json:"hull_uuid"`
	EngineUUID uuid.UUID `json:"engine_uuid"`
	ShieldUUID OptNilUUID `json:"shield_uuid"`
	WeaponUUID OptNilUUID `json:"weapon_uuid"`
	// ...
}

Before

Previously, checking for an omitted value required a more verbose approach:

func OrderCreateRqToModel(orderRq *orderv1.CreateOrderRequest) model.CreateOrderRq {
    // Required fields
	result := model.CreateOrderRq{
		HullUUID:   orderRq.HullUUID.String(),
		EngineUUID: orderRq.EngineUUID.String(),
	}
    // Process optional field if present
	if orderRq.GetShieldUUID().IsSet() && !orderRq.GetShieldUUID().IsNull() {
		val := orderRq.GetShieldUUID().Value.String()
		result.ShieldUUID = &val
	}
    // Handle omitted field
	if !orderRq.GetWeaponUUID().IsSet() && !orderRq.GetWeaponUUID().IsNull() {
		// ...
	}
	return result
}

After

With IsEmpty(), the logic is much more concise:

func OrderCreateRqToModel(orderRq *orderv1.CreateOrderRequest) model.CreateOrderRq {
    // Required fields
	result := model.CreateOrderRq{
		HullUUID:   orderRq.HullUUID.String(),
		EngineUUID: orderRq.EngineUUID.String(),
	}
    // Process optional field if present
	if !orderRq.GetShieldUUID().IsEmpty() {
		val := orderRq.GetShieldUUID().Value.String()
		result.ShieldUUID = &val
	}
    // Handle omitted field
	if orderRq.GetWeaponUUID().IsEmpty() {
		// ...
	}
	return result
}

@ernado ernado merged commit d93a653 into ogen-go:main May 4, 2026
15 checks passed
@MikeKirillov MikeKirillov deleted the ogen_OptNil_IsEmpty branch May 4, 2026 12:54
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.

2 participants