Skip to content

Commit

Permalink
fix protoveneer support functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jba committed Dec 12, 2023
1 parent d760d3e commit 49a56fd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
28 changes: 14 additions & 14 deletions genai/generativelanguagepb_veneer.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (v *Candidate) toProto() *pb.Candidate {
return nil
}
return &pb.Candidate{
Index: support.ZeroToNil(v.Index),
Index: support.AddrOrNil(v.Index),
Content: v.Content.toProto(),
FinishReason: pb.Candidate_FinishReason(v.FinishReason),
SafetyRatings: support.TransformSlice(v.SafetyRatings, (*SafetyRating).toProto),
Expand All @@ -124,7 +124,7 @@ func (Candidate) fromProto(p *pb.Candidate) *Candidate {
return nil
}
return &Candidate{
Index: support.NilToZero(p.Index),
Index: support.DerefOrZero(p.Index),
Content: (Content{}).fromProto(p.Content),
FinishReason: FinishReason(p.FinishReason),
SafetyRatings: support.TransformSlice(p.SafetyRatings, (SafetyRating{}).fromProto),
Expand Down Expand Up @@ -183,7 +183,7 @@ func (v *CitationSource) toProto() *pb.CitationSource {
StartIndex: v.StartIndex,
EndIndex: v.EndIndex,
Uri: v.URI,
License: support.ZeroToNil(v.License),
License: support.AddrOrNil(v.License),
}
}

Expand All @@ -195,7 +195,7 @@ func (CitationSource) fromProto(p *pb.CitationSource) *CitationSource {
StartIndex: p.StartIndex,
EndIndex: p.EndIndex,
URI: p.Uri,
License: support.NilToZero(p.License),
License: support.DerefOrZero(p.License),
}
}

Expand Down Expand Up @@ -461,12 +461,12 @@ func (v *GenerationConfig) toProto() *pb.GenerationConfig {
return nil
}
return &pb.GenerationConfig{
CandidateCount: support.ZeroToNil(v.CandidateCount),
CandidateCount: support.AddrOrNil(v.CandidateCount),
StopSequences: v.StopSequences,
MaxOutputTokens: support.ZeroToNil(v.MaxOutputTokens),
Temperature: support.ZeroToNil(v.Temperature),
TopP: support.ZeroToNil(v.TopP),
TopK: support.ZeroToNil(v.TopK),
MaxOutputTokens: support.AddrOrNil(v.MaxOutputTokens),
Temperature: support.AddrOrNil(v.Temperature),
TopP: support.AddrOrNil(v.TopP),
TopK: support.AddrOrNil(v.TopK),
}
}

Expand All @@ -475,12 +475,12 @@ func (GenerationConfig) fromProto(p *pb.GenerationConfig) *GenerationConfig {
return nil
}
return &GenerationConfig{
CandidateCount: support.NilToZero(p.CandidateCount),
CandidateCount: support.DerefOrZero(p.CandidateCount),
StopSequences: p.StopSequences,
MaxOutputTokens: support.NilToZero(p.MaxOutputTokens),
Temperature: support.NilToZero(p.Temperature),
TopP: support.NilToZero(p.TopP),
TopK: support.NilToZero(p.TopK),
MaxOutputTokens: support.DerefOrZero(p.MaxOutputTokens),
Temperature: support.DerefOrZero(p.Temperature),
TopP: support.DerefOrZero(p.TopP),
TopK: support.DerefOrZero(p.TopK),
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/protoveneer/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func (identityConverter) genTransformTo() string { return "" }
// A derefConverter converts between T in the veneer and *T in the proto.
type derefConverter struct{}

func (derefConverter) genFrom(arg string) string { return fmt.Sprintf("support.NilToZero(%s)", arg) }
func (derefConverter) genTo(arg string) string { return fmt.Sprintf("support.ZeroToNil(%s)", arg) }
func (derefConverter) genFrom(arg string) string { return fmt.Sprintf("support.DerefOrZero(%s)", arg) }
func (derefConverter) genTo(arg string) string { return fmt.Sprintf("support.AddrOrNil(%s)", arg) }
func (derefConverter) genTransformFrom() string { panic("can't handle deref slices") }
func (derefConverter) genTransformTo() string { panic("can't handle deref slices") }

Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/protoveneer/testdata/basic/golden
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ func (v *GenerationConfig) toProto() *pb.GenerationConfig {
return nil
}
return &pb.GenerationConfig{
Temperature: support.ZeroToNil(v.Temperature),
CandidateCount: support.ZeroToNil(v.CandidateCount),
Temperature: support.AddrOrNil(v.Temperature),
CandidateCount: support.AddrOrNil(v.CandidateCount),
StopSequences: v.StopSequences,
HarmCat: pb.HarmCategory(v.HarmCat),
FinishReason: pb.Candidate_FinishReason(v.FinishReason),
Expand All @@ -182,8 +182,8 @@ func (GenerationConfig) fromProto(p *pb.GenerationConfig) *GenerationConfig {
return nil
}
return &GenerationConfig{
Temperature: support.NilToZero(p.Temperature),
CandidateCount: support.NilToZero(p.CandidateCount),
Temperature: support.DerefOrZero(p.Temperature),
CandidateCount: support.DerefOrZero(p.CandidateCount),
StopSequences: p.StopSequences,
HarmCat: HarmCategory(p.HarmCat),
FinishReason: FinishReason(p.FinishReason),
Expand Down
8 changes: 4 additions & 4 deletions internal/support/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ func TransformMapValues[K comparable, VFrom, VTo any](from map[K]VFrom, f func(V
return to
}

// ZeroToNil returns nil if x is the zero value for T,
// AddrOrNil returns nil if x is the zero value for T,
// or &x otherwise.
func ZeroToNil[T comparable](x T) *T {
func AddrOrNil[T comparable](x T) *T {
var z T
if x == z {
return nil
}
return &x
}

// NilToZero returns the zero value for T if x is nil,
// DerefOrZero returns the zero value for T if x is nil,
// or *x otherwise.
func NilToZero[T any](x *T) T {
func DerefOrZero[T any](x *T) T {
if x == nil {
var z T
return z
Expand Down

0 comments on commit 49a56fd

Please sign in to comment.