-
Notifications
You must be signed in to change notification settings - Fork 250
Adding ability to use JSON Names for pb fields #1220
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
base: master
Are you sure you want to change the base?
Conversation
This adds a simple variable to map the fields to the JSONName() instead of Name() on the protobuf field descriptors. This allows for using cel expressions against the same JSON representations in some scenarios. The default behavior is identical. Test has been added to validate field names with setting turned on.
2905c5e to
c81c195
Compare
|
Is there more information/work I can do on this PR to move one way or the other? |
|
/gcbrun |
| for i := 0; i < fields.Len(); i++ { | ||
| f := fields.Get(i) | ||
| fieldMap[string(f.Name())] = newFieldDescription(f) | ||
| if UseJSONFieldNames { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you move this flag up as a function argument, you should be able to pass the same argument into the pb.Db instance which generates the internal type descriptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically, you'll want to update the common/types/pb.go definition for the ``Db` struct:
type fieldNameKind int
const (
fieldNameKind proto = iota
json
jsonOrProto
)
type Db struct {
revFileDescriptorMap map[string]*FileDescription
// files contains the deduped set of FileDescriptions whose types are contained in the pb.Db.
files []*FileDescription
// extensions contains the mapping between a given type name, extension name and its FieldDescription
extensions map[string]map[string]*FieldDescription
// fieldNameConvention supports type resolution using either the protobuf name or its JSON name
fieldNameConvention fieldNameKind
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would then need to support a functional option to the NewDb call:
// NewDb creates a new `pb.Db` with an empty type name to file description map.
func NewDb(DbOption ...opts) *Db {
pbdb := &Db{ ... }
for _, opt := range opts {
pbdb = opt(pbdb)
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me look into that, thanks for the response.
This adds a simple variable to map the fields to the JSONName() instead of Name() on the protobuf field descriptors.
This allows for using cel expressions against the same JSON representations in some scenarios.
The default behavior is identical. Test has been added to validate field names with setting turned on and all tests pass.
Closes #1214