Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TomlMapper chaining/delegation #3

Closed
comp500 opened this issue May 23, 2022 · 2 comments
Closed

TomlMapper chaining/delegation #3

comp500 opened this issue May 23, 2022 · 2 comments
Milestone

Comments

@comp500
Copy link
Contributor

comp500 commented May 23, 2022

I'd like to structure the mappers for my types in a bottom-up way, rather than specifying everything from the top level, and pass various context data down. This is the sort of thing I have currently:

data class IndexFileLoc(
	val file: PackwizPath,
	val hashFormat: String,
	val hash: String,
) {
	companion object {
		fun mapper(base: PackwizPath.Base) = tomlMapper {
			mapping<IndexFileLoc>("hash-format" to "hashFormat")
			val pathMapper = PackwizPath.mapper(base)
			decoder { it: TomlValue.String -> pathMapper.decode<PackwizPath>(it) }
			encoder { it: PackwizPath -> pathMapper.encode(it) }
		}
	}
}

It'd be great if there was a cleaner way to handle this, such as a delegate method that specifies a mapper to delegate mapping of a given type to (or infer from the types encoders/decoders/mappings are specified for?)

@valderman
Copy link
Owner

I'm not sure I follow. From your example, it looks like you'd want a function TomlMapperConfigurator.delegate<T>(mapper: TomlMapper), or perhaps TomlMapperConfigurator.delegate(typesToDelegate: Set<KType>, mapper: TomlMapper), which would basically inherit transcoding for the given type(s) from the given mapper. Is that right?

@comp500
Copy link
Contributor Author

comp500 commented Jun 20, 2022

Yep, that's right. I'm not sure whether specifying types upfront or having them be inferred would be better; but it's important that decoders/encoders defined in a TomlMapper take precedence over delegated mappers if they are inferred.

@valderman valderman added this to the 1.1.0 milestone Jun 23, 2022
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

No branches or pull requests

2 participants