Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@renevo
Copy link

@renevo renevo commented Sep 25, 2025

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

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.
@renevo renevo force-pushed the json-fieldname-option branch from 2905c5e to c81c195 Compare September 25, 2025 23:05
@renevo
Copy link
Author

renevo commented Oct 13, 2025

Is there more information/work I can do on this PR to move one way or the other?

@TristonianJones
Copy link
Collaborator

/gcbrun

for i := 0; i < fields.Len(); i++ {
f := fields.Get(i)
fieldMap[string(f.Name())] = newFieldDescription(f)
if UseJSONFieldNames {
Copy link
Collaborator

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.

Copy link
Collaborator

@TristonianJones TristonianJones Oct 14, 2025

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
}

Copy link
Collaborator

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)
	}

Copy link
Author

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.

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.

Support JSON Name in pb field names

2 participants