Skip to content

Commit 0188112

Browse files
authored
fix: generate correct fields using both type and format part from data type formats (#70)
1 parent 2ce975f commit 0188112

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

filters/all.js

+42-29
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,43 @@ const SPRING_CLOUD_STREAM_VERSION = '3.0.7.RELEASE';
1414
const SOLACE_HOST = 'tcp://localhost:55555';
1515
const SOLACE_DEFAULT = 'default';
1616

17-
// This maps json schema types to Java format strings.
18-
const formatMap = new Map();
19-
formatMap.set('boolean', '%s');
20-
formatMap.set('enum', '%s');
21-
formatMap.set('integer', '%d');
22-
formatMap.set('number', '%f');
23-
formatMap.set('null', '%s');
24-
formatMap.set('string', '%s');
25-
26-
// This maps json schema types to examples of values.
27-
const sampleMap = new Map();
28-
sampleMap.set('boolean', 'true');
29-
sampleMap.set('integer', '1');
30-
sampleMap.set('null', 'string');
31-
sampleMap.set('number', '1.1');
32-
sampleMap.set('string', '"string"');
33-
34-
// This maps json schema types to Java types.
17+
const stringMap = new Map();
18+
stringMap.set('date',{javaType: 'java.time.LocalDate', printFormat: '%s', sample: '2000-12-31'});
19+
stringMap.set('date-time',{javaType: 'java.time.OffsetDateTime', printFormat: '%s', sample: '2000-12-31T23:59:59+01:00'});
20+
stringMap.set('byte',{javaType: 'byte[]', printFormat: '%s', sample: 'U3dhZ2dlciByb2Nrcw=='});
21+
stringMap.set('binary',{javaType: 'byte[]', printFormat: '%s', sample: 'base64-encoded file contents'});
22+
stringMap.set(undefined,{javaType: 'String', printFormat: '%s', sample: '"string"'});
23+
24+
const integerMap = new Map();
25+
integerMap.set('int32',{javaType: 'Integer', printFormat: '%d', sample: '1'});
26+
integerMap.set('int64',{javaType: 'Long', printFormat: '%d', sample: '1L'});
27+
integerMap.set(undefined,{javaType: 'Integer', printFormat: '%d', sample: '1'});
28+
29+
const numberMap = new Map();
30+
numberMap.set('float',{javaType: 'Float', printFormat: '%f', sample: '1.1F'});
31+
numberMap.set('double',{javaType: 'Double', printFormat: '%f', sample: '1.1'});
32+
numberMap.set(undefined,{javaType: 'java.math.BigDecimal', printFormat: '%s', sample: '100.1'});
33+
34+
const booleanMap = new Map();
35+
booleanMap.set(undefined,{javaType: 'Boolean', printFormat: '%s', sample: 'true'});
36+
37+
const nullMap = new Map();
38+
nullMap.set(undefined,{javaType: 'String', printFormat: '%s', sample: 'null'});
39+
3540
const typeMap = new Map();
36-
typeMap.set('boolean', 'Boolean');
37-
typeMap.set('integer', 'Integer');
38-
typeMap.set('null', 'String');
39-
typeMap.set('number', 'Double');
40-
typeMap.set('string', 'String');
41+
typeMap.set('boolean', booleanMap);
42+
typeMap.set('integer', integerMap);
43+
typeMap.set('null', nullMap);
44+
typeMap.set('number', numberMap);
45+
typeMap.set('string', stringMap);
46+
47+
function getType(type, format) {
48+
let typeObject = typeMap.get(type).get(format);
49+
if (typeObject === undefined) {
50+
typeObject = typeMap.get(type).get(undefined);
51+
}
52+
return typeObject;
53+
}
4154

4255
class SCSFunction {
4356
get publishBindingName() {
@@ -210,7 +223,6 @@ function indent3(numTabs) {
210223
return indent(numTabs + 2);
211224
}
212225
filter.indent3 = indent3;
213-
214226
// This returns the proper Java type for a schema property.
215227
function fixType([name, javaName, property]) {
216228
//console.log('fixType: ' + name + " " + dump(property));
@@ -220,11 +232,12 @@ function fixType([name, javaName, property]) {
220232
// For message headers, type is a property.
221233
// For schema properties, type is a function.
222234
let type = property.type;
223-
235+
let format = property.format;
224236
//console.log("fixType: " + property);
225237

226238
if (typeof type === 'function') {
227239
type = property.type();
240+
format = property.format();
228241
}
229242

230243
//console.log(`fixType: type: ${type} javaNamne ${javaName}` );
@@ -272,7 +285,7 @@ function fixType([name, javaName, property]) {
272285
//console.log("It's an enum.");
273286
typeName = _.upperFirst(javaName);
274287
} else {
275-
typeName = typeMap.get(type);
288+
typeName = getType(type,format).javaType;
276289
if (!typeName) {
277290
typeName = type;
278291
}
@@ -659,7 +672,7 @@ function getTopicInfo(channelName, channel) {
659672
const nameWithBrackets = `{${ name }}`;
660673
const parameter = channel.parameter(name);
661674
const schema = parameter.schema();
662-
const type = schema.type();
675+
const type = getType(schema.type(), schema.format());
663676
const param = { name: _.lowerFirst(name) };
664677
//console.log("name: " + name + " type: " + type);
665678
let sampleArg = 1;
@@ -678,12 +691,12 @@ function getTopicInfo(channelName, channel) {
678691
const javaType = typeMap.get(type);
679692
if (!javaType) throw new Error(`topicInfo filter: type not found in typeMap: ${ type}`);
680693
param.type = javaType;
681-
const printfArg = formatMap.get(type);
694+
const printfArg = type.printFormat;
682695
//console.log("printf: " + printfArg);
683696
if (!printfArg) throw new Error(`topicInfo filter: type not found in formatMap: ${ type}`);
684697
//console.log("Replacing " + nameWithBrackets);
685698
publishTopic = publishTopic.replace(nameWithBrackets, printfArg);
686-
sampleArg = sampleMap.get(type);
699+
sampleArg = type.sample;
687700
} else {
688701
const en = schema.enum();
689702
if (en) {

0 commit comments

Comments
 (0)