versionpb

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2022 License: MIT Imports: 8 Imported by: 0

README

versionpb

基于 protobuf 自定义选项,实现的数据多版本定义方法

例子

协议定义

syntax = "proto3";
package examplepb;
option go_package = "examplepb/";
import "github.com/fananchong/versionpb/version.proto";

message Msg1 {
  option (versionpb.version_msg) = "3.0";
  enum Enum1 {
    option (versionpb.version_enum) = "3.0";
    E1 = 0;
    E2 = 1;
    E3 = 2 [ (versionpb.version_enum_value) = "3.3" ];
  }
  bytes f1 = 1;
  int64 f2 = 2;
  Enum1 f3 = 3;
  bool f4 = 4;
  int64 f5 = 5 [ (versionpb.version_field) = "3.1" ];
  Enum1 f6 = 6 [ (versionpb.version_field) = "3.2" ];
}

使用

package main

import (
	"example1/examplepb"
	"fmt"

	"github.com/fananchong/versionpb"
	"google.golang.org/protobuf/reflect/protoregistry"
)

func main() {
	{
		msg := &examplepb.Msg1{}
		fmt.Printf("%v\n", versionpb.MinimalVersion(msg))
	}

	{
		msg := &examplepb.Msg1{F6: examplepb.Msg1_E2}
		fmt.Printf("%v\n", versionpb.MinimalVersion(msg))
	}

	{
		msg := &examplepb.Msg1{F6: examplepb.Msg1_E3}
		fmt.Printf("%v\n", versionpb.MinimalVersion(msg))
	}

	annotations, err := versionpb.AllVersionByFiles(protoregistry.GlobalFiles, []string{"google.protobuf"})
	if err != nil {
		panic(err)
	}
	for _, v := range annotations {
		fmt.Printf("fullname:%v version:%v\n", v.FullName, v.Version)
	}
}

完整例子参见: https://github.com/fananchong/use_protobuf_define_multi_version_example

API

api 说明
MinimalVersion 获取消息版本
AllVersionByFiles 获取所有协议定义版本

参考

本项目是参考了Etcd 对 WAL 文件 Entry 做多版本识别的方法

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// optional string version_enum = 50002;
	E_VersionEnum = &file_version_proto_extTypes[2]
)

Extension fields to descriptorpb.EnumOptions.

View Source
var (
	// optional string version_enum_value = 50003;
	E_VersionEnumValue = &file_version_proto_extTypes[3]
)

Extension fields to descriptorpb.EnumValueOptions.

View Source
var (
	// optional string version_field = 50001;
	E_VersionField = &file_version_proto_extTypes[1]
)

Extension fields to descriptorpb.FieldOptions.

View Source
var (
	// optional string version_msg = 50000;
	E_VersionMsg = &file_version_proto_extTypes[0]
)

Extension fields to descriptorpb.MessageOptions.

View Source
var File_version_proto protoreflect.FileDescriptor

Functions

func MinimalVersion

func MinimalVersion(m protoreflect.Message) *semver.Version

MinimalVersion 根据消息,获取消息的版本

func VisitFileDescriptor

func VisitFileDescriptor(file protoreflect.FileDescriptor, visitor Visitor) error

VisitFileDescriptor 根据 proto 文件描述,获取消息的版本

Types

type VersionAnnotation added in v0.0.2

type VersionAnnotation struct {
	FullName protoreflect.FullName
	Version  *semver.Version
}

func AllVersionByFiles added in v0.0.2

func AllVersionByFiles(files *protoregistry.Files, externalPackages []string) (annotations []VersionAnnotation, err error)

AllVersionByFiles 根据 proto 协议定义,获取协议版本

type Visitor

type Visitor func(path protoreflect.FullName, ver *semver.Version) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL