gmfs

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: MIT Imports: 7 Imported by: 0

README

gmfs

Generate protobuf message from go struct.

Installing

go install github.com/Dokiys/gmfs/cmd/gmfs@latest

Usage

$ gmfs -h
usage: gmfs [OPTION] [GO_FILES]
  -i int
	  Set int convert type, only allow [32,64]. (default 64)
  -r string
	  Regexp match struct name. (default ".*")
  -v,--version		Show version info and exit.

If under macOS copy struct name and run gmfs -s=$(pbpaste) $(ls | grep ".go") | pbcopy at go file path will copy the result to clipboard.

Example:

$ cat example.go 
package example

import (
	"strings"
	"time"
)

func (i *Item) P() {}

// Item Comment 1
/*
	Item Comment 1
*/
// Item Comment 1
type Item struct {
	// Item ItemId Comment 3

	// Item ItemId Comment 2
	ItemId    int // Item ItemId Comment 1
	Name      string
	Duration  time.Duration
	CreatedAt time.Time
}

type TemplateData struct {
	Arr   []string
	Items []*Item
	Map1  map[string]*Item

	// Unsupported
	function func() bool
	strings.Reader
}
$ gmfs example.go

// Item Comment 1
/*
	Item Comment 1
*/
// Item Comment 1
message Item {
	// Item ItemId Comment 1
	int64 item_id = 1;

	string name = 2;

	Duration duration = 3;

	google.protobuf.Timestamp created_at = 4;
}

message TemplateData {

	repeated string arr = 1;

	repeated Item items = 2;

	map<string,Item> map1 = 3;
	// Unsupported
	// Unsupported field: function

	// Unsupported field: strings.Reader
}

Documentation

Overview

Package gmfs provides a method to convert specified struct from reader and write to given writer.

Index

Examples

Constants

This section is empty.

Variables

View Source
var IntType = fmt.Sprintf("int%d", 32<<(^uint(0)>>63))

Functions

func GenMsg

func GenMsg(r io.Reader, w io.Writer, exp regexp.Regexp) error
Example
package main

import (
	"bytes"
	"os"
	"regexp"

	"github.com/Dokiys/gmfs"
)

func main() {
	src := `
package example

import (
	"strings"
	"time"
)

func (i *Item) P() {}

// Item Comment 1
/*
	Item Comment 1
*/
// Item Comment 1
type Item struct {
	// Item ItemId Comment 3

	// Item ItemId Comment 2
	ItemId    int // Item ItemId Comment 1
	Name      string
	Duration  time.Duration
	CreatedAt time.Time
}

type TemplateData struct {
	Arr   []string
	Items []*Item
	Map1  map[string]*Item

	// Unsupported
	function func() bool
	strings.Reader
}
`
	r := bytes.NewReader([]byte(src))

	exp, _ := regexp.Compile(".*")
	_ = gmfs.GenMsg(r, os.Stdout, *exp)
}
Output:

// Item Comment 1
/*
	Item Comment 1
*/
// Item Comment 1
message Item {
	// Item ItemId Comment 1
	int64 item_id = 1;

	string name = 2;

	Duration duration = 3;

	google.protobuf.Timestamp created_at = 4;
}

message TemplateData {

	repeated string arr = 1;

	repeated Item items = 2;

	map<string,Item> map1 = 3;
	// Unsupported
	// Unsupported field: function

	// Unsupported field: strings.Reader
}

func RegisterValidateFunc

func RegisterValidateFunc(f ValidateFunc)

Types

type ValidateFunc

type ValidateFunc = func(field *ast.Field) string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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