Skip to content

Commit

Permalink
feat: add enum support for android (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
akashrpo authored Oct 1, 2024
1 parent f50c325 commit c40e69b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
55 changes: 54 additions & 1 deletion src/generators/android/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ type AndroidObjectContext = {
type AndroidPropertyContext = {
// The formatted name for this property, ex: "numAvocados".
name: string;
// The formatted enum name, ex: "NumAvocados".
enumName?: string;
// Whether this property has an enum.
hasEnum: boolean;
// The formatted enum values, ex: "NUM
enumValues?: any;
// The type of this property. ex: "String".
type: string;
// Whether the property is nullable (@NonNull vs @Nullable modifier).
Expand Down Expand Up @@ -56,19 +62,23 @@ export const android: Generator<
},
generatePrimitive: async (client, schema, parentPath) => {
let type = 'Object';
let hasEnum = false;

if (schema.type === Type.STRING) {
type = 'String';
hasEnum = !!schema.enum;
} else if (schema.type === Type.BOOLEAN) {
type = 'Boolean';
} else if (schema.type === Type.INTEGER) {
type = 'Long';
hasEnum = !!schema.enum;
} else if (schema.type === Type.NUMBER) {
type = 'Double';
hasEnum = !!schema.enum;
}

return {
...defaultPropertyContext(client, schema, type, parentPath),
...defaultPropertyContext(client, schema, type, parentPath, hasEnum),
};
},
setup: async () => ({}),
Expand Down Expand Up @@ -128,23 +138,66 @@ export const android: Generator<
'generators/android/templates/SerializableProperties.java.hbs',
context,
),
...context.objects.map((o) =>
o.properties.forEach((p) => {
if (p.hasEnum) {
// Generate the enum file
client.generateFile(
`${upperFirst(camelCase(p.name))}.java`,
'generators/android/templates/enumeration.java.hbs',
p,
);
}
}),
),
...context.objects.map((o) =>
client.generateFile(`${o.name}.java`, 'generators/android/templates/class.java.hbs', o),
),
]);
},
};

const convertToEnum = (values: any[], type: string) => {
return (
values
.map((value) => {
let key, formattedValue;

if (type === 'String' || typeof value === 'string') {
key = 'S_' + value.toString().trim().toUpperCase().replace(/ /g, '');
formattedValue = `"${value.toString().trim()}"`; // String values
} else if (type === 'Long') {
key = 'L_' + value.toString();
formattedValue = `${value}L`; // Long values
} else if (type === 'Double') {
key = 'D_' + value.toString().replace('.', '_');
formattedValue = `${value}D`; // Double values
}

return `${key}(${formattedValue})`;
})
.join(',\n ') + ';'
); // Join with commas and add semicolon at the end
};

function defaultPropertyContext(
client: GeneratorClient,
schema: Schema,
type: string,
namespace: string,
hasEnum?: boolean,
): AndroidPropertyContext {
return {
name: client.namer.register(schema.name, namespace, {
transform: camelCase,
}),
hasEnum: !!hasEnum,
enumName: hasEnum
? client.namer.register(schema.name, namespace, {
transform: (name) => upperFirst(camelCase(name)),
})
: undefined,
enumValues: hasEnum && 'enum' in schema ? convertToEnum(schema.enum!, type) : undefined,
type,
isVariableNullable: !schema.isRequired || !!schema.isNullable,
shouldThrowRuntimeError: schema.isRequired && !schema.isNullable,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* {{autogeneratedFileWarning}}
*/
package com.rudderstack.generated;
package com.rudderstack.ruddertyper.generated;

import java.util.*;
import com.rudderstack.android.sdk.core.RudderClient;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/android/templates/RudderTyperUtils.java.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* {{autogeneratedFileWarning}}
*/
package com.rudderstack.generated;
package com.rudderstack.ruddertyper.generated;

import com.rudderstack.android.sdk.core.RudderOption;
import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* {{autogeneratedFileWarning}}
*/
package com.rudderstack.generated;
package com.rudderstack.ruddertyper.generated;

import java.util.*;
import com.rudderstack.android.sdk.core.RudderProperty;
Expand Down
4 changes: 2 additions & 2 deletions src/generators/android/templates/class.java.hbs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* {{autogeneratedFileWarning}}
*/
package com.rudderstack.generated;
package com.rudderstack.ruddertyper.generated;

import java.util.*;
import com.rudderstack.android.sdk.core.RudderProperty;
Expand Down Expand Up @@ -43,7 +43,7 @@ public final class {{name}} extends SerializableProperties {
* This property is optional and not required to generate a valid {{../name}} object
{{/if}}
*/
public Builder {{name}}(final {{#if isVariableNullable}}@Nullable{{else}}@NonNull{{/if}} {{type}} {{name}}) {
public Builder {{name}}(final {{#if isVariableNullable}}@Nullable{{else}}@NonNull{{/if}} {{#if hasEnum}}{{enumName}}{{else}}{{type}}{{/if}} {{name}}) {
{{#if isListType}}
List<?> p = RudderTyperUtils.serializeList({{name}});
properties.put("{{rawName}}", p);
Expand Down
20 changes: 20 additions & 0 deletions src/generators/android/templates/enumeration.java.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* {{autogeneratedFileWarning}}
*/
package com.rudderstack.ruddertyper.generated;


public enum {{enumName}} {

{{enumValues}}

private final Object value;

{{enumName}}(Object value) {
this.value = value;
}

public Object getValue() {
return value;
}
}

0 comments on commit c40e69b

Please sign in to comment.