jmattheis / goverter

Generate type-safe Go converters by defining signatures of different types.
https://goverter.jmattheis.de/
MIT License
546 stars 48 forks source link

Support regex for mapping field names #122

Open jmattheis opened 10 months ago

jmattheis commented 10 months ago

When you have fields that are flattened and then prefixed with the nested field name, it would be useful when goverter would support regex.

This example is pretty verbose:

// goverter:map DBObject.ID ID
// goverter:map DBObject.CreatedAt CreatedAt
// goverter:map DBObject.UpdatedAt UpdatedAt
// goverter:map VictoryConditions.NumCriteriaRequired VictoryConditionsNumCriteriaRequired
// goverter:map VictoryConditions.YearsPassed VictoryConditionsYearsPassed
// goverter:map VictoryConditions.OwnPlanets VictoryConditionsOwnPlanets
// goverter:map VictoryConditions.AttainTechLevel VictoryConditionsAttainTechLevel
// goverter:map VictoryConditions.AttainTechLevelNumFields VictoryConditionsAttainTechLevelNumFields
// goverter:map VictoryConditions.ExceedsScore VictoryConditionsExceedsScore
// goverter:map VictoryConditions.ExceedsSecondPlaceScore VictoryConditionsExceedsSecondPlaceScore
// goverter:map VictoryConditions.ProductionCapacity VictoryConditionsProductionCapacity
// goverter:map VictoryConditions.OwnCapitalShips VictoryConditionsOwnCapitalShips
// goverter:map VictoryConditions.HighestScoreAfterYears VictoryConditionsHighestScoreAfterYears
// goverter:map VictoryConditions.Conditions VictoryConditionsConditions
// goverter:map Area.X AreaX
// goverter:map Area.Y AreaY
ConvertGameGame(source *cs.Game) *Game

but with regex this is fairly easy to describe:

// goverter:map:regex DBObject.(\w+) $1
// goverter:map:regex VictoryConditions.(\w+) VictoryConditions$1
// goverter:map:regex Area.(\w+) Area$1
ConvertGameGame(source *cs.Game) *Game

Example from: https://github.com/sirgwain/craig-stars/blob/e3683743518428ee08bf8e63bbf1483f3d1e517f/db/converter.go#L140

Please :+1: this issue if you like this functionality. If you have a specific use-case in mind, feel free to comment it.