A lightweight message codegen tool — a simple alternative to Protocol Buffers for C++.
libyang reads an XML message schema and generates C++ structs with tag-based Marshal / Unmarshal helpers. Field tags enable forward/backward compatibility across versions: unknown tags are skipped on decode, and missing fields stay at their current values (call Clear() first for defaults).
The generator is a pure Go CLI (no external scripting runtime). Generated code depends only on the bundled header yang.h.
Download a prebuilt binary from the Releases page, or build from source:
git clone https://github.com/esrrhs/libyang.git
cd libyang
go build -ldflags="-s -w" -o libyang .Docker:
docker pull esrrhs/libyang- Edit
sample.xml(or your own schema) to define enums and structs. - Generate the header:
./libyang -xml sample.xml -out sample.h
# or
./libyang sample.xml sample.h
# or
./run.sh- Include
yang.hand the generated header in your C++ project.
Show version:
./libyang -version<result>
<enum name="ConstDefine" comment="constants">
<member name="ACC_LEN" type="32" comment="account length"/>
<member name="PWD_LEN" type="32" comment="password length"/>
</enum>
<enum name="MsgID" comment="message ids">
<member name="CS_REQ_LOGIN" type="1" comment="login request"/>
</enum>
<struct name="CSReqLogin" comment="login request">
<member name="Acc" type="char" length="ACC_LEN" comment="account" tag="1"/>
<member name="Pwd" type="char" length="PWD_LEN" comment="password" tag="2"/>
</struct>
</result>Supported member shapes:
- Scalar numeric types (
uint32_t,float, …) and nested structs - Fixed-length
charstrings (type="char"+length) - Arrays with a runtime count field (
length+ref) - Tagged unions (
structwithtype="union", members userefas the case enum)
The generator validates the schema before writing a header. Tags must be in 1–255 and unique within a struct; char fields need a positive length (a number or a previously defined enum constant); arrays need ref; types and union case refs must be defined.
Each field is encoded as:
tag: uint8
type: uint8 (ProtoDataType in yang.h)
payload:
integer: little-endian two's complement / unsigned value
float: IEEE-754 binary32, bits stored little-endian
double: IEEE-754 binary64, bits stored little-endian
string: len as little-endian uint16, then len bytes (no trailing NUL required on the wire)
struct: len as little-endian uint16, then len bytes of nested fields
Limits:
- Tags are 1–255.
- String and nested-struct payload lengths are at most 65535 bytes.
Marshalreturns-1if a payload would exceed this, instead of truncating the length.
This is a portability-oriented wire-format evolution introduced in 0.2. Integers and lengths are now always little-endian, and values are copied with memcpy so unaligned buffers are well-defined. On little-endian machines the layout is the same as the previous native encoding. Big-endian machines no longer write values in native byte order, so records written by a pre-0.2 big-endian process are not compatible.
- Unknown tags are skipped on decode (forward compatible).
- Missing fields are left unchanged (backward compatible if you
Clear()first for defaults). - A known field with a truncated or corrupt payload causes
Unmarshalto return-1. Type mismatches on a known tag skip that field's payload so later fields stay aligned. - Union objects require a valid discriminator (tag 1) and a matching branch payload (tag 2). Failure returns
-1and does not adopt a newm_Typeor union data. Marshalreturns the number of bytes written on success, or-1if the buffer is too small or a length exceeds 65535.Unmarshalreturns 0 on success, or-1on failure. After a failedUnmarshal, the object may be partially updated; do not use it.
go test -count=1 ./...
./run_cpp_tests.shgo test also runs the C++ runtime tests when a C++ compiler is available. The C++ suite is built with ASan/UBSan when the compiler supports them.
./pack.shCross-platform archives are written to pack/ (binary plus yang.h, sample.xml, docs). The Release workflow watches version.go: when its version string changes on master (and the tag does not exist yet), CI runs ./pack.sh and publishes a GitHub Release.
See CHANGELOG.md for version history.
MIT — see LICENSE.