protorowdf

package module
v0.0.0-...-332aeb4 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 17 Imported by: 0

README

protorowdf (ProtoBuf Row and DataFrame)

Sometimes we want to represent data as a series of rows, and some times the same data should be in columns. This library is designed to generate two proto message definitions, one of row and one for column, and provides the conversion functions between the two.

Example

Provided below input:

comment: |
  Test package for row/dataframe generation for protobuf.
    comment can start with some spaces/indents.

  blank lines will be conserved if they are in between non-empty lines.
package_name: pkg.name
options:
  - name: go_package
    value: '"example.com/what/a/good/package"'
  - name: optimize_for
    value: CODE_SIZE
  - name: cc_enable_arenas
    value: true
structs:
  - name: ARowStruct
    comment: |
      Struct comments are similar to file comments.
    fields:
      - name: density
        plural_name: densities
        data_type: Int64
        comment: |
          comments on data field.
          can also be multiple lines.
        tag_num: 1
      - name: string_field
        data_type: String
        tag_num: 2
        comment: | # notice leading blank lines will be trimmed.



          more comment on another field.

  - name: AnotherRowStruct
    comment: |
      This is another struct's comment.

      And you should be able to put a blank line here too.
    fields:
      - name: adata
        data_type: string
        tag_num: 2
      - name: anotherdata
        data_type: fixed32
        tag_num: 3

The below output will be generated

syntax = "proto3";

// Test package for row/dataframe generation for protobuf.
//   comment can start with some spaces/indents.
//
// blank lines will be conserved if they are in between non-empty lines.
package pkg.name;

option go_package = "example.com/what/a/good/package";
option optimize_for = CODE_SIZE;
option cc_enable_arenas = true;

// Struct comments are similar to file comments.
message ARowStruct {
  // comments on data field.
  // can also be multiple lines.
  int64 density = 1;
  // more comment on another field.
  string string_field = 2;
}

// Plural Version of ARowStruct.
// Struct comments are similar to file comments.
message ARowStructs {
  // comments on data field.
  // can also be multiple lines.
  repeated int64 densities = 1;
  // more comment on another field.
  repeated string string_fields = 2;
}

// This is another struct's comment.
//
// And you should be able to put a blank line here too.
message AnotherRowStruct {
  string adata = 2;
  fixed32 anotherdata = 3;
}

// Plural Version of AnotherRowStruct.
// This is another struct's comment.
//
// And you should be able to put a blank line here too.
message AnotherRowStructs {
  repeated string adatas = 2;
  repeated fixed32 anotherdatas = 3;
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SupportedType_name = map[int32]string{
		0:  "Unknown",
		1:  "Int64",
		2:  "Int32",
		3:  "Uint64",
		4:  "Uint32",
		5:  "Sint64",
		6:  "Sint32",
		7:  "Fixed64",
		8:  "Fixed32",
		9:  "Sfixed64",
		10: "Sfixed32",
		11: "Bool",
		12: "String",
		13: "Bytes",
		14: "Float",
		15: "Double",
	}
	SupportedType_value = map[string]int32{
		"Unknown":  0,
		"Int64":    1,
		"Int32":    2,
		"Uint64":   3,
		"Uint32":   4,
		"Sint64":   5,
		"Sint32":   6,
		"Fixed64":  7,
		"Fixed32":  8,
		"Sfixed64": 9,
		"Sfixed32": 10,
		"Bool":     11,
		"String":   12,
		"Bytes":    13,
		"Float":    14,
		"Double":   15,
	}
)

Enum value maps for SupportedType.

View Source
var File_config_proto protoreflect.FileDescriptor

Functions

func GoProtoName

func GoProtoName(s string) string

GoProtoName is a copy of GoCamelCase from the protobuf-go library.

GoCamelCase camel-cases a protobuf name for use as a Go identifier.

If there is an interior underscore followed by a lower case letter, drop the underscore and convert the letter to upper case.

Types

type DuplicateTagNumberError

type DuplicateTagNumberError struct {
	MessageName string
	Duplicates  map[int32][]string
}

func IsDuplicateTagNumberError

func IsDuplicateTagNumberError(err error) *DuplicateTagNumberError

func (DuplicateTagNumberError) Error

func (d DuplicateTagNumberError) Error() string

type Field

type Field struct {

	// name of the field, it will be used in the message for row.
	Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	// name of the field when it's in the dataframe/column message.
	// If no plural_name is provided, name + "s" will be used.
	PluralName string `protobuf:"bytes,2,opt,name=plural_name,json=pluralName,proto3" json:"plural_name,omitempty"`
	// type of the data.
	DataType SupportedType `protobuf:"varint,11,opt,name=data_type,json=dataType,proto3,enum=protorowdf.SupportedType" json:"data_type,omitempty"`
	// tag num
	TagNum int32 `protobuf:"varint,12,opt,name=tag_num,json=tagNum,proto3" json:"tag_num,omitempty"`
	// comments will be copied over with the beginning spaces and
	// trailing spaces will be removed, otherwise it's verbatim copied over
	// including blank lines and indents.
	Comment string `protobuf:"bytes,21,opt,name=comment,proto3" json:"comment,omitempty"`
	// indent of the field, 2 spaces if set to empty.
	// this can also be set in the [ProtoFile] message.
	Indent string `protobuf:"bytes,42,opt,name=indent,proto3" json:"indent,omitempty"`
	// contains filtered or unexported fields
}

Field represents a field of generated proto message defintion.

func (*Field) CleanPluralName

func (f *Field) CleanPluralName() string

func (*Field) Descriptor deprecated

func (*Field) Descriptor() ([]byte, []int)

Deprecated: Use Field.ProtoReflect.Descriptor instead.

func (*Field) GetComment

func (x *Field) GetComment() string

func (*Field) GetDataType

func (x *Field) GetDataType() SupportedType

func (*Field) GetIndent

func (x *Field) GetIndent() string

func (*Field) GetName

func (x *Field) GetName() string

func (*Field) GetPluralName

func (x *Field) GetPluralName() string

func (*Field) GetTagNum

func (x *Field) GetTagNum() int32

func (*Field) GoName

func (f *Field) GoName() string

func (*Field) GoPluralName

func (f *Field) GoPluralName() string

func (*Field) GoType

func (f *Field) GoType() string

func (*Field) PrettyComments

func (f *Field) PrettyComments() []string

func (*Field) ProtoMessage

func (*Field) ProtoMessage()

func (*Field) ProtoReflect

func (x *Field) ProtoReflect() protoreflect.Message

func (*Field) Reset

func (x *Field) Reset()

func (*Field) RustNeedClone

func (f *Field) RustNeedClone() string

func (*Field) RustType

func (f *Field) RustType() string

func (*Field) String

func (x *Field) String() string

func (*Field) WriteFieldDefinition

func (f *Field) WriteFieldDefinition(w io.Writer) error

type GoProtoFile

type GoProtoFile struct {
	*ProtoFile

	ManualGoPackage string
}

func (*GoProtoFile) CopyDataCode

func (g *GoProtoFile) CopyDataCode() (string, error)

func (*GoProtoFile) GoPackageName

func (g *GoProtoFile) GoPackageName() string

func (*GoProtoFile) WriteCopyDataCode

func (g *GoProtoFile) WriteCopyDataCode(w io.Writer) error

type Option

type Option struct {
	Name  string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
	// contains filtered or unexported fields
}

Option for this protofile

func (*Option) Descriptor deprecated

func (*Option) Descriptor() ([]byte, []int)

Deprecated: Use Option.ProtoReflect.Descriptor instead.

func (*Option) GetName

func (x *Option) GetName() string

func (*Option) GetValue

func (x *Option) GetValue() string

func (*Option) ProtoMessage

func (*Option) ProtoMessage()

func (*Option) ProtoReflect

func (x *Option) ProtoReflect() protoreflect.Message

func (*Option) Reset

func (x *Option) Reset()

func (*Option) String

func (x *Option) String() string

type ProtoFile

type ProtoFile struct {

	// package name
	PackageName string `protobuf:"bytes,1,opt,name=package_name,json=packageName,proto3" json:"package_name,omitempty"`
	// messages contained in this file, note each Struct generate two message
	// types.
	Structs []*Struct `protobuf:"bytes,11,rep,name=structs,proto3" json:"structs,omitempty"`
	// Options
	Options []*Option `protobuf:"bytes,21,rep,name=options,proto3" json:"options,omitempty"`
	// comments will be copied over with the beginning spaces and
	// trailing spaces will be removed, otherwise it's verbatim copied over
	// including blank lines and indents.
	Comment string `protobuf:"bytes,31,opt,name=comment,proto3" json:"comment,omitempty"`
	// indent of fields for the fields contained in this file.
	FieldIndent string `protobuf:"bytes,42,opt,name=field_indent,json=fieldIndent,proto3" json:"field_indent,omitempty"`
	// contains filtered or unexported fields
}

ProtoFile defines the structure of the protofile.

func ParseJSON

func ParseJSON(s []byte) (*ProtoFile, error)

func ParseYAML

func ParseYAML(s []byte) (*ProtoFile, error)

func (*ProtoFile) Descriptor deprecated

func (*ProtoFile) Descriptor() ([]byte, []int)

Deprecated: Use ProtoFile.ProtoReflect.Descriptor instead.

func (*ProtoFile) GetComment

func (x *ProtoFile) GetComment() string

func (*ProtoFile) GetFieldIndent

func (x *ProtoFile) GetFieldIndent() string

func (*ProtoFile) GetOptions

func (x *ProtoFile) GetOptions() []*Option

func (*ProtoFile) GetPackageName

func (x *ProtoFile) GetPackageName() string

func (*ProtoFile) GetStructs

func (x *ProtoFile) GetStructs() []*Struct

func (*ProtoFile) PrettyComments

func (f *ProtoFile) PrettyComments() []string

func (*ProtoFile) ProtoFileDefinition

func (f *ProtoFile) ProtoFileDefinition() (string, error)

func (*ProtoFile) ProtoMessage

func (*ProtoFile) ProtoMessage()

func (*ProtoFile) ProtoReflect

func (x *ProtoFile) ProtoReflect() protoreflect.Message

func (*ProtoFile) Reset

func (x *ProtoFile) Reset()

func (*ProtoFile) String

func (x *ProtoFile) String() string

func (*ProtoFile) UpdateFieldIndent

func (f *ProtoFile) UpdateFieldIndent()

func (*ProtoFile) ValidateTagNumbers

func (f *ProtoFile) ValidateTagNumbers() error

func (*ProtoFile) WriteProtoFileDefinition

func (f *ProtoFile) WriteProtoFileDefinition(w io.Writer) error

type RustCopyConfig

type RustCopyConfig struct {
	GenInclude bool
	NoCargoOut bool
	FileName   string
}

type RustProtoFile

type RustProtoFile struct {
	*ProtoFile

	*RustCopyConfig
}

func (*RustProtoFile) CopyDataCode

func (r *RustProtoFile) CopyDataCode() (string, error)

func (*RustProtoFile) IncludeLine

func (r *RustProtoFile) IncludeLine() string

func (*RustProtoFile) WriteCopyDataCode

func (r *RustProtoFile) WriteCopyDataCode(w io.Writer) error

type Struct

type Struct struct {

	// name of the generate row message type.
	Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
	// name of the dataframe message type. use name + "s" if unset.
	PluralName string `protobuf:"bytes,2,opt,name=plural_name,json=pluralName,proto3" json:"plural_name,omitempty"`
	// fields for the struct.
	Fields []*Field `protobuf:"bytes,11,rep,name=fields,proto3" json:"fields,omitempty"`
	// comments will be copied over with the beginning spaces and
	// trailing spaces will be removed, otherwise it's verbatim copied over
	// including blank lines and indents.
	Comment string `protobuf:"bytes,21,opt,name=comment,proto3" json:"comment,omitempty"`
	// contains filtered or unexported fields
}

Struct represents the structure of the message.

func (*Struct) CleanPluralName

func (m *Struct) CleanPluralName() string

func (*Struct) Descriptor deprecated

func (*Struct) Descriptor() ([]byte, []int)

Deprecated: Use Struct.ProtoReflect.Descriptor instead.

func (*Struct) FirstField

func (m *Struct) FirstField() *Field

func (*Struct) GetComment

func (x *Struct) GetComment() string

func (*Struct) GetFields

func (x *Struct) GetFields() []*Field

func (*Struct) GetName

func (x *Struct) GetName() string

func (*Struct) GetPluralName

func (x *Struct) GetPluralName() string

func (*Struct) MessageDefinition

func (s *Struct) MessageDefinition(w io.Writer) error

func (*Struct) PrettyComments

func (s *Struct) PrettyComments() []string

func (*Struct) ProtoMessage

func (*Struct) ProtoMessage()

func (*Struct) ProtoReflect

func (x *Struct) ProtoReflect() protoreflect.Message

func (*Struct) Reset

func (x *Struct) Reset()

func (*Struct) String

func (x *Struct) String() string

func (*Struct) ValidTagNumbers

func (m *Struct) ValidTagNumbers() error

type SupportedType

type SupportedType int32

SupportedType limits the types that we support. Those types are their proto types with first letter capitalized, except for Unknown.

const (
	SupportedType_Unknown  SupportedType = 0
	SupportedType_Int64    SupportedType = 1
	SupportedType_Int32    SupportedType = 2
	SupportedType_Uint64   SupportedType = 3
	SupportedType_Uint32   SupportedType = 4
	SupportedType_Sint64   SupportedType = 5
	SupportedType_Sint32   SupportedType = 6
	SupportedType_Fixed64  SupportedType = 7
	SupportedType_Fixed32  SupportedType = 8
	SupportedType_Sfixed64 SupportedType = 9
	SupportedType_Sfixed32 SupportedType = 10
	SupportedType_Bool     SupportedType = 11
	SupportedType_String   SupportedType = 12
	SupportedType_Bytes    SupportedType = 13
	SupportedType_Float    SupportedType = 14
	SupportedType_Double   SupportedType = 15
)

func (SupportedType) Descriptor

func (SupportedType) Enum

func (x SupportedType) Enum() *SupportedType

func (SupportedType) EnumDescriptor deprecated

func (SupportedType) EnumDescriptor() ([]byte, []int)

Deprecated: Use SupportedType.Descriptor instead.

func (SupportedType) GoType

func (t SupportedType) GoType() (string, error)

func (SupportedType) MarshalYAML

func (t SupportedType) MarshalYAML() ([]byte, error)

func (SupportedType) MustProtoTypeString

func (t SupportedType) MustProtoTypeString() string

func (SupportedType) Number

func (SupportedType) ProtoTypeString

func (t SupportedType) ProtoTypeString() (string, error)

ProtoTypeString returns the proto type as a string

func (SupportedType) RustType

func (t SupportedType) RustType() (string, error)

func (SupportedType) String

func (x SupportedType) String() string

func (SupportedType) Type

func (*SupportedType) UnmarshalJSON

func (t *SupportedType) UnmarshalJSON(b []byte) error

func (*SupportedType) UnmarshalYAML

func (t *SupportedType) UnmarshalYAML(b []byte) error

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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