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

支持自定义protobuf生成字段自定义的json tag #3

Open
onecer opened this issue Sep 9, 2022 · 1 comment
Open

支持自定义protobuf生成字段自定义的json tag #3

onecer opened this issue Sep 9, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@onecer
Copy link

onecer commented Sep 9, 2022

What is the feature you want to add?
我们protobuf结构生成的struct如下,json tag默认带着omitempty,忽略空值的。

type ClientWatchConfigFileRequest struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

	ClientIp    *wrappers.StringValue   `protobuf:"bytes,1,opt,name=client_ip,json=clientIp,proto3" json:"client_ip,omitempty"`
	ServiceName *wrappers.StringValue   `protobuf:"bytes,2,opt,name=service_name,json=serviceName,proto3" json:"service_name,omitempty"`
	WatchFiles  []*ClientConfigFileInfo `protobuf:"bytes,3,rep,name=watch_files,json=watchFiles,proto3" json:"watch_files,omitempty"`
}

这在用go-restful-openapi 处理生成swagger的model的时候,遇到以下几个问题。

  1. 字段都无法生成出来,因为带着 omitempty 。
    这是它处理逻辑
func (b definitionBuilder) isPropertyRequired(field reflect.StructField) bool {
	required := true
	if optionalTag := field.Tag.Get("optional"); optionalTag == "true" {
		return false
	}
	if jsonTag := field.Tag.Get("json"); jsonTag != "" {
		s := strings.Split(jsonTag, ",")
		if len(s) > 1 && s[1] == "omitempty" {
			return false
		}
	}
	return required
}
---
	for i := 0; i < st.NumField(); i++ {
		field := st.Field(i)
		jsonName, modelDescription, prop := b.buildProperty(field, &sm, modelName)
		if len(modelDescription) > 0 {
			modelDescriptions = append(modelDescriptions, modelDescription)
		}

		// add if not omitted
		if len(jsonName) != 0 {
			// update description
			if fieldDoc, ok := fullDoc[jsonName]; ok {
				prop.Description = fieldDoc
			}
			// update Required
			if b.isPropertyRequired(field) {
				sm.Required = append(sm.Required, jsonName)
			}
			sm.Properties[jsonName] = prop
		}
	}

生成后的字段只有

	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

而这个是是我们不需要的。因此我们需要支持自定义protobuf生成的json tag。比如新增 default和description字段,和删除omitempty字段。
支持删除或者屏蔽掉state、sizeCache和unknownFields字段

protobuf的类型字段是一个结构·wrappers.StringValue· 它会被认为是嵌套的结构类型,也会被解析为结构,而我们不希望它被解析。

@chuntaojun

Why do you want to add this feature?

How to implement this feature?

Additional context
Add any other context or screenshots about the feature request here.

@onecer onecer added the enhancement New feature or request label Sep 9, 2022
@chuntaojun
Copy link
Member

这个我来处理吧

@chuntaojun chuntaojun transferred this issue from polarismesh/polaris Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants