Skip to content

Commit bc7b76f

Browse files
authored
Merge pull request #22 from konsumer/uipolish
Ui polish
2 parents f13e191 + 8bc88ac commit bc7b76f

File tree

9 files changed

+55
-23
lines changed

9 files changed

+55
-23
lines changed

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,12 @@ export class ReaderMessage {
384384
return query(this, this.path, ...queries)
385385
}
386386

387-
toJS (queryMap = {}, prefix = 'f') {
388-
return toJS(this, queryMap, prefix)
387+
toJS (queryMap = {}, prefix = 'f', nameMap, typeMap) {
388+
return toJS(this, queryMap, prefix, nameMap, typeMap)
389389
}
390390

391-
toProto (queryMap = {}, prefix = 'f') {
392-
return toProto(this, queryMap, prefix, queryMap)
391+
toProto (queryMap = {}, prefix = 'f', nameMap, typeMap, messageName = 'MessageRoot') {
392+
return toProto(this, queryMap, prefix, nameMap, typeMap, messageName)
393393
}
394394
}
395395

test/hearthstone.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// tests that show how to actually parse some real protobuf, in a practical sense
1+
// Show how to actually parse some real protobuf, in a practical sense
22

33
/* global describe test expect */
44

test/json.test.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// JSON-safe values & correct output for toJS
2+
13
/* global test, expect, describe */
24

35
import { fileURLToPath } from 'url'
@@ -46,24 +48,26 @@ const simplemap = {
4648

4749
describe('Simple', () => {
4850
test('Get JSON from binary proto', () => {
49-
// TODO: commented need work
50-
const j = new RawProto(bytes).toJS(simplemap)
51+
const j = JSON.parse(JSON.stringify(new RawProto(bytes).toJS(simplemap)))
5152
expect(j.double).toEqual([150])
5253
expect(j.float).toEqual([150])
5354
expect(j.int32).toEqual([150])
5455
expect(j.int64).toEqual([150])
5556
expect(j.uint32).toEqual([150])
5657
expect(j.uint64).toEqual([150])
57-
// expect(j.sint32).toEqual([150]) // 300
58-
// expect(j.sint64).toEqual([150]) // 300
5958
expect(j.fixed32).toEqual([150])
6059
expect(j.fixed64).toEqual([150])
6160
expect(j.sfixed32).toEqual([150])
6261
expect(j.sfixed64).toEqual([150])
6362
expect(j.bool).toEqual([true])
6463
expect(j.string).toEqual(['testing'])
65-
expect(j.bytes).toEqual([new Uint8Array([116, 101, 115, 116, 105, 110, 103])])
64+
expect(j.bytes).toEqual([{ 0: 116, 1: 101, 2: 115, 3: 116, 4: 105, 5: 110, 6: 103 }])
6665
expect(j.sub.enum).toEqual([2])
66+
67+
// TODO: these are broke
68+
69+
// expect(j.sint32).toEqual([150]) // 300
70+
// expect(j.sint64).toEqual([150]) // 300
6771
// expect(j.sub.packedvarint).toEqual([[0, 1, 2, 3]])
6872
// expect(j.sub.packedint32).toEqual([[0, 1, 2, 3]])
6973
// expect(j.sub.packedint64).toEqual([[0, 1, 2, 3]])
@@ -75,12 +79,12 @@ describe('Simple', () => {
7579

7680
describe('Hearthstone', () => {
7781
test('Get JSON from binary proto', () => {
78-
const j = appTree.toJS({
82+
const j = JSON.parse(JSON.stringify(appTree.toJS({
7983
id: '1.2.4.1:string',
8084
title: '1.2.4.5:string',
8185
company: '1.2.4.6:string',
8286
description: '1.2.4.7:string'
83-
})
87+
})))
8488
expect(j.id).toEqual(['com.blizzard.wtcg.hearthstone'])
8589
expect(j.title).toEqual(['Hearthstone'])
8690
expect(j.company).toEqual(['Blizzard Entertainment, Inc.'])

test/parsing.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Low-level parsing, and starting with different buffer-like objects
2+
13
/* global test, expect, describe */
24

35
import RawProto, { wireTypes, hex } from 'rawproto'

test/proto.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Protobuf SDL generation from toProto
2+
13
/* global test, expect, describe */
24

35
import { fileURLToPath } from 'url'

ui/src/App.jsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import ProtoDisplay from './ProtoDisplay.jsx'
55
import ErrorBoundary from './ErrorBoundary.jsx'
66
import ButtonHex from './ButtonHex.jsx'
77
import ButtonFieldType from './ButtonFieldType.jsx'
8+
import ButtonDownloadJSON from './ButtonDownloadJSON.jsx'
9+
import ButtonDownloadProto from './ButtonDownloadProto.jsx'
810

911
function App () {
1012
const [fields, setFields] = useState()
1113
const [nameMap, setNameMap] = useState({})
1214
const [typeMap, setTypeMap] = useState({})
1315

14-
1516
const handleFileChange = async (e) => {
1617
const file = e.target.files[0]
1718
if (file) {
@@ -43,16 +44,20 @@ function App () {
4344
<div>
4445
<input type='file' className='mb-2 w-full file-input' onChange={handleFileChange} />
4546
</div>
47+
{!!fields && (<ButtonDownloadJSON tree={fields} typeMap={typeMap} nameMap={nameMap} />)}
48+
{!!fields && (<ButtonDownloadProto tree={fields} typeMap={typeMap} nameMap={nameMap} />)}
4649
</div>
4750

48-
<ErrorBoundary>
49-
<ul className='w-full menu bg-base-200 rounded-box'>
50-
<li>
51-
<ProtoDisplay tree={fields} open typeMap={typeMap} nameMap={nameMap} />
52-
</li>
53-
</ul>
51+
{!!fields && (
52+
<ErrorBoundary>
53+
<ul className='w-full menu bg-base-200 rounded-box'>
54+
<li>
55+
<ProtoDisplay tree={fields} open typeMap={typeMap} nameMap={nameMap} />
56+
</li>
57+
</ul>
5458

55-
</ErrorBoundary>
59+
</ErrorBoundary>
60+
)}
5661
</main>
5762
<footer className='p-5 text-center bg-neutral text-neutral-content'>
5863
<p>

ui/src/ButtonDownloadJSON.jsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function ButtonDownloadJSON ({ tree, prefix = 'f', typeMap, nameMap, children = 'Download JSON', className = 'btn btn-primary', filename = 'download.json' }) {
2+
const handleClick = e => {
3+
const d = tree.toJS(undefined, prefix, nameMap, typeMap)
4+
console.log({ tree, typeMap, nameMap, json: d })
5+
const u = URL.createObjectURL(new Blob([JSON.stringify(d, null, 2)], { type: 'application/json' }))
6+
e.target.setAttribute('href', u)
7+
setTimeout(() => URL.revokeObjectURL(u), 0)
8+
}
9+
10+
return <a className={className} onClick={handleClick} href='#proto' download={filename}>{children}</a>
11+
}

ui/src/ButtonDownloadProto.jsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function ButtonDownloadProto ({ tree, prefix = 'f', typeMap, nameMap, messageName = 'MessageRoot', children = 'Download Proto', className = 'btn btn-secondary', filename = 'download.proto' }) {
2+
const handleClick = e => {
3+
const d = tree.toProto(undefined, prefix, nameMap, typeMap, messageName)
4+
const u = URL.createObjectURL(new Blob([d], { type: 'text/plain' }))
5+
e.target.setAttribute('href', u)
6+
setTimeout(() => URL.revokeObjectURL(u), 0)
7+
}
8+
9+
return <a className={className} onClick={handleClick} href='#proto' download={filename}>{children}</a>
10+
}

ui/src/ProtoDisplay.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,15 @@ badgeColors[wireTypes.I64] = 'accent'
2323
badgeColors[wireTypes.I32] = 'secondary'
2424
badgeColors[wireTypes.SGROUP] = 'primary'
2525

26-
2726
const hex = (b) => [...b].map((c) => c.toString(16).padStart(2, '0')).join(' ')
2827

2928
export default function ProtoDisplay ({ open = false, tree, typeMap = {}, nameMap = {}, className }) {
30-
const [o, setO] = useState(open)
3129
if (tree) {
3230
tree.renderType = 'sub'
3331
}
3432
return tree
3533
? (
36-
<details open={o} className={className}>
34+
<details className={className} open={open}>
3735
<summary>
3836
<div className={`badge badge-${badgeColors[tree.type]} gap-2`}>{tree.name || 'root (0)'}</div>
3937
<p className='text-gray-500 italic'>{parseLabels[tree.renderType]}</p>

0 commit comments

Comments
 (0)