44
55import javax .annotation .Nullable ;
66import java .io .InputStream ;
7- import java .lang .annotation .Annotation ;
87import java .nio .file .Files ;
98import java .nio .file .Path ;
109import java .util .EnumSet ;
1110import java .util .LinkedHashSet ;
1211import java .util .Properties ;
1312import java .util .Set ;
13+ import java .util .function .Function ;
1414
1515/**
1616 * Global properties loader.
@@ -29,15 +29,19 @@ public static Props loadProps(@Nullable Path userPropertiesFile) {
2929 if (userPropsInputstream != null ) {
3030 properties .load (userPropsInputstream );
3131 }
32- return loadProps (properties );
32+ Props props = loadProps (properties );
33+ if (props .getTypescript ().getOptionalFieldFormats ().isEmpty ()) {
34+ throw new IllegalArgumentException ("Props 'typescript.optional-field-format' cannot be empty." );
35+ }
36+ return props ;
3337 } catch (Exception e ) {
3438 throw new SharedTypeException ("Failed to load properties." , e );
3539 }
3640 }
3741
38- private static Props loadProps (Properties properties ) throws Exception {
42+ private static Props loadProps (Properties properties ) {
3943 return Props .builder ()
40- .targets (parseEnumSet (properties . getProperty ( "sharedtype.targets" ) , OutputTarget .class ))
44+ .targets (parseEnumSet (properties , "sharedtype.targets" , OutputTarget .class , OutputTarget :: valueOf ))
4145 .optionalAnnotations (parseClassSet (properties , "sharedtype.optional-annotations" ))
4246 .optionalContainerTypes (splitArray (properties .getProperty ("sharedtype.optional-container-types" )))
4347 .accessorGetterPrefixes (splitArray (properties .getProperty ("sharedtype.accessor.getter-prefixes" )))
@@ -50,6 +54,8 @@ private static Props loadProps(Properties properties) throws Exception {
5054 .outputFileName (properties .getProperty ("sharedtype.typescript.output-file-name" ))
5155 .interfacePropertyDelimiter (properties .getProperty ("sharedtype.typescript.interface-property-delimiter" ).charAt (0 ))
5256 .javaObjectMapType (properties .getProperty ("sharedtype.typescript.java-object-map-type" ))
57+ .optionalFieldFormats (parseEnumSet (properties ,"sharedtype.typescript.optional-field-format" ,
58+ Props .Typescript .OptionalFieldFormat .class , Props .Typescript .OptionalFieldFormat ::fromString ))
5359 .build ())
5460 .rust (Props .Rust .builder ()
5561 .outputFileName (properties .getProperty ("sharedtype.rust.output-file-name" ))
@@ -60,11 +66,15 @@ private static Props loadProps(Properties properties) throws Exception {
6066 .build ();
6167 }
6268
63- private static <T extends Enum <T >> Set <T > parseEnumSet (String value , Class <T > type ) {
64- Set <String > trimmedElems = splitArray (value );
69+ private static <T extends Enum <T >> Set <T > parseEnumSet (Properties properties , String propertyName , Class <T > type , Function < String , T > enumValueOf ) {
70+ Set <String > trimmedElems = splitArray (properties . getProperty ( propertyName ) );
6571 Set <T > set = EnumSet .noneOf (type );
66- for (String trimmed : trimmedElems ) {
67- set .add (Enum .valueOf (type , trimmed ));
72+ try {
73+ for (String trimmed : trimmedElems ) {
74+ set .add (enumValueOf .apply (trimmed ));
75+ }
76+ } catch (Exception e ) {
77+ throw new IllegalArgumentException (String .format ("Failed to parse property '%s'" , propertyName ), e );
6878 }
6979 return set ;
7080 }
0 commit comments