annotate src/capnproto-0.6.0/doc/capnp-tool.md @ 84:08ae793730bd

Add null config files
author Chris Cannam
date Mon, 02 Mar 2020 14:03:47 +0000
parents 0994c39f1e94
children
rev   line source
cannam@62 1 ---
cannam@62 2 layout: page
cannam@62 3 title: The capnp Tool
cannam@62 4 ---
cannam@62 5
cannam@62 6 # The `capnp` Tool
cannam@62 7
cannam@62 8 Cap'n Proto comes with a command-line tool called `capnp` intended to aid development and
cannam@62 9 debugging. This tool can be used to:
cannam@62 10
cannam@62 11 * Compile Cap'n Proto schemas to produce source code in multiple languages.
cannam@62 12 * Generate unique type IDs.
cannam@62 13 * Decode Cap'n Proto messages to human-readable text.
cannam@62 14 * Encode text representations of Cap'n Proto messages to binary.
cannam@62 15 * Evaluate and extract constants defined in Cap'n Proto schemas.
cannam@62 16
cannam@62 17 This page summarizes the functionality. A complete reference on the command's usage can be
cannam@62 18 found by typing:
cannam@62 19
cannam@62 20 capnp help
cannam@62 21
cannam@62 22 ## Compiling Schemas
cannam@62 23
cannam@62 24 capnp compile -oc++ myschema.capnp
cannam@62 25
cannam@62 26 This generates files `myschema.capnp.h` and `myschema.capnp.c++` which contain C++ source code
cannam@62 27 corresponding to the types defined in `myschema.capnp`. Options exist to control output location
cannam@62 28 and import paths.
cannam@62 29
cannam@62 30 The above example generates C++ code, but the tool is able to generate output in any language
cannam@62 31 for which a plugin is available. Compiler plugins are just regular programs named
cannam@62 32 `capnpc-language`. For example, the above command runs `capnpc-c++`. [More on how to write
cannam@62 33 compiler plugins](otherlang.html#how-to-write-compiler-plugins).
cannam@62 34
cannam@62 35 Note that some Cap'n Proto implementations (especially for interpreted languages) do not require
cannam@62 36 generating source code.
cannam@62 37
cannam@62 38 ## Decoding Messages
cannam@62 39
cannam@62 40 capnp decode myschema.capnp MyType < message.bin > message.txt
cannam@62 41
cannam@62 42 `capnp decode` reads a binary Cap'n Proto message from standard input and decodes it to a
cannam@62 43 human-readable text format (specifically, the format used for specifying constants and default
cannam@62 44 values in [the schema language](language.html)). By default it
cannam@62 45 expects an unpacked message, but you can decode a
cannam@62 46 [packed](encoding.html#packing) message with the `--packed` flag.
cannam@62 47
cannam@62 48 ## Encoding Messages
cannam@62 49
cannam@62 50 capnp encode myschema.capnp MyType < message.txt > message.bin
cannam@62 51
cannam@62 52 `capnp encode` is the opposite of `capnp decode`: it takes a text-format message on stdin and
cannam@62 53 encodes it to binary (possibly [packed](encoding.html#packing),
cannam@62 54 with the `--packed` flag).
cannam@62 55
cannam@62 56 This is mainly useful for debugging purposes, to build test data or to apply tweaks to data
cannam@62 57 decoded with `capnp decode`. You should not rely on `capnp encode` for encoding data written
cannam@62 58 and maintained in text format long-term -- instead, use `capnp eval`, which is much more powerful.
cannam@62 59
cannam@62 60 ## Evaluating Constants
cannam@62 61
cannam@62 62 capnp eval myschema.capnp myConstant
cannam@62 63
cannam@62 64 This prints the value of `myConstant`, a [const](language.html#constants) declaration, after
cannam@62 65 applying variable substitution. It can also output the value in binary format (`--binary` or
cannam@62 66 `--packed`).
cannam@62 67
cannam@62 68 At first glance, this may seem no more interesting that `capnp encode`: the syntax used to define
cannam@62 69 constants in schema files is the same as the format accepted by `capnp encode`, right? There is,
cannam@62 70 however, a big difference: constants in schema files may be defined in terms of other constants,
cannam@62 71 which may even be imported from other files.
cannam@62 72
cannam@62 73 As a result, `capnp eval` is a great basis for implementing config files. For example, a large
cannam@62 74 company might maintain a production server that serves dozens of clients and needs configuration
cannam@62 75 information about each one. Rather than maintaining the config as one enormous file, it can be
cannam@62 76 written as several separate files with a master file that imports the rest.
cannam@62 77
cannam@62 78 Such a configuration should be compiled to binary format using `capnp eval` before deployment,
cannam@62 79 in order to verify that there are no errors and to make deployment easier and faster. While you
cannam@62 80 could technically ship the text configs to production and have the servers parse them directly
cannam@62 81 (e.g. with `capnp::SchemaParser`), encoding before deployment is more efficient and robust.