Skip to content

Add generated Protobuf types#16

Open
stevendborrelli wants to merge 4 commits intocrossplane:mainfrom
stevendborrelli:proto-types
Open

Add generated Protobuf types#16
stevendborrelli wants to merge 4 commits intocrossplane:mainfrom
stevendborrelli:proto-types

Conversation

@stevendborrelli
Copy link
Member

@stevendborrelli stevendborrelli commented Mar 5, 2026

Note: this requires a fix to the upstream repo before we can merge it, see stephenh/ts-proto#1216

Description of your changes

Add TypeScript Type Registry Support for Protobuf Type Detection

Summary

This PR enhances the SDK's ability to detect and work with protobuf types by enabling the outputTypeRegistry=true option in protoc-gen-ts_proto. This adds a $type property to all generated protobuf types, providing a reliable type identifier that simplifies type detection throughout the codebase.

Changes

1. Generated Protobuf Code

  • Added typeRegistry.ts: Created the type registry file that provides the messageTypeRegistry Map and type definitions used by all generated protobuf code
  • Updated protobuf generation: All protobuf types now include a $type property with their fully-qualified type name (e.g., "apiextensions.fn.proto.v1.Resource")
  • Fixed type conversions: Updated fromPartial() methods in run_function.ts to use as unknown as for safer type casting when dealing with Object.entries()

2. Improved Type Detection in resource.ts

Before:

const isProtoResource =
  typeof source === 'object' &&
  source !== null &&
  'resource' in source &&
  typeof (source as { resource?: unknown }).resource === 'object' &&
  ('connectionDetails' in source || 'ready' in source);
const isProtoResource =
  typeof source === 'object' &&
  source !== null &&
  '$type' in source &&
  (source as { $type?: string }).$type === 'apiextensions.fn.proto.v1.Resource';

Benefits:

  • More reliable - checks exact protobuf type identifier
  • Simpler - single property check vs multiple heuristics
  • More specific - distinguishes protobuf types from plain objects with similar properties
  • More maintainable - doesn't rely on checking implementation details

Test Updates

Updated response.test.ts to expect the $type property in SchemaSelector objects:

expect(result.requirements?.schemas?.['xr-schema']).toEqual({
  $type: 'apiextensions.fn.proto.v1.SchemaSelector',
  apiVersion: 'example.org/v1',
  kind: 'MyResource',
});

I have:

Signed-off-by: Steven Borrelli <steve@borrelli.org>
Signed-off-by: Steven Borrelli <steve@borrelli.org>
Signed-off-by: Steven Borrelli <steve@borrelli.org>
@stevendborrelli stevendborrelli requested a review from Copilot March 5, 2026 17:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Signed-off-by: Steven Borrelli <steve@borrelli.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants