Skip to content

Allowlist based conversion #210

Description

@zhulik

Is your feature request related to a problem? Please describe.

In my project, I have a generated struct which looks like this:

type AssetSetter struct {
	UUID                          *uuid.UUID           `db:"uuid,pk" `
	...
	DeactivationReason            *sql.Null[string]    `db:"deactivation_reason" `
	CreatedAt                     *time.Time           `db:"created_at" `
	UpdatedAt                     *time.Time           `db:"updated_at" `
	DeactivatedAt                 *sql.Null[time.Time] `db:"deactivated_at" `
}

It's used to create and update records in the sql table. Is case of insert operation - all fields must present(except those which are nullable), in case of update - only non-nil ones will be updated.

I have a corresponding struct in the domain model which has the same fields, but may have different types and so on. I use goverter to generate converters from the domain struct to the setter.

The problem is that I have cases when I only want to update a subset of fields given the entire domain struct. In case of deactivation, I want to make sure that only DeactivatedAt is converted from the domain struct to the setter:

domain.Asset{
<skipped non-nil and non-zero fields> 
  DeactivatedAt: time.Now(),
  DeactivationReason: &"deleted",
}

should be converted into

AssetSetter{DeactivatedAt: <from the domain struct>, DeactivationReson: <from the domain struct>}

All the rest of the fields for the setter should remain nils.

Describe the solution you'd like

It would be awesome to have a possibility to convert values based on an allow list:

type Converter{
  // goverter:ignoreAll <- a new comment
  // goverter:map DeactivatedAt DeactivatedAt <- may have only one argument, may have a diffent name like goverter:allow
  // goverter:map DeactivationReason DeactivationReason
  Convert(domain.Asset) db.AssetSetter
}

Only fields allowed explicitly will be converted.

Describe alternatives you've considered
I tried goverter:ignore, but the more fields the output struct has, the more ignores I should add, it quickly turns into a burden

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions