Genbox / SimpleS3

A .NET Core implementation of Amazon's S3 API with focus on simplicity, security and performance
MIT License
44 stars 8 forks source link

How do I convert a string region code to an enum? #40

Closed LordMike closed 3 years ago

LordMike commented 3 years ago

The S3 and other configs seem to prefer the region codes as enums - but my users will provide it as strings. Ideally, it should be possible to use AmazonS3Region directly in the config files, and have Options convert it to the enum from the string it was. This can be done by implementing a TypeConveter and attributing the enums with the [TypeConverter] attribute.

At runtime, Options will call Convert.ChangeType() which then calls the custom TypeConverter.

Could this be added? :)

Genbox commented 3 years ago

There is a IRegionConverter interface to convert between enums and strings. I've improved it a bit so now it supports conversion both ways.

Use it like this:

RegionConverter converter = new RegionConverter(AmazonS3RegionData.Instance);
var regionInfo = converter.GetRegion(TestRegion.RegionOne);

RegionInfo contains the region code, name of the region, and the enum value.

You should be able to create a TypeConverter that converts between the enum and string with that.