Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tidy up formatting of generated code #57

Merged
merged 1 commit into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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