Search in sources :

Example 1 with Settings

use of cz.habarta.typescript.generator.Settings in project typescript-generator by vojtechhabarta.

the class AxiosClientExtensionTest method test.

@Test
public void test() {
    final Settings settings = TestUtils.settings();
    settings.outputFileType = TypeScriptFileType.implementationFile;
    settings.outputKind = TypeScriptOutputKind.module;
    settings.generateJaxrsApplicationClient = true;
    settings.jaxrsNamespacing = JaxrsNamespacing.perResource;
    settings.extensions.add(new AxiosClientExtension());
    final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(JaxrsApplicationTest.OrganizationApplication.class));
    final String errorMessage = "Unexpected output: " + output;
    Assert.assertTrue(errorMessage, output.contains("interface Organization"));
    Assert.assertTrue(errorMessage, output.contains("interface Address"));
    Assert.assertTrue(errorMessage, output.contains("interface Person"));
    Assert.assertTrue(errorMessage, output.contains("interface HttpClient"));
    Assert.assertTrue(errorMessage, output.contains("class OrganizationsResourceClient<O>"));
    Assert.assertTrue(errorMessage, output.contains("class PersonResourceClient<O>"));
    Assert.assertTrue(errorMessage, output.contains("type RestResponse<R> = Promise<Axios.GenericAxiosResponse<R>>"));
    Assert.assertTrue(errorMessage, output.contains("class AxiosHttpClient implements HttpClient<Axios.AxiosRequestConfig>"));
    Assert.assertTrue(errorMessage, output.contains("request<R>(requestConfig: { method: string; url: string; queryParams?: any; data?: any; copyFn?: (data: R) => R; options?: Axios.AxiosRequestConfig; }): RestResponse<R>"));
    Assert.assertTrue(errorMessage, output.contains("class AxiosOrganizationsResourceClient extends OrganizationsResourceClient<Axios.AxiosRequestConfig>"));
    Assert.assertTrue(errorMessage, output.contains("class AxiosPersonResourceClient extends PersonResourceClient<Axios.AxiosRequestConfig>"));
    Assert.assertTrue(errorMessage, output.contains("constructor(baseURL: string, axiosInstance: Axios.AxiosInstance = axios.create())"));
}
Also used : TypeScriptGenerator(cz.habarta.typescript.generator.TypeScriptGenerator) Settings(cz.habarta.typescript.generator.Settings) Test(org.junit.Test) JaxrsApplicationTest(cz.habarta.typescript.generator.JaxrsApplicationTest)

Example 2 with Settings

use of cz.habarta.typescript.generator.Settings in project openremote by openremote.

the class AssetModelInfoExtension method emitElements.

@SuppressWarnings("rawtypes")
@Override
public void emitElements(Writer writer, Settings settings, boolean exportKeyword, TsModel model) {
    Map<String, String> assetMap = new HashMap<>();
    Map<String, String> otherMap = new HashMap<>();
    Arrays.stream(ValueUtil.getAssetInfos(null)).forEach(assetModelInfo -> {
        String assetDescriptorName = assetModelInfo.getAssetDescriptor().getName();
        assetMap.put(assetDescriptorName.toUpperCase(Locale.ROOT), assetDescriptorName);
        // Store attributes
        Arrays.stream(assetModelInfo.getAttributeDescriptors()).forEach(attributeDescriptor -> {
            String attributeName = attributeDescriptor.getName();
            otherMap.put(attributeName.toUpperCase(Locale.ROOT), attributeName);
        });
    });
    emitEnum(writer, "WellknownAssets", assetMap);
    writer.writeIndentedLine("");
    emitEnum(writer, "WellknownAttributes", otherMap);
    otherMap.clear();
    Arrays.stream(ValueUtil.getMetaItemDescriptors()).forEach(metaItemDescriptor -> {
        String metaName = metaItemDescriptor.getName();
        otherMap.put(metaName.toUpperCase(Locale.ROOT), metaName);
    });
    writer.writeIndentedLine("");
    emitEnum(writer, "WellknownMetaItems", otherMap);
    otherMap.clear();
    Arrays.stream(ValueUtil.getValueDescriptors()).forEach(valueDescriptor -> {
        String valueTypeName = valueDescriptor.getName();
        otherMap.put(valueTypeName.toUpperCase(Locale.ROOT).replaceAll("[^A-Z0-9]", ""), valueTypeName);
    });
    writer.writeIndentedLine("");
    emitEnum(writer, "WellknownValueTypes", otherMap);
    otherMap.clear();
    Arrays.stream(Constants.class.getFields()).filter(f -> f.getName().startsWith("UNITS_")).forEach(unitField -> {
        String unitName = unitField.getName().substring(6);
        try {
            otherMap.put(unitName.toUpperCase(Locale.ROOT), (String) unitField.get(null));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    });
    writer.writeIndentedLine("");
    emitEnum(writer, "WellknownUnitTypes", otherMap);
    otherMap.clear();
    Arrays.stream(Ruleset.class.getFields()).filter(f -> f.getType() == MetaItemDescriptor.class).forEach(rulesetMeta -> {
        try {
            MetaItemDescriptor metaItemDescriptor = (MetaItemDescriptor) rulesetMeta.get(null);
            otherMap.put(metaItemDescriptor.getName().toUpperCase(Locale.ROOT).replaceAll("[^A-Z0-9]", ""), metaItemDescriptor.getName());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    });
    writer.writeIndentedLine("");
    emitEnum(writer, "WellknownRulesetMetaItems", otherMap);
}
Also used : Ruleset(org.openremote.model.rules.Ruleset) java.util(java.util) EmitterExtensionFeatures(cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures) MetaItemDescriptor(org.openremote.model.value.MetaItemDescriptor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Extension(cz.habarta.typescript.generator.Extension) ValueUtil(org.openremote.model.util.ValueUtil) Constants(org.openremote.model.Constants) Settings(cz.habarta.typescript.generator.Settings) TsModel(cz.habarta.typescript.generator.emitter.TsModel) AgentModelProvider(org.openremote.agent.protocol.AgentModelProvider) Ruleset(org.openremote.model.rules.Ruleset) Constants(org.openremote.model.Constants) MetaItemDescriptor(org.openremote.model.value.MetaItemDescriptor)

Example 3 with Settings

use of cz.habarta.typescript.generator.Settings in project typescript-generator by vojtechhabarta.

the class BeanPropertyPathExtensionTest method basicTest.

@Test
public void basicTest() throws Exception {
    final StringBuilder data = new StringBuilder();
    final EmitterExtension.Writer writer = new EmitterExtension.Writer() {

        @Override
        public void writeIndentedLine(String line) {
            data.append(line + "\n");
        }
    };
    final Settings settings = new Settings();
    settings.sortDeclarations = true;
    final TypeProcessor typeProcessor = new DefaultTypeProcessor();
    final Model model = new Jackson2Parser(settings, typeProcessor).parseModel(ClassA.class);
    final TsModel tsModel = new ModelCompiler(settings, typeProcessor).javaToTypeScript(model);
    new BeanPropertyPathExtension().emitElements(writer, settings, false, tsModel);
    String dataStr = data.toString();
    final String expected = Utils.readString(getClass().getResourceAsStream("/ext/expected.ts"), "\n");
    Assert.assertEquals(expected.trim(), dataStr.trim());
}
Also used : Jackson2Parser(cz.habarta.typescript.generator.parser.Jackson2Parser) EmitterExtension(cz.habarta.typescript.generator.emitter.EmitterExtension) DefaultTypeProcessor(cz.habarta.typescript.generator.DefaultTypeProcessor) TsModel(cz.habarta.typescript.generator.emitter.TsModel) ModelCompiler(cz.habarta.typescript.generator.compiler.ModelCompiler) TypeProcessor(cz.habarta.typescript.generator.TypeProcessor) DefaultTypeProcessor(cz.habarta.typescript.generator.DefaultTypeProcessor) Model(cz.habarta.typescript.generator.parser.Model) TsModel(cz.habarta.typescript.generator.emitter.TsModel) Settings(cz.habarta.typescript.generator.Settings) Test(org.junit.Test)

Aggregations

Settings (cz.habarta.typescript.generator.Settings)3 TsModel (cz.habarta.typescript.generator.emitter.TsModel)2 Test (org.junit.Test)2 DefaultTypeProcessor (cz.habarta.typescript.generator.DefaultTypeProcessor)1 Extension (cz.habarta.typescript.generator.Extension)1 JaxrsApplicationTest (cz.habarta.typescript.generator.JaxrsApplicationTest)1 TypeProcessor (cz.habarta.typescript.generator.TypeProcessor)1 TypeScriptGenerator (cz.habarta.typescript.generator.TypeScriptGenerator)1 ModelCompiler (cz.habarta.typescript.generator.compiler.ModelCompiler)1 EmitterExtension (cz.habarta.typescript.generator.emitter.EmitterExtension)1 EmitterExtensionFeatures (cz.habarta.typescript.generator.emitter.EmitterExtensionFeatures)1 Jackson2Parser (cz.habarta.typescript.generator.parser.Jackson2Parser)1 Model (cz.habarta.typescript.generator.parser.Model)1 java.util (java.util)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AgentModelProvider (org.openremote.agent.protocol.AgentModelProvider)1 Constants (org.openremote.model.Constants)1 Ruleset (org.openremote.model.rules.Ruleset)1 ValueUtil (org.openremote.model.util.ValueUtil)1 MetaItemDescriptor (org.openremote.model.value.MetaItemDescriptor)1