Adding ability to use JSON Names for pb fields#1220
Closed
renevo wants to merge 1 commit into
Closed
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
Author
|
Is there more information/work I can do on this PR to move one way or the other? |
Collaborator
|
/gcbrun |
| for i := 0; i < fields.Len(); i++ { | ||
| f := fields.Get(i) | ||
| fieldMap[string(f.Name())] = newFieldDescription(f) | ||
| if UseJSONFieldNames { |
Collaborator
There was a problem hiding this comment.
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.
Collaborator
There was a problem hiding this comment.
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
}
Collaborator
There was a problem hiding this comment.
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)
}
Author
There was a problem hiding this comment.
let me look into that, thanks for the response.
Author
|
This was implemented in #1286 closing this out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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