📌往期推文全新看点(文中附带最新·鸿蒙全栈学习笔记)
📃 鸿蒙(HarmonyOS)北向开发知识点记录~
📃 鸿蒙(OpenHarmony)南向开发保姆级知识点汇总~
📃 鸿蒙应用开发与鸿蒙系统开发哪个更有前景?
📃 嵌入式开发适不适合做鸿蒙南向开发?看完这篇你就了解了~
📃 对于大前端开发来说,转鸿蒙开发究竟是福还是祸?
📃 鸿蒙岗位需求突增!移动端、PC端、IoT到底该怎么选?
📃 记录一场鸿蒙开发岗位面试经历~
📃 持续更新中……
介绍
当前基于cangjie v0.53.4
特性
- 🚀 支持 Google Protocol Buffers
软件架构
架构
软件目前主要分为 protobuf, protoc 两个部分
-
protobuf
提供libprotobuf-cj.a
, 在仓颉中使用 Google Protocol Buffers 需要链接该静态库 -
protoc
提供protoc-gen-cj
, 一个 google protoc 的 plugin, 用于将.proto 文件编译为仓颉代码
protobuf:
- abstract class MessageLite <: ToString: 定义了messages的通用操作, proto中定义的message均继承MessageLite.
- interface Enum<E>: 定义了仓颉 enum 和 C 风格 enum 之间"映射"关系的接口, protobuf enum 实现这些接口, 从而在语言之交换 enum 信息
protoc(protoc-gen-cj):
- 从 stdin 读取二进制数据解析为 CodeGeneratorRequest, 根据该请求组织生成仓颉代码, 写入CodeGeneratorResponse, 打包并输出到 stdout
源码目录
.
├── README.md
├── doc
│ ├── assets
│ ├── design.md
│ ├── feature_api.md
├── ci_test
├── src
│ ├── google
│ ├── protobuf
│ ├── protoc
└── test
├── HLT
├── LLT
└── UT
doc
存放库的设计文档、提案、库的使用文档、LLT 用例覆盖报告(此项目不支持 cpm build 编译,而覆盖率需要 cpm build --coverage,所以未能输出覆盖报告)ci_test
是python源码目录src
是库源码目录test
存放测试用例,包括 HLT 用例、LLT 用例和 UT 用例
编译执行
示例
example.proto 文件内容
syntax = "proto3";
package foo;
message Person {
string name = 1;
int32 id = 2;
string email = 3;
}
示例
import collection.*
import io.*
import foo.* // The foo package is generated by the protobuf-gen-cj plugin.
main() {
var p = Person()
// parameter assignment
p.name = "test"
p.id = 1
p.email = "test@huawei.com"
// print
println("Serialize to string: ${p}")
// encode
var b = p.pack()
println("Serialize result: ${b}")
// decode
var p1 = Person.fromBytes(b)
println("Deserialization result: ${p1}")
return
}
安装教程
- 安装对应版本cangjie
- 设置CANGJIE_PATH为合法目录,作为
.cjo
文件的安装目录 - 安装google protobuf compiler
- 编译&安装
protobuf-cj
库、protoc-gen-cj
插件(见使用说明1-4)
使用说明
cd protobuf-cj
git submodule update --init --recursive
拉取依赖库make
编译*.a、*.cjo、protoc-gen-cjmake install
安装*.a、*.cjo、protoc-gen-cjprotoc --cj_out=src/ -Isrc/ $(proto_files)
编译proto_files
生成*.pb.cj文件- 在项目中使用protobuf messages, 使用时导入*.pb.cj所在包, 编译时添加
-lprotobuf-cj
选项
若依赖库拉取失败,需要将依赖库手动拷贝到对应目录下,git submodule
可以查看所有依赖库
约束与限制
1.支持的标量数值类型double、float、int32、int64、uint32、uint64、sint32、sint64、fixed32、fixed64、sfixed32、sfixed64、bool、string、bytes。
2.当前版本基于 Google Protocol Buffers V21.5 版本开发,需要符合对应版本协议的proto书写规范