-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·142 lines (127 loc) · 3.26 KB
/
index.js
File metadata and controls
executable file
·142 lines (127 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env node
const argv = require('optimist').argv;
const _ = require('lodash');
const util = require('util')
const primitives = require('./lib/primitives');
var config;
function getAllTypes(datum) {
var allTypes = _.uniq(_.map(datum, function (element, key) {
var s = generateSchema(element, key);
if (isPrimitiveAvro(s.type)) {
return s.type;
} else {
return s;
}
}), false, uniqTransformer);
return allTypes;
}
function isPrimitiveAvro(avroType) {
return _.contains(['int', 'string', 'enum', 'float', 'null', 'boolean'], avroType);
}
function uniqTransformer(el) {
delete el.name;
return JSON.stringify(el);
}
function getAllSchemas(datum) {
return _.map(datum, generateSchema);
}
function generateObjectSchema(datum, key) {
var allTypes = getAllTypes(datum);
console.log('doing object, datum: ', datum, 'key: ', key, 'data types:', allTypes);
if (allTypes.length === 1) {
//map
return {
name: key,
type: {
type: 'map',
items: _.first(allTypes)
}
};
} else {
//record
return {
name: key,
type: 'record',
fields: getAllSchemas(datum)
};
}
}
function generateArraySchema(datum, key) {
var allTypes = getAllTypes(datum);
if (allTypes.length === 1) {
allTypes = _.first(allTypes);
}
return {
name: key,
type: {
type: 'array',
items: allTypes
}
};
}
function getAvroType(jsType, datum, key) {
var avroType = primitives.JAVASCRIPT_TO_AVRO_TYPE[jsType];
if (_.isFunction(avroType)) {
avroType = avroType(datum, key);
}
return avroType;
}
function generateSchema(datum, key) {
console.log('generateSchema: ', datum, key);
var type = typeof datum;
if (primitives.isPrimitive(type)) {
var avroType = getAvroType(type, datum, key);
return {
name: key,
type: avroType,
doc: ''
};
} else {
if (Array.isArray(datum)) {
console.log('doing array');
return generateArraySchema(datum, key);
} else {
//map or record
console.log('doing object');
return generateObjectSchema(datum, key);
}
}
}
function generate(data) {
// TODO: Add the generation code.
var result = _.map(data, function(jsons, namespacePrefix) {
// TODO: Combine schemas from multiple samples.
return _.map(jsons, function(json) {
// TODO: Write these schemas out in the output directory.
return generateSchema(json, namespacePrefix);
});
});
console.log('Data: ', JSON.stringify(result, undefined, 2));
}
function main() {
try {
config = require('./configs/' + argv.config);
} catch (e) {
console.log('Incorrect config option.',
' Please supply valid filename from config/ folder using --config option.');
return -1;
}
if (_.some(['fetcher', 'outputDirectory'], function(key) { return !_.has(config, key);})) {
console.log('Some fields are absent from the config.');
return -1;
}
var fetcher;
try {
fetcher = require(config.fetcher);
} catch (e) {
console.log('Could not require ', config.fetcher);
return -1;
}
primitives.configure(config);
var dataPromise = fetcher.fetch();
dataPromise.then(generate).fail(console.log);
}
if (require.main === module) {
main();
}
module.exports.generate = generate