thriftgoplugin

package module
v0.1.15 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

README

Thrift Go Plugin

Overview

This is a Thrift Go Plugin project designed to extend and enhance Thrift code generation capabilities for Go projects. It provides a flexible framework for customizing Thrift code generation using the CloudWeGo Thriftgo library.

Features

  • Custom Thrift code generation for Go
  • Extensible plugin architecture
  • Supports Thriftgo v0.3.18

Prerequisites

  • Go 1.23.3 or later
  • CloudWeGo Thriftgo v0.3.18

Installation

Install via go get:

go get gitee.com/aesoper/thriftgoplugin

Or clone the repository:

git clone https://gitee.com/aesoper/thriftgoplugin.git
cd thriftgoplugin
go mod download

Usage

To use the plugin in your Thrift code generation process:

import (
    "context"
    "gitee.com/aesoper/thriftgoplugin"
    "github.com/cloudwego/thriftgo/plugin"
)

type MyHandler struct{}

func (h *MyHandler) Handle(ctx context.Context, env thriftgoplugin.PluginEnv, req thriftgoplugin.CodeGeneratorRequest, resp thriftgoplugin.CodeGeneratorResponse) error {
    // Implement your custom handling logic
    resp.AddFile("test.go", "package main\n\nfunc main() {\n\tprintln(\"Hello, World!\")\n}")
    return nil
}

func main() {
    handler := &MyHandler{}
    thriftgoplugin.Main(handler, thriftgoplugin.WithVersion("0.0.1"))
}

Development

Running Tests
go test ./...
Building
go build

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the Apache License 2.0. See LICENSE for more information.

Contact

Project Link: https://gitee.com/aesoper/thriftgoplugin

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombineOutputPath added in v0.1.15

func CombineOutputPath(outputPath, ns string) string

func Debug added in v0.1.1

func Debug(format string, a ...any)

func Main

func Main(handler Handler, mainOptions ...Option)

Main is the main entry point for the thriftgen plugin.

func Run added in v0.1.1

func Run(
	ctx context.Context,
	env Env,
	handler Handler,
	runOptions ...Option,
) error

Run runs the thriftgo plugin using the Handler for the given environment.

Types

type CodeGeneratorRequest added in v0.1.1

type CodeGeneratorRequest interface {
	// RawRequest returns raw thriftgo plugin request
	RawRequest() *tplugin.Request

	// CodeUtils returns golang code utils
	// CodeUtils returns golang code utils
	CodeUtils() *golang.CodeUtils

	// GetOutputPath returns output path
	GetOutputPath() string

	// Ast returns thrift ast
	Ast() *parser.Thrift

	// GetReplaceImportDesc returns replace import desc
	GetReplaceImportDesc() []ReplaceImportDesc

	// GetSameBatchFiles returns all same batch files,
	// you can choose to generate these files by yourself.
	// Notes: it includes all thrift files (include the current file).
	GetSameBatchFiles() []string
}

CodeGeneratorRequest is thriftgo plugin request

type CodeGeneratorResponse added in v0.1.1

type CodeGeneratorResponse interface {
	// AddFile add file to response
	AddFile(name, content string)
	// AddError add error to response
	AddError(message string)
	// AddWarning add warning to response
	AddWarning(message ...string)
	// AddCodeGeneratorResponseFiles add code generator response files to response
	AddCodeGeneratorResponseFiles(files ...*tplugin.Generated)

	// RawResponse returns raw thriftgo plugin response
	RawResponse() *tplugin.Response
}

CodeGeneratorResponse is thriftgo plugin response

type Env added in v0.1.1

type Env struct {
	Args []string // 插件参数

	Environ []string // 环境变量

	Stdout io.Writer // 标准输出
	Stderr io.Writer // 标准错误输出
	Stdin  io.Reader // 标准输入
}

type Handler added in v0.1.1

type HandlerFunc added in v0.1.1

func (HandlerFunc) Handle added in v0.1.1

type Option added in v0.1.1

type Option func(opts *options)

func WithHelpFunc added in v0.1.6

func WithHelpFunc(helpFunc func()) Option

WithHelpFunc sets the help function for the plugin.

func WithParamFunc added in v0.1.1

func WithParamFunc(paramFunc func(name, value string) error) Option

WithParamFunc sets the parameter function for the plugin. eg: flag.Set or pflag.Set

func WithVersion added in v0.1.1

func WithVersion(version string) Option

WithVersion sets the version for the plugin.

type PluginEnv added in v0.1.1

type PluginEnv struct {
	Environ []string // 环境变量

	Stderr io.Writer // 标准错误输出
}

type ReplaceImportDesc added in v0.1.7

type ReplaceImportDesc struct {
	Old          string // 旧的包路径
	New          string // 新的包路径
	BaseFilename string // 基础文件名 . eg: demo.thrift
	Filename     string // 文件名 . eg: foo/demo.thrift
}

Jump to

Keyboard shortcuts

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