Skip to content

Commit

Permalink
fix: tidy up formatting of generated code (#57)
Browse files Browse the repository at this point in the history
To pass linting rules
  • Loading branch information
achingbrain authored Aug 4, 2022
1 parent 411c017 commit 387c9e9
Show file tree
Hide file tree
Showing 10 changed files with 432 additions and 217 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ node_modules
# Lock files
package-lock.json
yarn.lock
.clinic
68 changes: 49 additions & 19 deletions packages/protons-benchmark/src/protons/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ export interface Foo {
}

export namespace Foo {
let _codec: Codec<Foo>

export const codec = (): Codec<Foo> => {
return message<Foo>({
1: { name: 'baz', codec: uint32 }
})
if (_codec == null) {
_codec = message<Foo>({
1: { name: 'baz', codec: uint32 }
})
}

return _codec
}

export const encode = (obj: Foo): Uint8ArrayList => {
Expand All @@ -30,10 +36,16 @@ export interface Bar {
}

export namespace Bar {
let _codec: Codec<Bar>

export const codec = (): Codec<Bar> => {
return message<Bar>({
1: { name: 'tmp', codec: Foo.codec() }
})
if (_codec == null) {
_codec = message<Bar>({
1: { name: 'tmp', codec: Foo.codec() }
})
}

return _codec
}

export const encode = (obj: Bar): Uint8ArrayList => {
Expand Down Expand Up @@ -65,10 +77,16 @@ export interface Yo {
}

export namespace Yo {
let _codec: Codec<Yo>

export const codec = (): Codec<Yo> => {
return message<Yo>({
1: { name: 'lol', codec: FOO.codec(), repeats: true }
})
if (_codec == null) {
_codec = message<Yo>({
1: { name: 'lol', codec: FOO.codec(), repeats: true }
})
}

return _codec
}

export const encode = (obj: Yo): Uint8ArrayList => {
Expand All @@ -86,11 +104,17 @@ export interface Lol {
}

export namespace Lol {
let _codec: Codec<Lol>

export const codec = (): Codec<Lol> => {
return message<Lol>({
1: { name: 'lol', codec: string },
2: { name: 'b', codec: Bar.codec() }
})
if (_codec == null) {
_codec = message<Lol>({
1: { name: 'lol', codec: string },
2: { name: 'b', codec: Bar.codec() }
})
}

return _codec
}

export const encode = (obj: Lol): Uint8ArrayList => {
Expand All @@ -110,13 +134,19 @@ export interface Test {
}

export namespace Test {
let _codec: Codec<Test>

export const codec = (): Codec<Test> => {
return message<Test>({
6: { name: 'meh', codec: Lol.codec() },
3: { name: 'hello', codec: uint32 },
1: { name: 'foo', codec: string },
7: { name: 'payload', codec: bytes }
})
if (_codec == null) {
_codec = message<Test>({
6: { name: 'meh', codec: Lol.codec() },
3: { name: 'hello', codec: uint32 },
1: { name: 'foo', codec: string },
7: { name: 'payload', codec: bytes }
})
}

return _codec
}

export const encode = (obj: Test): Uint8ArrayList => {
Expand Down
46 changes: 25 additions & 21 deletions packages/protons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,34 @@ export interface ${messageDef.name} {
}`
interfaceCodecDef = `
let _codec: Codec<${messageDef.name}>
export const codec = (): Codec<${messageDef.name}> => {
if (!_codec) _codec = message<${messageDef.name}>({
${Object.entries(fields)
.map(([name, fieldDef]) => {
let codec = encoders[fieldDef.type]
if (codec == null) {
const def = findDef(fieldDef.type, messageDef, moduleDef)
if (isEnumDef(def)) {
moduleDef.imports.add('enumeration')
} else {
moduleDef.imports.add('message')
}
const typeName = findTypeName(fieldDef.type, messageDef, moduleDef)
codec = `${typeName}.codec()`
} else {
moduleDef.imports.add(codec)
}
if (_codec == null) {
_codec = message<${messageDef.name}>({
${Object.entries(fields)
.map(([name, fieldDef]) => {
let codec = encoders[fieldDef.type]
if (codec == null) {
const def = findDef(fieldDef.type, messageDef, moduleDef)
if (isEnumDef(def)) {
moduleDef.imports.add('enumeration')
} else {
moduleDef.imports.add('message')
}
const typeName = findTypeName(fieldDef.type, messageDef, moduleDef)
codec = `${typeName}.codec()`
} else {
moduleDef.imports.add(codec)
}
return `${fieldDef.id}: { name: '${name}', codec: ${codec}${fieldDef.options?.proto3_optional === true ? ', optional: true' : ''}${fieldDef.rule === 'repeated' ? ', repeats: true' : ''} }`
}).join(',\n ')}
})
}).join(',\n ')}
})
}
return _codec
}
Expand Down
14 changes: 10 additions & 4 deletions packages/protons/test/fixtures/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ export interface Basic {
}

export namespace Basic {
let _codec: Codec<Basic>

export const codec = (): Codec<Basic> => {
return message<Basic>({
1: { name: 'foo', codec: string, optional: true },
2: { name: 'num', codec: int32 }
})
if (_codec == null) {
_codec = message<Basic>({
1: { name: 'foo', codec: string, optional: true },
2: { name: 'num', codec: int32 }
})
}

return _codec
}

export const encode = (obj: Basic): Uint8ArrayList => {
Expand Down
32 changes: 22 additions & 10 deletions packages/protons/test/fixtures/circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ export namespace CircuitRelay {
}

export namespace Peer {
let _codec: Codec<Peer>

export const codec = (): Codec<Peer> => {
return message<Peer>({
1: { name: 'id', codec: bytes },
2: { name: 'addrs', codec: bytes, repeats: true }
})
if (_codec == null) {
_codec = message<Peer>({
1: { name: 'id', codec: bytes },
2: { name: 'addrs', codec: bytes, repeats: true }
})
}

return _codec
}

export const encode = (obj: Peer): Uint8ArrayList => {
Expand All @@ -99,13 +105,19 @@ export namespace CircuitRelay {
}
}

let _codec: Codec<CircuitRelay>

export const codec = (): Codec<CircuitRelay> => {
return message<CircuitRelay>({
1: { name: 'type', codec: CircuitRelay.Type.codec(), optional: true },
2: { name: 'srcPeer', codec: CircuitRelay.Peer.codec(), optional: true },
3: { name: 'dstPeer', codec: CircuitRelay.Peer.codec(), optional: true },
4: { name: 'code', codec: CircuitRelay.Status.codec(), optional: true }
})
if (_codec == null) {
_codec = message<CircuitRelay>({
1: { name: 'type', codec: CircuitRelay.Type.codec(), optional: true },
2: { name: 'srcPeer', codec: CircuitRelay.Peer.codec(), optional: true },
3: { name: 'dstPeer', codec: CircuitRelay.Peer.codec(), optional: true },
4: { name: 'code', codec: CircuitRelay.Status.codec(), optional: true }
})
}

return _codec
}

export const encode = (obj: CircuitRelay): Uint8ArrayList => {
Expand Down
Loading

0 comments on commit 387c9e9

Please sign in to comment.