Skip to content

Commit

Permalink
Merge pull request #2 from BeProductable/seanami/url-param-wrapper-types
Browse files Browse the repository at this point in the history
Handle well-known wrapper types with native Typescript types, matching gprc-gateway serialization
  • Loading branch information
Sean McBride authored Mar 24, 2023
2 parents e8b32fc + 64bc751 commit df4e4d3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
36 changes: 22 additions & 14 deletions generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,6 @@ function isPrimitive(value: unknown): boolean {
return ["string", "number", "boolean"].some(t => typeof value === t);
}
/**
* Checks if given primitive is zero-value
* @param {Primitive} value
* @return {boolean}
*/
function isZeroValuePrimitive(value: Primitive): boolean {
return value === false || value === 0 || value === "";
}
/**
* Flattens a deeply nested request payload and returns an object
* with only primitive values and non-empty array of primitive values
Expand All @@ -391,14 +382,11 @@ function flattenRequestPayload<T extends RequestPayload>(
value.every(v => isPrimitive(v)) &&
value.length > 0;
const isNonZeroValuePrimitive =
isPrimitive(value) && !isZeroValuePrimitive(value as Primitive);
let objectToMerge = {};
if (isPlainObject(value)) {
objectToMerge = flattenRequestPayload(value as RequestPayload, newPath);
} else if (isNonZeroValuePrimitive || isNonEmptyPrimitiveArray) {
} else if (isPrimitive(value) || isNonEmptyPrimitiveArray) {
objectToMerge = { [newPath]: value };
}
Expand Down Expand Up @@ -551,7 +539,9 @@ func tsType(r *registry.Registry, fieldType data.Type) string {
}

typeStr := ""
if strings.Index(info.Type, ".") != 0 {
if mapWellKnownType(info.Type) != "" {
typeStr = mapWellKnownType(info.Type)
} else if strings.Index(info.Type, ".") != 0 {
typeStr = mapScalaType(info.Type)
} else if !info.IsExternal {
typeStr = typeInfo.PackageIdentifier
Expand All @@ -565,6 +555,24 @@ func tsType(r *registry.Registry, fieldType data.Type) string {
return typeStr
}

func mapWellKnownType(protoType string) string {
switch protoType {
case ".google.protobuf.BoolValue":
return "boolean | undefined"
case ".google.protobuf.StringValue":
return "string | undefined"
case ".google.protobuf.DoubleValue",
".google.protobuf.FloatValue",
".google.protobuf.Int32Value",
".google.protobuf.Int64Value",
".google.protobuf.UInt32Value",
".google.protobuf.UInt64Value":
return "number | undefined"
}

return ""
}

func mapScalaType(protoType string) string {
switch protoType {
case "uint64", "sint64", "int64", "fixed64", "sfixed64", "string":
Expand Down
5 changes: 5 additions & 0 deletions registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ func (r *Registry) collectExternalDependenciesFromData(filesData map[string]*dat
if !ok {
return errors.Errorf("cannot find type info for %s, $v", typeName)
}
if typeInfo.File == "google/protobuf/wrappers.proto" {
// Skip well-known wrapper types without importing them as an external dependency,
// since their types are converted to native TypeScript types by mapWellKnownType.
continue
}
identifier := typeInfo.Package + "|" + typeInfo.File

if _, ok := dependencies[identifier]; !ok {
Expand Down

0 comments on commit df4e4d3

Please sign in to comment.