diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-14 11:12:24 +0300 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2018-04-14 11:12:24 +0300 |
| commit | 6c2f9a8d4d87cc1ff937089bdab05a26e2925485 (patch) | |
| tree | 0aca4bce0af1747b2411c2b1eb08fd1bd3ece909 | |
| parent | 3ffcfd52e6531788c2e0aa6d4d1f5e8f5658574e (diff) | |
| download | nimpb-6c2f9a8d4d87cc1ff937089bdab05a26e2925485.tar.gz nimpb-6c2f9a8d4d87cc1ff937089bdab05a26e2925485.zip | |
Make ServiceGenerator an object
The object has a number of callbacks, which the service backend can fill to
perform different tasks. Currently there are two callbacks: genImports and
genService. genImports is called once per file and genService once per service.
| -rw-r--r-- | nimpb/compiler/compiler.nim | 2 | ||||
| -rw-r--r-- | nimpb/compiler/generator.nim | 17 |
2 files changed, 13 insertions, 6 deletions
diff --git a/nimpb/compiler/compiler.nim b/nimpb/compiler/compiler.nim index c44ade6..b52a4d6 100644 --- a/nimpb/compiler/compiler.nim +++ b/nimpb/compiler/compiler.nim @@ -5,7 +5,7 @@ import strformat import strutils from generator import processFileDescriptorSet, ServiceGenerator, Service, ServiceMethod -export Service, ServiceMethod +export ServiceGenerator, Service, ServiceMethod const nimpbProtocInfo = gorgeEx("nimble dump nimpb_protoc") diff --git a/nimpb/compiler/generator.nim b/nimpb/compiler/generator.nim index 5c4000a..fde4da0 100644 --- a/nimpb/compiler/generator.nim +++ b/nimpb/compiler/generator.nim @@ -60,7 +60,9 @@ type Proto2 Proto3 - ServiceGenerator* = proc (service: Service): string + ServiceGenerator* = ref object of RootObj + genImports*: proc (): string + genService*: proc (service: Service): string Service* = ref object name*: string @@ -1013,6 +1015,10 @@ proc processFile(fdesc: google_protobuf_FileDescriptorProto, addLine(result.data, "import nimpb/json as nimpb_json") addLine(result.data, "") + if serviceGenerator != nil: + if serviceGenerator.genImports != nil: + add(result.data, serviceGenerator.genImports()) + for dep in fdesc.dependency: var (dir, depname, _) = splitFile(dep) @@ -1046,10 +1052,11 @@ proc processFile(fdesc: google_protobuf_FileDescriptorProto, addLine(result.data, "") if serviceGenerator != nil: - for serviceDesc in fdesc.service: - let service = newService(serviceDesc, parsed) - addLine(result.data, "") - add(result.data, serviceGenerator(service)) + if serviceGenerator.genService != nil: + for serviceDesc in fdesc.service: + let service = newService(serviceDesc, parsed) + addLine(result.data, "") + add(result.data, serviceGenerator.genService(service)) proc processFileDescriptorSet*(filename: string, outdir: string, |
