Skip to content

Commit

Permalink
Merge pull request #28 from palantir/rmcnamara/add-additional-output-…
Browse files Browse the repository at this point in the history
…types

generateTypescriptService now takes an optional list of classes to output
  • Loading branch information
ryanmcnamara committed Mar 4, 2016
2 parents 217d5d1 + 30810de commit 7d561ec
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ public ServiceEmitter(ServiceModel model, TypescriptServiceGeneratorConfiguratio
this.writer = writer;
}

public void emitTypescriptTypes(TypescriptServiceGeneratorConfiguration settings) {
public void emitTypescriptTypes(TypescriptServiceGeneratorConfiguration settings, List<Type> additionalTypesToOutput) {
Settings settingsToUse = settings.getSettings();
TypeProcessor baseTypeProcessor = settingsToUse.customTypeProcessor;

Set<Type> referencedTypes = model.referencedTypes();
Set<Type> referencedTypes = Sets.newHashSet(model.referencedTypes().iterator());
referencedTypes.addAll(additionalTypesToOutput);
Set<Class<?>> referencedClasses = getReferencedClasses(referencedTypes, settings);
final Set<Type> discoveredTypes = Sets.newHashSet(referencedClasses.iterator());
referencedClasses = filterInputClasses(referencedClasses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.List;

import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -51,6 +52,10 @@ public ServiceGenerator(TypescriptServiceGeneratorConfiguration settings) {
}

public void generateTypescriptService(Class<?> clazz) {
this.generateTypescriptService(clazz, Lists.newArrayList());
}

public void generateTypescriptService(Class<?> clazz, List<Type> additionalClassesToOutput) {
OutputStream output = null;
try {
output = new FileOutputStream(new File(settings.generatedFolderLocation(), Character.toLowerCase(clazz.getSimpleName().charAt(0)) + clazz.getSimpleName().substring(1) + ".ts"));
Expand All @@ -62,7 +67,7 @@ public void generateTypescriptService(Class<?> clazz) {

ServiceModel serviceModel = new ServiceClassParser().parseServiceClass(clazz, settings);
ServiceEmitter serviceEndpointEmitter = new ServiceEmitter(serviceModel, settings, writer);
serviceEndpointEmitter.emitTypescriptTypes(settings);
serviceEndpointEmitter.emitTypescriptTypes(settings, additionalClassesToOutput);
serviceEndpointEmitter.emitTypescriptInterface();
serviceEndpointEmitter.emitTypescriptClass();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
import org.junit.Before;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.palantir.code.ts.generator.model.ServiceModel;
import com.palantir.code.ts.generator.utils.TestUtils.ConcreteObjectService;
import com.palantir.code.ts.generator.utils.TestUtils.DuplicateMethodNamesService;
import com.palantir.code.ts.generator.utils.TestUtils.MyObject;
import com.palantir.code.ts.generator.utils.TestUtils.TestComplexServiceClass;
import com.palantir.code.ts.generator.utils.TestUtils.TestServiceClass;

public class ServiceEmitterTest {

Expand Down Expand Up @@ -45,7 +48,7 @@ public void before() {
public void testComplexServiceClassEmitTypes() {
ServiceModel model = serviceClassParser.parseServiceClass(TestComplexServiceClass.class, settings);
ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer);
serviceEmitter.emitTypescriptTypes(settings);
serviceEmitter.emitTypescriptTypes(settings, Lists.newArrayList());
writer.close();
String expectedOutput = "" +
"\n" +
Expand Down Expand Up @@ -84,7 +87,7 @@ public void testComplexServiceClassEmitInterface() {
}

@Test
public void testComplexServicClassEmitClass() {
public void testComplexServiceClassEmitClass() {
ServiceModel model = serviceClassParser.parseServiceClass(TestComplexServiceClass.class, settings);
ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer);
serviceEmitter.emitTypescriptClass();
Expand Down Expand Up @@ -187,29 +190,42 @@ public void testConcreteObjectService() {
serviceEmitter.emitTypescriptClass();
writer.close();
String expectedOutput = "\n" +
"export class ConcreteObjectServiceImpl implements ConcreteObjectService {\n" +
"\n" +
" private httpApiBridge: HttpApiBridge;\n" +
" constructor(httpApiBridge: HttpApiBridge) {\n" +
" this.httpApiBridge = httpApiBridge;\n" +
" }\n" +
"\n" +
" public noPathGetter() {\n" +
" var httpCallData = <HttpEndpointOptions> {\n" +
" serviceIdentifier: \"concreteObjectService\",\n" +
" endpointPath: \"concreteObject\",\n" +
" endpointName: \"noPathGetter\",\n" +
" method: \"GET\",\n" +
" mediaType: \"application/json\",\n" +
" requiredHeaders: [],\n" +
" pathArguments: [],\n" +
" queryArguments: {\n" +
" },\n" +
" data: null\n" +
" };\n" +
" return this.httpApiBridge.callEndpoint<string>(httpCallData);\n" +
" }\n" +
"export class ConcreteObjectServiceImpl implements ConcreteObjectService {\n" +
"\n" +
" private httpApiBridge: HttpApiBridge;\n" +
" constructor(httpApiBridge: HttpApiBridge) {\n" +
" this.httpApiBridge = httpApiBridge;\n" +
" }\n" +
"\n" +
" public noPathGetter() {\n" +
" var httpCallData = <HttpEndpointOptions> {\n" +
" serviceIdentifier: \"concreteObjectService\",\n" +
" endpointPath: \"concreteObject\",\n" +
" endpointName: \"noPathGetter\",\n" +
" method: \"GET\",\n" +
" mediaType: \"application/json\",\n" +
" requiredHeaders: [],\n" +
" pathArguments: [],\n" +
" queryArguments: {\n" +
" },\n" +
" data: null\n" +
" };\n" +
" return this.httpApiBridge.callEndpoint<string>(httpCallData);\n" +
" }\n" +
"}\n";
assertEquals(expectedOutput, new String(stream.toByteArray()));
}

@Test
public void testAdditionalClassesToOutput() {
ServiceModel model = serviceClassParser.parseServiceClass(TestServiceClass.class, settings);
ServiceEmitter serviceEmitter = new ServiceEmitter(model, settings, writer);
serviceEmitter.emitTypescriptTypes(settings, Lists.newArrayList(MyObject.class));
writer.close();
String expectedOutput = "\n" +
" export interface MyObject {\n" +
" y: MyObject;\n" +
" }\n";
assertEquals(expectedOutput, new String(stream.toByteArray()));
}
}

0 comments on commit 7d561ec

Please sign in to comment.