Skip to content

Commit 3e5abe8

Browse files
committed
[ISSUE#51]: getschema done
igned-off-by: ashish <ashishpatel0720@gmail.com> Signed-off-by: ashish <ashishpatel0720@gmail.com>
1 parent 229d610 commit 3e5abe8

File tree

5 files changed

+99
-20
lines changed

5 files changed

+99
-20
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ OPTIONS
187187
_See code: [src/commands/minify.ts](https://github.com/codingtools/cdt/blob/v0.1.5/src/commands/minify.ts)_
188188
<!-- commandsstop -->
189189

190+
## Acknowledgement
191+
* this cli uses following opensource libraries/services
192+
* [bundlephobia](https://bundlephobia.com/)
193+
* [avro-js](https://openbase.io/js/avro-js)
194+
* [avsc](https://github.com/mtth/avsc)
195+
196+
And many others, great thanks to all the people involved in developnment and support :)
190197

191198
## Contribution
192199

my.json

Whitespace-only changes.

src/commands/avro.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {Command, flags} from '@oclif/command'
2-
import * as avsc from 'avsc'
3-
import * as CryptoJS from 'crypto-js'
2+
import * as avro from 'avsc' // includes all from avro-js and some more
43

54
import Logger from '../utilities/logger'
65
import Utilities from '../utilities/utilities'
76

87
export default class Avro extends Command {
98
static description = 'Avro Utility command'
9+
static SupportedCommands = ['get_schema', 'to_json', 'to_avro']
1010
static flags = {
1111
help: flags.help({char: 'h'}),
1212
file: flags.string({char: 'f' , description: 'input file path'}),
@@ -16,36 +16,58 @@ export default class Avro extends Command {
1616
}
1717

1818
static args = [{name: 'command'}] // operation type
19-
19+
/*
20+
* input,output, and operation are all must
21+
* */
2022
async run() {
2123
const {args, flags} = this.parse(Avro)
2224

23-
args.string = Utilities.getInputString(this, flags, args) // from either -s,-f or args
2425
this.checkParameters(flags, args)
2526
args.commandFunction = this.getCommandCaller(args)
27+
args.commandFunction(flags, args)
2628
}
2729

2830
// to check required parameters passed or not
2931
private checkParameters(flags: any, args: any) {
30-
if (args.string === undefined || args.string === '')
31-
Logger.error(this, 'Input is empty or not provided')
32+
if (flags.file === undefined || flags.file === '')
33+
Logger.error(this, 'Input file is not provided')
34+
if (flags.output === undefined || args.output === '')
35+
Logger.error(this, 'Output file is not provided')
3236
if (args.command === undefined || args.command === '')
33-
Logger.error(this, 'command is empty or not provided')
37+
Logger.error(this, 'Command is empty or not provided, supported:' + Avro.SupportedCommands)
3438
}
3539

3640
private getCommandCaller(args: any) {
37-
let supportedCommands = ['get_schema', 'to_json', 'to_avro']
38-
3941
switch (args.command.toLowerCase()) {
40-
case supportedCommands[0]:
41-
return 'supported'
42-
case supportedCommands[1]:
43-
return 'supported'
44-
case supportedCommands[2]:
45-
return 'supported'
42+
case Avro.SupportedCommands[0]:
43+
return this.getSchema
44+
case Avro.SupportedCommands[1]:
45+
return this.toJson
46+
case Avro.SupportedCommands[2]:
47+
return this.toAvro
4648
default:
47-
Logger.error(this, 'Unsupported Commands Mode, supported: ' + supportedCommands)
49+
Logger.error(this, 'Unsupported Command, supported: ' + Avro.SupportedCommands)
4850
}
4951
}
5052

53+
private getSchema(flags: any, args: any) {
54+
avro.createFileDecoder(flags.file)
55+
.on('metadata', function (type) {
56+
let output = type.schema()
57+
let schemaStr = JSON.stringify(output, null, '\t')
58+
Utilities.writeStringToFile(this, flags.output, schemaStr)
59+
})
60+
61+
}
62+
private toJson(flags: any, args: any) {
63+
avro.createFileDecoder(flags.file)
64+
.on('data', function (data) {
65+
return data
66+
})
67+
68+
}
69+
private toAvro(flags: any, args: any) {
70+
71+
}
72+
5173
}

test/commands/avro.test.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,43 @@ import {expect, test} from '@oclif/test'
33
// TODO: add test for invalid package
44
// test for valid with matching
55
describe('avro', () => {
6+
//todo if file is invalid
67
test
78
.stdout()
89
.command(['avro'])
910
.exit(0)
10-
.it('if nothing passed', ctx => {
11-
expect(ctx.stdout).to.contain('Input is empty or not provided')
11+
.it('if input is not passed', ctx => {
12+
expect(ctx.stdout).to.contain('Input file is not provided')
1213
})
13-
1414
test
1515
.stdout()
1616
.command(['avro', '-f' ,'test/resources/avro/test-tabular.avro'])
1717
.exit(0)
18+
.it('if output is not passed', ctx => {
19+
expect(ctx.stdout).to.contain('Output file is not provided')
20+
})
21+
22+
test
23+
.stdout()
24+
.command(['avro', '-f' ,'test/resources/avro/test-tabular.avro', '-o', 'output_file.example'])
25+
.exit(0)
1826
.it('if command not passed', ctx => {
19-
expect(ctx.stdout).to.contain('command is empty or not provided')
27+
expect(ctx.stdout).to.contain('Command is empty or not provided')
28+
})
29+
30+
test
31+
.stdout()
32+
.command(['avro', '-f' ,'test/resources/avro/test-tabular.avro', '-o', 'output_file.example', 'unsupported_command'])
33+
.exit(0)
34+
.it('if command is invalid', ctx => {
35+
expect(ctx.stdout).to.contain('Unsupported Command')
36+
})
37+
38+
test
39+
.stdout()
40+
.command(['avro', '-f' ,'test/resources/avro/file_not_exists.avro', '-o', 'output_file.example', 'get_schema'])
41+
.exit(0)
42+
.it('if input file path is invalid', ctx => {
43+
expect(ctx.stdout).to.contain('Unsupported Command')
2044
})
2145
})

test/resources/avro/schema.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "Person",
3+
"type": "record",
4+
"fields": [
5+
{
6+
"name": "ID",
7+
"type": "long"
8+
},
9+
{
10+
"name": "First",
11+
"type": "string"
12+
},
13+
{
14+
"name": "Last",
15+
"type": "string"
16+
},
17+
{
18+
"name": "Phone",
19+
"type": "string"
20+
},
21+
{
22+
"name": "Age",
23+
"type": "int"
24+
}
25+
]
26+
}

0 commit comments

Comments
 (0)