arg() = term() | named_arg()
arg_template() = [arg()]
bin_ret() = {ok, module(), code()} | {ok, module(), code(), [warning()]}
boolean_opt(X) = X | {X, boolean()}
Just an option X
means {X,true}
code() = binary() | gpb_parse:defs() | [code_item()]
code_item() = {erl, ErlCode :: binary()} | {nif, NifCcText :: string()}
comp_ret() = mod_ret() | bin_ret() | error_ret()
directory() = string()
error_ret() = error | {error, reason()} | {error, reason(), [warning()]}
fetcher_ret() = from_file | {ok, string()} | {error, term()}
fn_name() = atom()
gpb_field_type() = int32 | int64 | uint32 | uint64 | sint32 | sint64 | fixed32 | fixed64 | sfixed32 | sfixed64 | bool | float | double | string | bytes | {enum, atom()} | {msg, atom()} | {group, atom()} | {map, gpb_map_key(), gpb_map_value()}
gpb_map_key() = int32 | int64 | uint32 | uint64 | sint32 | sint64 | fixed32 | fixed64 | sfixed32 | sfixed64 | bool | string
gpb_map_value() = gpb_scalar() | {enum, atom()} | {msg, atom()}
gpb_scalar() = int32 | int64 | uint32 | uint64 | sint32 | sint64 | fixed32 | fixed64 | sfixed32 | sfixed64 | bool | float | double | string | bytes
import_fetcher_fun() = fun((string()) -> fetcher_ret())
mod_fn_argtemplate() = {module(), fn_name(), arg_template()}
mod_ret() = ok | {ok, [warning()]}
msg_name_change() = name_change() | {prefix, {by_proto, prefix_by_proto()}}
name_change() = {prefix, string() | atom()} | {suffix, string() | atom()} | lowercase | snake_case | dots_to_underscores | base_name
named_arg() = '$1' | '$2' | '$errorf' | '$user_data' | '$op'
opt() = type_specs | {type_specs, boolean()} | {verify, optionally | always | never} | {copy_bytes, true | false | auto | integer() | float()} | {strings_as_binaries, boolean()} | strings_as_binaries | boolean_opt(defs_as_proplists) | boolean_opt(descriptor) | boolean_opt(maps) | boolean_opt(msgs_as_maps) | boolean_opt(mapfields_as_maps) | boolean_opt(defs_as_maps) | {maps_unset_optional, omitted | present_undefined} | {maps_oneof, tuples | flat} | boolean_opt(nif) | {load_nif, string()} | {i, directory()} | {o, directory()} | {o_erl, directory()} | {o_hrl, directory()} | {o_nif_cc, directory()} | binary | to_proto_defs | to_msg_defs | return | boolean_opt(return_warnings) | boolean_opt(return_errors) | report | boolean_opt(report_warnings) | boolean_opt(report_errors) | boolean_opt(warnings_as_errors) | boolean_opt(include_as_lib) | boolean_opt(use_packages) | {erlc_compile_options, string()} | {rename, renaming()} | {msg_name_prefix, string() | atom() | {by_proto, prefix_by_proto()}} | {msg_name_suffix, string() | atom()} | boolean_opt(msg_name_to_snake_case) | boolean_opt(msg_name_to_lower) | {module_name_prefix, string() | atom()} | {module_name_suffix, string() | atom()} | {module_name, string() | atom()} | {translate_type, {gpb_field_type(), [translation()]}} | {any_translate, [translation()]} | boolean_opt(epb_compatibility) | boolean_opt(epb_functions) | boolean_opt(defaults_for_omitted_optionals) | boolean_opt(type_defaults_for_omitted_optionals) | {import_fetcher, import_fetcher_fun()} | {target_erlang_version, integer() | current} | term()
opts() = [opt()]
prefix_by_proto() = [{ProtoName :: atom(), Prefix :: string() | atom()}]
reason() = term()
renaming() = {pkg_name, name_change()} | {msg_name, msg_name_change()} | {msg_fqname, msg_name_change()} | {group_name, name_change()} | {group_fqname, name_change()} | {service_name, name_change()} | {service_fqname, name_change()} | {rpc_name, name_change()}
translation() = {encode, mod_fn_argtemplate()} | {decode, mod_fn_argtemplate()} | {merge, mod_fn_argtemplate()} | {verify, mod_fn_argtemplate()}
warning() = term()
c/0 | Command line interface for the compiler. |
c/1 | This function is intended as a command line interface for the compiler. |
c/2 | |
file/1 | Equivalent to file(File, []). |
file/2 | Compile a .proto file to a .erl file and to a .hrl file. |
format_error/1 | Produce a plain-text error message from a reason returned by
for instance file/2 or proto_defs/2 . |
format_warning/1 | Produce a plain-text error message from a reason returned by
for instance file/2 or proto_defs/2 . |
locate_import/2 | Locate an import target. |
msg_defs/2 | Deprecated, use proto_defs/2 instead. |
msg_defs/3 | Deprecated, use proto_defs/2 instead. |
parse_opts_and_args/1 | |
proto_defs/2 | Equivalent to proto_defs(Mod, Defs, []). |
proto_defs/3 | Compile a list of pre-parsed definitions to file or to a binary. |
read_import/2 | Read an import file. |
show_args/0 | |
show_version/0 | |
string/2 | Equivalent to string(Mod, Str, []). |
string/3 |
Compile a .proto file as string. |
c() -> no_return()
Command line interface for the compiler. With no proto file to compile, print a help message and exit.
c(Files :: [string() | atom()]) -> no_return()
This function is intended as a command line interface for the compiler. Call it from the command line as follows:
erl <erlargs> [gpb-opts] -s gpb_compile c File.proto ... erl <erlargs> -s gpb_compile c File.proto ... -extra [gpb-opts]
The <erlargs>
can be -noshell -noinput +B -boot start_clean -pa SomeDir
-o-erl
and -o_erl
are the same option.
-IDir
-I Dir
-o Dir
-o-erl Dir
| -o-hrl Dir
| -o-nif-cc Dir
-nif
option is specified.
The -o-erl Dir
option overrides any -o Dir
option, and
similarly for the other file-type specific output options.-v optionally | always | never
-nif
-load_nif FunctionDefinition
FunctionDefinition
as the text that defines the
function load_nif/0
. This is called as the on_load
hook for loading the NIF. See also the doc for the load_nif
option in the file/2
function.-c true | false | auto | integer() | float()
bytes
. See the copy_bytes
option
for the function file/2
for more info.-strbin
-pldefs
#field{}
records, to make the generated code
completely free of even compile-time dependencies to gpb.-pkgs
-translate_type WMsFs
TMsFs
to pack, unpack, merge and verify
for specifed submessages. The TMsFs
is a string on the
following format: type=Type,e=Mod:Fn,d=Mod:Fn[,m=Mod:Fn][,V=Mod:Fn]
.
The Type and specified modules and functions are called and used
as follows:
type=msg:MsgName
MsgName
(after any renaming operations).e=Mod:Fn
Mod:Fn(Term)
to pack the Term
to
a google.protobuf.Any
message.d=Mod:Fn
Mod:Fn(Any)
to unpack the Any
to
unpack a google.protobuf.Any
message to a term.m=Mod:Fn
Mod:Fn(Term1, Term2) -> Term3
to merge two
unpacked terms to a resulting Term3.V=Mod:Fn
Mod:Fn(Term) -> _
to verify an unpacked Term
.
If Term
is valid, the function is expected to just return
any value, which is ignored and discarded.
If Term
is invalid, the function is exptected to not
return anything, but instead either crash, call
erlang:error/1
, or throw/1
or exit/1
. with the
reason for error.
If you want to use a verifier, this is the new preferred
approach.v=Mod:Fn
Mod:Fn(Term, ErrorF) -> _
to verify an unpacked Term
.
This exists for backwards compatibility, and its use
is deprecated.-any_translate MsFs
MsFs
to pack, unpack, merge and verify
google.protobuf.Any
messages. The MsFs
is a string on the
following format: e=Mod:Fn,d=Mod:Fn[,m=Mod:Fn][,V=Mod:Fn]
.
See the translate option for details on the string components.-msgprefix Prefix
Prefix
. This can be useful to
when including different sub-projects that have colliding
message names.-modprefix Prefix
Prefix
. Normally the module name of
the generated code is based on the name of the .proto
file.
This option prepends a prefix to the module name, which can be
useful when including different sub-projects that have
colliding proto file names.-msgsuffix Suffix
Suffix
.-modsuffix Suffix
Suffix
.-modname Name
-msgtolower
-il
-include_lib
instead of -include
, which is the default.-type
-no_type
::Type()
annotations in the generated code.
Default is to enable if there are no cyclic dependencies.-descr
-maps
-msgs-as-maps
-mapfields-as-maps
-defs-as-maps
maps
option for the function file/2
for more info.-maps_unset_optional omitted | present_undefined
-maps_oneof tuples | flat
-msgs-as-maps
.hrl
file will
be generated.
Without this option, messages will be records.-mapfields-as-maps
map<_,_>
should be maps.
Otherwise, they will be 2-tuples.-defs-as-maps
-pldefs
option
is specified)-erlc_compile_options Options
-compile(...)
directive on the generated code.-epb
-epb-functions
option-defaults-for-omitted-optionals
option-modsuffix _pb
option-msgtolower
option-epb-functions
encode/1
, decode/2
, encode_MsgName/1
and decode_MsgName/1
-defaults-for-omitted-optionals
undefined
.-type-defaults-for-omitted-optionals
undefined
.-for-version N
-Werror
, -W1
, -W0
, -W
, -Wall
-Werror
means treat warnings as errors-W1
enables warnings, -W0
disables warnings.-W
and -Wall
are the same as -W1
--help
or -h
--version
or -V
protoc
does.
c(Opts :: opts(), Args :: [ProtoFileName :: string()]) -> no_return()
file(File :: string()) -> comp_ret()
Equivalent to file(File, []).
file(File :: string(), Opts :: opts()) -> comp_ret()
Compile a .proto file to a .erl file and to a .hrl file.
The File
argument must not include path to the .proto file. Example:
"SomeDefinitions.proto" is ok, while "/path/to/SomeDefinitions.proto"
is not ok.
The .proto file is expected to be found in a directories specified by an
{i,directory()}
option. It is possible to specify {i,directory()}
several times, they will be searched in the order specified.
The type_specs
option enables or disables ::Type()
annotations
in the generated .hrl file. Default is true
. The default changed
in gpb version 4.0.0. Previously, the default was false
.
If you have messages referencing other messages cyclically, and get into
troubles when compiling the generated files, set this to false
.
verify
option specifies whether or not to generate code
that verifies, during encoding, that values are of correct type and
within range. The verify
option can have the following values:
always
never
optionally
encode_msg/2
that accepts
the run-time option verify
or {verify,boolean()}
for specifying
whether or not to verify values.Erlang value verification either succeeds or crashes with the error
{gpb_type_error,Reason}
. Regardless of the verify
option,
a function, verify_msg/1
is always generated.
copy_bytes
option specifies whether when decoding data of
type bytes
(or strings if the strings_as_binaries
is set), the
decoded bytes should be copied or not. Copying requires the
binary
module, which first appeared in Erlang R14A. When not
copying decoded bytes, they will become sub binaries of the larger
input message binary. This may tie up the memory in the input
message binary longer than necessary after it has been
decoded. Copying the decoded bytes will avoid creating sub
binaries, which will in turn make it possible to free the input message
binary earlier. The copy_bytes
option can have the following values:
false
true
auto
binary:copy/1
function. (This is the default)The strings_as_binaries
option specifies whether strings should
be returned from decoding as strings (list of Unicode code points),
or as binaries (UTF-8 encoded). The copy_bytes
option applies
to strings as well, when the strings_as_binaries
option is set.
Upon encoding, both binaries and iolists are accepted.
The defs_as_proplists
option changes the generated introspection
functions find_msg_def
and get_msg_defs
to return the description
of each message field as a proplist, instead of as a #field{}
record.
The purpose is to make the generated code completely independent
of gpb, at compile-time (it is already independent at run-time).
The keys of the proplist are the names of the record fields in the
#field{}
record. See also gpb:proplists_to_field_records()
and related functions for conversion functions between these two
formats.
The descriptor
option specifies whether or not to generate a
function, descriptor/0, which returns a binary that describes the
proto file(s) contents according to the protobuf's descriptor.proto
.
The default is to not generate such a description. The generated
description binary is most likely not identical to what protoc
would generate, but the contents is roughly equivalent.
The {o,directory()}
option specifies directory to use for storing
the generated .erl
and .hrl
files. Default is the same
directory as for the proto File
.
The {o_erl,directory()}
, {o_hrl,directory()}
, {o_nif_cc,directory()}
,
options specify output directories for where to generate the .erl
and .hrl
files respectively, and for the NIF C++ file,
if the nif
option is specified. The {o_erl,directory()}
option
overrides any {o,directory()}
option, and similarly for the
other file-type specific output options.
maps
option will generate a protobuf encoder/decoder that
uses maps instead of records. This option expands to the following
options:
msgs_as_maps
.hrl
file will be generated, and the functions
encode_msg
, merge_msgs
and verify_msg
will take the
message name as an additional parameter.mapfields_as_maps
map<_,_>
will be a map
instead of a list of 2-tuples.defs_as_maps
#field{}
records, unless, of course
defs_as_proplists
is specified, in which case they will be
proplists instead.maps_unset_optional
option specifies the Erlang-internal
representation; both how it is expected to be found at encoding,
and how decoding will return it:
omitted
present_undefined
undefined
.
This was the default before gpb version 4.0.0.
maps_oneof
option can be used for messages as maps, and can only
take effect if maps_unset_optional
is omitted
(default since 4.0.0).
It changes the representation of oneof fields as described below, if
we would have a oneof-field, xf
with two alternatives a1
and a2
:
{maps_oneof,tuples}
#{xf => {a1, Value}}
or #{xf => {a2, Value}}
{maps_oneof,flat}
#{a1 => Value}}
or #{a2 => Value}}
The nif
option will cause the compiler to generate nif C++ code
for encoding and decoding. The generated nif C++ code can be linked
with the Google protobuf C++ library. Read the file
README.nif-cc
for more info.
The binary
option will cause the generated and compiled code to be
returned as a binary. No files will be written. The return value
will be on the form {ok,Mod,Code}
or {ok,Mod,Code,Warnings}
if the compilation is successful. This option may be useful
e.g. when generating test cases. In case the nif
option is set,
the Code
will be a list of tuples: {erl,binary()}
which
contains the Erlang object byte code, and {nif,binary()}
which
contains the C++ code. You will have to compile the C++ code with a
C++ compiler, before you can use the Erlang code.
The to_proto_defs
option will result in {ok,Defs}
or
{ok,Defs,Warns}
being returned if the compilation is successful.
The returned message definitions can be used with the
proto_defs/2
or proto_defs/3
functions.
The to_msg_defs
option is a deprecated alias for to_proto_defs
.
report_errors
/report_warnings
report
report_errors
and
report_warnings
.return_errors
{error,ErrorList,WarningList}
is
returned when there are errors.return_warnings
WarningList
is added to the tuples returned on success.return
return_errors
and
return_warnings
.Setting the warnings_as_errors
option will cause warnings to be
treated as errors. If there are warnings but no errors, and
return_warnings
is not specified, then error
will be returned.
See format_error/1
for a way to turn an error Reason to
plain text.
If the include_as_lib
option is set, the generated code will include
gpb.hrl as a library, which is necessary if dependencies are managed with
Rebar. Otherwise, the header file is included directly and must be located
in the path, which is default behavior.
The use_packages
option instructs gpb to prepend the name of a package
to every message it contains. If no package is defined, nothing will be
prepended. This enables the reference of messages in other packages which
would otherwise not be possible. However, for reasons of backward
compatibility, this option is disabled by default.
If the the {erlc_compile_options,string()}
option is set,
then the genereted code will contain a directive -compile([String]).
The {rename,{What,How}}
can transform message names, package names,
service and rpc names in various ways. This option supersedes the
options {msg_name_prefix,Prefix}
, {msg_name_suffix,Suffix}
,
msg_name_to_lower
and msg_name_to_snake_case
, while at the same
time giving more fine-grained control. It is for example possible to
apply snake_casing only to the message name, while keeping the
package name, the service name and the rpc name intact. This can be
useful with grpc, where these name components are exposed. The
msg_fqname
refers to the fully qualified message name, as in
Package.MsgName
, while the msg_name
refers to just the message
name without package. The service_fqname
and service_name
specifiers
work analogously.
It is possible to stack rename
options, and they will be applied in
the order they are specified. So it is for example possible to
snake_case a name, and then also prefix it.
The {msg_name_prefix,Prefix}
will add Prefix
(a string or an atom)
to each message. This might be useful for resolving colliding names,
when incorporating several protocol buffer definitions into the same
project. The {msg_name_suffix,Suffix}
works correspondingly.
The {msg_name_prefix,Prefix}
option expands
to [{rename,{pkg_name,Prefix}},{rename,{msg_fqname,{prefix,Prefix}}},
{rename,{group_fqname,{prefix,Prefix}}}}]
,
and ditto for suffixes.
For backwards compatibility, the {msg_name_prefix,{by_proto,PrefixList}}
expands to just [{rename,{msg_fqname,{prefix,PrefixList}}}]
.
The msg_name_to_lower
and msg_name_to_snake_case
options expands
to [{rename,{pkg_name,X}},{rename,{service_fqname,X}},
{rename,{rpc_name,X}},{rename,{msg_fqname,X}},
{rename,{rpc_name,X}},{rename,{group_fqname,X}}]
where X
is
lowercase
or snake_case
respectively.
The {module_name_prefix,Prefix}
will add Prefix
(a string or an atom)
to the generated code and definition files. The {module_name_suffix,Suffix}
works correspondingly. For the case of compatibility with Erlang Protobuffs,
the epb_compatibility
option implies {module_name_suffix,"_pb"}
The {module_name,Name}
can be used to specify the module name of the
generated code freely, instead of basing it on the proto file name.
The name specified with module_name
can be prefixed and suffixed with
the module_name_prefix
and module_name_suffix
options.
translate_type
option can be used to provide packer and unpacker
functions for message fields of a certain type. For now, the only type
supported is {msg,MsgName}
, and for now only when they occur as
submessages. For messages, the MsgName
refers to a name after
renaming has taken place.
The merge translator is optional, and is called either via the merge_msgs
function in the generated code, or when the decoder sees another
field of the same type. The default merge operation is to let the second
element overwrite previous elements. The verify translator is
optional too, since verification can be disabled.
The translation calls are specified as {Mod,Fn,ArgTemplate}
where
Mod
,Fn
is a module and function to call, ArgTemplate
is a list
of terms, containing markers, such as $1
, $2
and so on, for where
to place the actual args. This makes it possible to specify additional
static argument terms, for instance.
The translator functions are called as follows:
Mod:Fn(Term)
to pack the Term
($1
) to
a google.protobuf.Any
message.Mod:Fn(Any)
to unpack the Any
($1
) to
unpack a google.protobuf.Any
message to a term.Mod:Fn(Term1, Term2) -> Term3
to merge two
unpacked terms to a resulting Term3. The $1
is the
previously seen term (during decoding, on encountering a
second field of the same type), or the first argument to the
merge_msgs
function. The $2
is the lastly seen term, or
the second argument to the merge_msgs
function.Mod:Fn(Term) -> _
to verify an unpacked Term
.
If Term
($1
) is valid, the function is expected to just return
any value, which is ignored and discarded.
If Term
is invalid, the function is exptected to not
return anything, but instead either crash, call
erlang:error/1
, or throw/1
or exit/1
. with the
reason for error.
(For backwards compatibility, it is also possible
to have an error function as argument, using $errorf
,
but this is deprecated.)$user_data
user_data
option to the
generated encode_msg
, decode_msg
, merge_msgs
and
verify_msg
functions. If that option is not specified, the
value undefined
is used substituted for $user_data
.$op
encode
, decode
, merge
or
verify
, depending on from which context it is actually
called. This can be useful because if the message is to be
verified on encoding (see the verify
option), then the same
options, and thus the same user-data, are used for both
encode_msg
and for verify_msg
. The $op
marker makes it
possible to tell these two call sites apart, if needed.The option {any_translate,Translations}
is retained for backwards
compatibility, and expands to
{translate_type,{'google.protobuf.Any',Translations}}
.
epb_compatibility
option is an umbrella-option for
compatibility with the Erlang protobuffs library. It will expand to
the options below. It will expand in-place, meaning any of these
can be overridden if specified before the epb_compatibility
option.
epb_functions
defaults_for_omitted_optionals
{module_name_suffix,"_pb"}
{msg_name_to_lower,true}
epb_functions
option is specified, then for compatibility
with Erlang protobuffs, the following functions will be generated:
encode/1
encode_<MsgName>/1
decode/2
decode_<MsgName>/1
The defaults_for_omitted_optionals
and
type_defaults_for_omitted_optionals
options generates code that
set default values or type-defaults respectively, on decoding, if
an optional field is not present in the binary to decode. Normally
it would otherwise have been set to undefined
. Note that with
these options it is not possible to determine after decoding
whether a field contained data in the binary message. Also note
that these options are only applicable for proto2 syntax messages,
and are ignored for proto3 syntax messages. (For proto3, it
effectively must be ignored, since, on the wire, a field
set to its type-default value is indistinguishable from an omitted
value.)
The import_fetcher
option can be used to catch imports. The
option value must be a function taking one argument, the name of
the file to import. It must return either from_file
, letting this
file pass through the normal file import, or {ok,string()}
if it
has fetched the file itself, or {error,term()}
.
target_erlang_version
can be used to specify another major
version of Erlang/OTP to generate code for. The default, current
means that the generated code is expected to be compiled and run
on the same major version as gpb runs on.
format_error(Reason :: {error, Reason} | Reason) -> io_list()
Reason = term()
Produce a plain-text error message from a reason returned by
for instance file/2
or proto_defs/2
.
format_warning(X :: warning()) -> iolist()
Produce a plain-text error message from a reason returned by
for instance file/2
or proto_defs/2
.
locate_import(ProtoFileName :: string(), Opts :: opts()) -> {ok, File :: string()} | {error, reason()}
Locate an import target. This function might be potentially
useful for instance in an intercepting import_fetcher
fun that
just wants to record the accessed imports.
msg_defs(Mod :: module(), Defs :: gpb_parse:defs()) -> comp_ret()
Equivalent to msg_defs(Mod, Defs, []).
Deprecated, use proto_defs/2 instead.
msg_defs(Mod, Defs, Opts) -> CompRet
Equivalent to proto_defs(Mod, Defs, Opts).
Deprecated, use proto_defs/2 instead.
parse_opts_and_args(Argv :: [string()]) -> {ok, {opts(), Args :: [string()]}} | {error, Reason :: string()}
proto_defs(Mod :: module(), Defs :: gpb_parse:defs()) -> comp_ret()
Equivalent to proto_defs(Mod, Defs, []).
proto_defs(Mod :: module(), Defs :: gpb_parse:defs(), Opts :: opts()) -> comp_ret()
Compile a list of pre-parsed definitions to file or to a binary.
See file/2
for information on options and return values.
Read an import file. This function might be potentially
useful for instance in an intercepting import_fetcher
fun that
just wants to record the accessed imports.
show_args() -> term()
show_version() -> term()
string(Mod :: module(), Str :: string()) -> comp_ret()
Equivalent to string(Mod, Str, []).
string(Mod :: module(), Str :: string(), Opts :: opts()) -> comp_ret()
Compile a .proto
file as string. See file/2
for information
on options and return values.
Generated by EDoc