codedownio / aeson-typescript

Generate TypeScript definition files from your ADTs
BSD 3-Clause "New" or "Revised" License
60 stars 27 forks source link

Add functions to FormatOptions to modify output interface/type names. #5

Closed Reisen closed 5 years ago

Reisen commented 5 years ago

Aeson provides two functions, fieldLabelModifier and constructorTagModifier which are used to modify the names of fields and data constructor tags, aeson-typescript respects these but gives no control over the ability to modify the names of the interfaces and types it generates.

The main problem this causes happens when you want to generate types for two types from different qualified modules, for example:

import qualified Foo
import qualified Bar
import           Data.Aeson ( defaultOptions )

deriveTypeScript defaultOptions ''Foo.Example
deriveTypeScript defaultOptions ''Bar.Example

Which will create a file with two duplicate types:

// From the first derivation.
type Example = IExample;
interface IExample { ... };

// From the second derivation.
type Example = IExample;
interface IExample { ... }

This PR adds two modifier functions similar to aeson's to FormattingOptions, adding these gives the ability to modify record type names in any way you need. Either automatically with generics or just manually such as:

formatTSDeclaration (FormatOptions 4 (<> "Foo") (<> "Foo")) declaration

To produce:

// From the first derivation.
type ExampleFoo = IExampleFoo;
interface IExampleFoo { ... };
thomasjm commented 5 years ago

Thanks! Looks good, merging now.