h2o

module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: Apache-2.0

README

h2o

脚手架工具,统一的dsl,方便生成一些代码, 静态MVC。持有model,其余的就是交给h2o生成代码。最多写写logic 目前可以直接生成的代码有:

  1. http client:实现一个第三方sdk,so easy
  2. http server:实现服务再不用纠结结构体的定义
  3. curl 命令: 方便自测和api交流
  4. protobuf定义: 从json或者yaml生成

Install

go install github.com/antlabs/h2o/cmd/h2o@latest

命令行

  • codemsg: 用于生成错误码相关代码
  • http: 生成http client 和http server(代码)
  • curl: 生成curl命令
  • jsonstruct: 字符串转golang struct定义
  • yamlstruct: 字符串转golang struct定义
  • pb: dsl生成protobuf定义
Usage:
    ./h2o [Options] <Subcommand>

Options:
    -h,--help     print the help information

Subcommand:
    codemsg       Generate code in codemsg format from constants
    curl          gen curl command
    http          gen http code
    jsonstruct    Generate structure from json
    pb            gen protobuf code
    transport     transport
    yamlstruct    Generate structure from yaml

json 子命令

# 从json文件中生成结构体, -n 选项表示分拆
h2o jsonstruct -f ./test.yaml -n
# 从stdin生成结构体, 按ctrl+d 结束
h2o jsonstruct -f -

yaml 子命令

# 从json文件中生成结构体, -n 选项表示分拆
h2o yamlstruct -f ./test.yaml -n
h2o yamlstruct -f -

codemsg 子命令

只要实现code,自动生成String()类型和CodeMsg类型。可以在错误码代码节约时间
1.给每个ErrNo类型生成String()方法, 内容就是注释里面的。
2.给每个ErrNo类型生成CodeMsg{}结构

// 这段代码是我们要写的。
package demo

type ErrNo int32 // 这里的类型可以自定义,--type后面类型

const (
	ENo ErrNo = 1003 // 号码出错

	ENotFound ErrNo = 1004 // 找不到
)

生成的CodeMsg代码如下

// Code generated by "h2o codemsg --code-msg --linecomment --type ErrNo ./testdata/err.go"; DO NOT EDIT."

package demo

import (
	"encoding/json"
	"strings"
)

type CodeMsger interface {
	SetCode(int)
	SetMsg(string)
	GetCode() int
	GetMsg() string
}

var _ CodeMsger = (*CodeMsg)(nil)

type CodeMsg struct {
	Code    ErrNo  "json:\"code\""
	Message string "json:\"message\""
}

func (x *CodeMsg) Error() string {
	all, _ := json.Marshal(x)
	var b strings.Builder
	b.Write(all)
	return b.String()
}

func (x *CodeMsg) SetCode(code int) {
	x.Code = ErrNo(code)
}

func (x *CodeMsg) SetMsg(msg string) {
	x.Message = msg
}

func (x *CodeMsg) GetCode() int {
	return int(x.Code)
}

func (x *CodeMsg) GetMsg() string {
	return x.Message
}

func NewCodeMsg(code ErrNo) error {
	return &CodeMsg{
		Code:    code,
		Message: code.String(),
	}
}

var (
	ErrCodeMsgENo error = NewCodeMsg(ENo) //号码出错

	ErrCodeMsgENotFound error = NewCodeMsg(ENotFound) //找不到

)
./h2o codemsg --code-msg --linecomment --string --type ErrNo ./testdata/err.go
// 如果要修改生成的String方法名, 可以通过--string-method String2  选项
// 如果要生成grpc的错误就用下面的命令
// h2o codemsg --code-msg --linecomment --type ErrNo ./testdata/err.go --grpc --string-method string2 --string

Directories

Path Synopsis
cmd
h2o
internal
url

Jump to

Keyboard shortcuts

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