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

chore(internal/protoveneer): support custom field converters #9456

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/protoveneer/cmd/protoveneer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ type fieldConfig struct {
Type string // veneer type
// Omit from output.
Omit bool
// Custom conversion functions: "tofunc, fromfunc"
ConvertToFrom string `yaml:"convertToFrom"`
}

func (c *config) init() {
Expand Down
10 changes: 10 additions & 0 deletions internal/protoveneer/cmd/protoveneer/protoveneer.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ func generate(conf *config, pkg *ast.Package, fset *token.FileSet) (src []byte,
// Use the converters map to give every field a converter.
for _, ti := range toWrite {
for _, f := range ti.fields {
if f.converter != nil {
continue
}
f.converter, err = makeConverter(f.af.Type, f.protoType, converters)
if err != nil {
return nil, fmt.Errorf("%s.%s: %w", ti.protoName, f.protoName, err)
Expand Down Expand Up @@ -482,6 +485,13 @@ func processField(af *ast.Field, tc *typeConfig, typeInfos map[string]*typeInfo)
}
af.Type = expr
}
if fc.ConvertToFrom != "" {
c, err := parseCustomConverter(id.Name, fc.ConvertToFrom)
if err != nil {
return nil, err
}
fi.converter = c
}
}
}
af.Type = veneerType(af.Type, typeInfos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type GenerationConfig struct {
HarmCat HarmCategory
FinishReason Candidate_FinishReason
CitMet *CitationMetadata
TopK *float32
}

// A collection of source attributions for a piece of content.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ types:
type: float32
CandidateCount:
type: int32
TopK:
type: '*int32'
convertToFrom: int32pToFloat32p, float32pToInt32p

Citation:
docVerb: contains
Expand Down
3 changes: 3 additions & 0 deletions internal/protoveneer/cmd/protoveneer/testdata/basic/golden
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ type GenerationConfig struct {
HarmCat HarmCategory
FinishReason FinishReason
CitMet *CitationMetadata
TopK *int32
}

func (v *GenerationConfig) toProto() *pb.GenerationConfig {
Expand All @@ -160,6 +161,7 @@ func (v *GenerationConfig) toProto() *pb.GenerationConfig {
HarmCat: pb.HarmCategory(v.HarmCat),
FinishReason: pb.Candidate_FinishReason(v.FinishReason),
CitMet: v.CitMet.toProto(),
TopK: int32pToFloat32p(v.TopK),
}
}

Expand All @@ -174,6 +176,7 @@ func (GenerationConfig) fromProto(p *pb.GenerationConfig) *GenerationConfig {
HarmCat: HarmCategory(p.HarmCat),
FinishReason: FinishReason(p.FinishReason),
CitMet: (CitationMetadata{}).fromProto(p.CitMet),
TopK: float32pToInt32p(p.TopK),
}
}

Expand Down