Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a tool for generate hessian2 java enum define golang code #304

Merged
merged 8 commits into from
Dec 30, 2021

Conversation

marsmay
Copy link
Contributor

@marsmay marsmay commented Dec 29, 2021

a tool for generate hessian2 java enum define golang code.

go build -o gen-go-enum tools/gen-go-enum/main.go

For example, run command gen-go-enum -c com.test.enums.TestColorEnum -v RED -v BLUE -v YELLOW will create a golang code file like this.

package enum

import (
	"strconv"
)

import (
	hessian "github.com/apache/dubbo-go-hessian2"
)

const (
	TestColorEnumRed TestColorEnum = iota
	TestColorEnumBlue
	TestColorEnumYellow
)

var _TestColorEnumValues = map[TestColorEnum]string{
	TestColorEnumRed: "RED",
	TestColorEnumBlue: "BLUE",
	TestColorEnumYellow: "YELLOW",
}

var _TestColorEnumEntities = map[string]TestColorEnum{
	"RED": TestColorEnumRed,
	"BLUE": TestColorEnumBlue,
	"YELLOW": TestColorEnumYellow,
}

type TestColorEnum hessian.JavaEnum

func (e TestColorEnum) JavaClassName() string {
	return "com.test.enums.TestColorEnum"
}

func (e TestColorEnum) String() string {
	if v, ok := _TestColorEnumValues[e]; ok {
		return v
	}

	return strconv.Itoa(int(e))
}

func (e TestColorEnum) EnumValue(s string) hessian.JavaEnum {
	if v, ok := _TestColorEnumEntities[s]; ok {
		return hessian.JavaEnum(v)
	}

	return hessian.InvalidJavaEnum
}

func NewTestColorEnum(s string) TestColorEnum {
	if v, ok := _TestColorEnumEntities[s]; ok {
		return v
	}

	return TestColorEnum(hessian.InvalidJavaEnum)
}

func init() {
	for v := range _TestColorEnumValues {
		hessian.RegisterJavaEnum(v)
	}
}

You can specify more options, like the usage.

gen-go-enum can generate golang code for hessian2 java enum.

Usage: gen-go-enum -c java_classname [-p golang_package_name] [-e golang_enum_name] -v java_enum_value [-v java_enum_value] [-o target_file]

Options
  -c	java class name (eg: com.test.enums.TestEnum)
  -p	golang package name, use 'enum' when not specified (eg: test_enum)
  -e	golang enum type name, use java class name when not specified (eg: TestEnum)
  -v	java enum values, can specify multiple (eg: -v TEST1 -v TEST2 -v TEST3)
  -o 	golang code file path, stored in the current directory when not specified

Example
  gen-go-enum -c com.test.enums.TestColorEnum -v RED -v BLUE -v YELLOW
  gen-go-enum -c com.test.enums.TestColorEnum -p test_enums -e ColorEnum -v RED -v BLUE -v YELLOW -o ./color_enum.go

@wongoo
Copy link
Contributor

wongoo commented Dec 29, 2021

@marsmay nice work!

Copy link
Contributor

@wongoo wongoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@codecov-commenter
Copy link

codecov-commenter commented Dec 29, 2021

Codecov Report

Merging #304 (a6561fd) into master (be67b60) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #304   +/-   ##
=======================================
  Coverage   70.69%   70.69%           
=======================================
  Files          27       27           
  Lines        3027     3027           
=======================================
  Hits         2140     2140           
  Misses        661      661           
  Partials      226      226           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update be67b60...a6561fd. Read the comment docs.

@AlexStocks
Copy link
Contributor

@marsmay pls add introduction of this tool in readme.

@seeflood
Copy link

niu b

@LaurenceLiZhixin
Copy link
Contributor

LGTM (老哥太猛)

@wongoo wongoo merged commit 0b16449 into apache:master Dec 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants