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

feat: support generating GetAnnotation method for enum #202

Merged
merged 4 commits into from
May 13, 2024

Conversation

DMwangnima
Copy link
Contributor

@DMwangnima DMwangnima commented May 13, 2024

Description

  1. options 中设置 get_enum_annotation
    -g go:get_enum_annotation
  • 生成内容:
  1. idl
enum KitexEnum {
    ONE(key1="val1")
    TWO(key2="val21,val22")
    THREE(key3="")
    FOUR(key41="val41,val42", key42="val43,val44")
    FIVE(key5="val51", key5="val52")
    SIX(key6="\"val6\"")
    SEVEN(key7='\"val7\"')
    EIGHT(key8='"val8"')
    NINE(key9='\'val9\'')
}
  1. 生成产物
var annotations_KitexEnum = map[KitexEnum]map[string][]string{
	KitexEnum_ONE: map[string][]string{
		`key1`: []string{`val1`},
	},
	KitexEnum_TWO: map[string][]string{
		`key2`: []string{`val21,val22`},
	},
	KitexEnum_THREE: map[string][]string{
		`key3`: []string{``},
	},
	KitexEnum_FOUR: map[string][]string{
		`key41`: []string{`val41,val42`},
		`key42`: []string{`val43,val44`},
	},
	KitexEnum_FIVE: map[string][]string{
		`key5`: []string{`val51`, `val52`},
	},
	KitexEnum_SIX: map[string][]string{
		`key6`: []string{`"val6"`},
	},
	KitexEnum_SEVEN: map[string][]string{
		`key7`: []string{`\"val7\"`},
	},
	KitexEnum_EIGHT: map[string][]string{
		`key8`: []string{`"val8"`},
	},
	KitexEnum_NINE: map[string][]string{
		`key9`: []string{`'val9'`},
	},
}

func (p KitexEnum) GetAnnotation(key string) []string {
	switch p {
	case KitexEnum_ONE:
		return annotations_KitexEnum[KitexEnum_ONE][key]
	case KitexEnum_TWO:
		return annotations_KitexEnum[KitexEnum_TWO][key]
	case KitexEnum_THREE:
		return annotations_KitexEnum[KitexEnum_THREE][key]
	case KitexEnum_FOUR:
		return annotations_KitexEnum[KitexEnum_FOUR][key]
	case KitexEnum_FIVE:
		return annotations_KitexEnum[KitexEnum_FIVE][key]
	case KitexEnum_SIX:
		return annotations_KitexEnum[KitexEnum_SIX][key]
	case KitexEnum_SEVEN:
		return annotations_KitexEnum[KitexEnum_SEVEN][key]
	case KitexEnum_EIGHT:
		return annotations_KitexEnum[KitexEnum_EIGHT][key]
	case KitexEnum_NINE:
		return annotations_KitexEnum[KitexEnum_NINE][key]
	}
	return nil
}

Motivation and Context

Related Issue

@DMwangnima DMwangnima requested review from a team as code owners May 13, 2024 01:46
generator/golang/option.go Outdated Show resolved Hide resolved
generator/golang/util.go Outdated Show resolved Hide resolved
HeyJavaBean
HeyJavaBean previously approved these changes May 13, 2024
@DMwangnima
Copy link
Contributor Author

Test Result:

  1. cases_and_options
(default setting)
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[naming_style=thriftgo]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[naming_style=golint]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[naming_style=apache]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[ignore_initialisms]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[json_enum_as_text]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[gen_setter]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[gen_db_tag]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[omitempty_for_optional=false]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[validate_set=false]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[value_type_in_container]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[scan_value_for_enum]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[reorder_fields]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[typed_enum_string]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[keep_unknown_fields]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[gen_deep_equal]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[reserve_comments]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[compatible_names]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[nil_safe]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[frugal_tag]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[unescape_double_quote]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
[json_stringer]
  case 'combinations': ok
  case 'cross_idl': ok
  case 'getter_setter': ok
  case 'import_not_use': ok
  case 'namespace': ok
  case 'namespace2': ok
  case 'namespace3': ok
  case 'negative_field_id': ok
  case 'param_name_conflict': ok
  case 'relative_path': ok
  case 'same_idl_name': ok
  case 'thrifttest': ok
  1. fieldmask
thriftgo -g go:package_prefix=github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-old -o gen-old a.thrift
[WARN] non-positive ID 0 of field "Name" in "TrafficEnv"
[WARN] non-positive ID 0 of field "Addr" in "Base"
[WARN] non-positive ID 0 of field "R0" in "BaseResp"
thriftgo -g go:package_prefix=github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-new,with_field_mask,with_reflection -o gen-new a.thrift
[WARN] non-positive ID 0 of field "Name" in "TrafficEnv"
[WARN] non-positive ID 0 of field "Addr" in "Base"
[WARN] non-positive ID 0 of field "R0" in "BaseResp"
thriftgo -g go:package_prefix=github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-halfway,with_field_mask,field_mask_halfway,with_reflection -o gen-halfway b.thrift
[WARN] non-positive ID 0 of field "Name" in "TrafficEnv"
[WARN] non-positive ID 0 of field "Addr" in "Base"
[WARN] non-positive ID 0 of field "R0" in "BaseResp"
thriftgo -g go:package_prefix=github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-zero,with_field_mask,field_mask_zero_required,with_reflection -o gen-zero c.thrift
[WARN] non-positive ID 0 of field "Name" in "TrafficEnv"
[WARN] non-positive ID 0 of field "Addr" in "Base"
[WARN] non-positive ID 0 of field "R0" in "BaseResp"
?       github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-halfway/base    [no test files]
?       github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-new/base        [no test files]
?       github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-old/base        [no test files]
?       github.com/cloudwego/thriftgo/test/golang/fieldmask/gen-zero/base       [no test files]
=== RUN   TestFieldMask_Write
--- PASS: TestFieldMask_Write (0.00s)
=== RUN   TestFieldMask_Write_Black
--- PASS: TestFieldMask_Write_Black (0.00s)
=== RUN   TestFieldMask_Read
--- PASS: TestFieldMask_Read (0.00s)
=== RUN   TestFieldMask_Read_Black
--- PASS: TestFieldMask_Read_Black (0.00s)
=== RUN   TestMaskRequired
{"path":"$","type":"Struct","is_black":false,"children":[{"path":12,"type":"Struct","is_black":false,"children":[{"path":2,"type":"Scalar","is_black":false}]},{"path":14,"type":"StrMap","is_black":false},{"path":111,"type":"Scalar","is_black":false}]}
=== RUN   TestMaskRequired/read
=== RUN   TestMaskRequired/current-write
=== RUN   TestMaskRequired/zero-required-write
&base.BaseResp{StatusMessage:"", StatusCode:0, R3:false, R4:0, R5:0, R6:0, R7:0, R8:"", R9:0, R10:[]*base.Val{}, R11:[]*base.Val{}, R12:(*base.TrafficEnv)(0x1400023b880), R13:map[string]*base.Val{}, R0:(*base.Val)(0x14000253f50), F1:map[string]string{"a":"b"}, F2:map[int64]string{}, F3:[]string{}, F4:[]string{}, F5:map[float64]*base.Val{}, F6:map[float64]string{}, F7:map[base.Ex]string{}, F8:map[float64][]string{1:[]string{"a"}}, F9:[]map[float64][]string{}, F10:map[*base.Val]*base.Val{}, _fieldmask:(*fieldmask.FieldMask)(nil)}
--- PASS: TestMaskRequired (0.00s)
    --- PASS: TestMaskRequired/read (0.00s)
    --- PASS: TestMaskRequired/current-write (0.00s)
    --- PASS: TestMaskRequired/zero-required-write (0.00s)
=== RUN   TestSetMaskHalfway
--- PASS: TestSetMaskHalfway (0.00s)
PASS
ok      github.com/cloudwego/thriftgo/test/golang/fieldmask     0.444s
  1. unknown_fields
=== RUN   TestEncodeDecode
--- PASS: TestEncodeDecode (0.00s)
=== RUN   TestTransfer
--- PASS: TestTransfer (0.00s)
PASS
ok      example.com/test        0.340s

@DMwangnima DMwangnima merged commit 5ef2d54 into cloudwego:main May 13, 2024
4 checks passed
@DMwangnima DMwangnima deleted the feat/get_enum_annotations branch May 13, 2024 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants