@@ -478,7 +478,7 @@ func (c *LekkoClient) {{$.FuncName}}({{$.ArgumentString}}) {{$.RetType}} {
478
478
}` ,
479
479
private : `// {{$.Description}}
480
480
func {{$.PrivateFunc}}({{$.ArgumentString}}) {{$.RetType}} {
481
- {{range $.NaturalLanguage }}{{ . }}
481
+ {{range $.NativeLanguage }}{{ . }}
482
482
{{end}}}` ,
483
483
}
484
484
}
@@ -498,7 +498,7 @@ func (c *LekkoClient) {{$.FuncName}}({{$.ArgumentString}}) *{{$.RetType}} {
498
498
}` ,
499
499
private : `// {{$.Description}}
500
500
func {{$.PrivateFunc}}({{$.ArgumentString}}) *{{$.RetType}} {
501
- {{range $.NaturalLanguage }}{{ . }}
501
+ {{range $.NativeLanguage }}{{ . }}
502
502
{{end}}}` ,
503
503
}
504
504
}
@@ -531,7 +531,7 @@ const (
531
531
532
532
// {{$.Description}}
533
533
func {{$.PrivateFunc}}({{$.ArgumentString}}) {{$.RetType}} {
534
- {{range $.NaturalLanguage }}{{ . }}
534
+ {{range $.NativeLanguage }}{{ . }}
535
535
{{end}}}` ,
536
536
}
537
537
}
@@ -613,19 +613,19 @@ func (g *goGenerator) genGoForFeature(ctx context.Context, r repo.ConfigurationR
613
613
}
614
614
615
615
data := struct {
616
- Description string
617
- FuncName string
618
- PrivateFunc string
619
- GetFunction string
620
- RetType string
621
- Namespace string
622
- Key string
623
- NaturalLanguage []string
624
- ArgumentString string
625
- CallString string
626
- EnumTypeName string
627
- EnumFields []EnumField
628
- CtxStuff string
616
+ Description string
617
+ FuncName string
618
+ PrivateFunc string
619
+ GetFunction string
620
+ RetType string
621
+ Namespace string
622
+ Key string
623
+ NativeLanguage []string
624
+ ArgumentString string
625
+ CallString string
626
+ EnumTypeName string
627
+ EnumFields []EnumField
628
+ CtxStuff string
629
629
}{
630
630
f .Description ,
631
631
funcName ,
@@ -644,7 +644,7 @@ func (g *goGenerator) genGoForFeature(ctx context.Context, r repo.ConfigurationR
644
644
generated := & generatedConfigCode {}
645
645
usedVariables := make (map [string ]string )
646
646
if staticCtxType != nil {
647
- data .NaturalLanguage = g .translateFeature (f , protoType , true , usedVariables , & generated .usedStrings , & generated .usedSlices )
647
+ data .NativeLanguage = g .translateFeature (f , protoType , true , usedVariables , & generated .usedStrings , & generated .usedSlices )
648
648
if staticCtxType .PackageAlias != "" {
649
649
data .ArgumentString = fmt .Sprintf ("args *%s.%s" , staticCtxType .PackageAlias , staticCtxType .Type )
650
650
} else {
@@ -653,7 +653,7 @@ func (g *goGenerator) genGoForFeature(ctx context.Context, r repo.ConfigurationR
653
653
data .CallString = "args"
654
654
} else {
655
655
data .CtxStuff = "args := context.Background()\n "
656
- data .NaturalLanguage = g .translateFeature (f , protoType , false , usedVariables , & generated .usedStrings , & generated .usedSlices )
656
+ data .NativeLanguage = g .translateFeature (f , protoType , false , usedVariables , & generated .usedStrings , & generated .usedSlices )
657
657
var arguments []string
658
658
var ctxAddLines []string
659
659
for f , t := range usedVariables {
@@ -896,13 +896,7 @@ func (g *goGenerator) translateProtoNonRepeatedValue(parent protoreflect.Message
896
896
// TODO: Actually handle enums, right now they're just numbers
897
897
return val .String ()
898
898
case protoreflect .StringKind :
899
- // If the string value is multiline, transform to raw literal instead
900
- valString := val .String ()
901
- quote := "\" "
902
- if strings .Count (valString , "\n " ) > 0 {
903
- quote = "`"
904
- }
905
- return fmt .Sprintf ("%s%s%s" , quote , val .String (), quote )
899
+ return g .toQuoted (val .String ())
906
900
case protoreflect .BoolKind :
907
901
return val .String ()
908
902
case protoreflect .BytesKind :
@@ -945,12 +939,7 @@ func (g *goGenerator) translateAnyValue(val *anypb.Any, protoType *ProtoImport)
945
939
if val .MessageIs ((* wrapperspb .StringValue )(nil )) {
946
940
var s wrapperspb.StringValue
947
941
try .To (val .UnmarshalTo (& s ))
948
- // If multiline string value, gen as raw string literal instead
949
- quote := "\" "
950
- if strings .Count (s .Value , "\n " ) > 0 {
951
- quote = "`"
952
- }
953
- return fmt .Sprintf ("%s%s%s" , quote , s .Value , quote )
942
+ return g .toQuoted (s .Value )
954
943
}
955
944
return string (try .To1 (protojson .Marshal (msg )))
956
945
}
@@ -977,6 +966,16 @@ func (g *goGenerator) translateProtoValue(parent protoreflect.Message, val proto
977
966
}
978
967
}
979
968
969
+ // Takes a string and returns a double-quoted literal with applicable internal characters escaped, etc.
970
+ // For strings with newlines, returns a raw string literal instead.
971
+ func (g * goGenerator ) toQuoted (s string ) string {
972
+ if strings .Count (s , "\n " ) > 0 {
973
+ return fmt .Sprintf ("`%s`" , s )
974
+ }
975
+ // Quote automatically handles escaping, etc.
976
+ return strconv .Quote (s )
977
+ }
978
+
980
979
// Handles getting import & type literal information for both top-level and nested messages.
981
980
// TODO: Consider moving logic into UnpackProtoType directly which is shared with TS codegen as well
982
981
// TODO: This can definitely be cached, and doesn't need values, just descriptors
0 commit comments