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

Handle well-known wrapper types in a way that matches grpc-gateway serialization/deserialization #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,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 @@ -386,14 +377,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 @@ -546,7 +534,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 @@ -560,6 +550,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