Skip to content

Random attempt at new ergonomics where copyIfPresent can be inlined #9015

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

inlined
Copy link
Member

@inlined inlined commented Aug 20, 2025

Just having fun with ergonomics.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @inlined, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors several instances of property copying using the copyIfPresent utility function. It introduces a new pick utility function that allows for more ergonomic and inline property extraction and spreading into new objects, aiming to improve code readability and conciseness.

Highlights

  • New Utility Function: Implemented a new pick utility function in src/gcp/proto.ts to selectively extract properties from an object.
  • Code Refactoring: Replaced numerous copyIfPresent calls with the new pick function combined with object spread syntax (...) across src/deploy/functions/build.ts, src/deploy/functions/runtimes/discovery/v1alpha1.ts, and src/deploy/functions/runtimes/node/parseTriggers.ts.
  • Improved Ergonomics: Enhanced code ergonomics by enabling inline property assignment and reducing verbosity in object construction.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new pick utility function to improve ergonomics by replacing copyIfPresent. This change is applied across multiple files, resulting in more concise and readable code for object property copying. The overall direction is good, but I've found a critical issue with an unnecessary import and a minor formatting problem that should be addressed.

@inlined
Copy link
Member Author

inlined commented Aug 20, 2025

/gemini review

@firebase firebase deleted a comment from gemini-code-assist bot Aug 20, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces new utility functions, pick and convert, to replace copyIfPresent for better ergonomics when constructing objects. The changes generally improve code clarity and maintainability. However, I've identified a few areas for improvement:

  • There's a high-severity issue in src/deploy/functions/build.ts where new logic for resolving certain properties is redundant with existing code, causing them to be processed twice.
  • In the new convert function implementation in src/gcp/proto.ts, using the in operator could lead to unexpected behavior. It would be safer to use hasOwnProperty for consistency and to avoid checking the prototype chain.
  • A minor suggestion in src/deploy/functions/build.ts to use a more concise object spread instead of proto.pick where they are equivalent.

Comment on lines +495 to +503
...proto.pick(bdEndpoint, "environmentVariables", "secretEnvironmentVariables", "labels"),
...proto.convert(bdEndpoint, r.resolveString, {
serviceAccount: "serviceAccount",
}),
...proto.convert(bdEndpoint, r.resolveInt, {
concurrency: "concurrency",
minInstances: "minInstances",
maxInstances: "maxInstances",
}),
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This change introduces redundant logic. The properties serviceAccount, concurrency, minInstances, and maxInstances are now being resolved here using proto.convert, but they are also resolved again later in this function (lines 524-532 in the file).

This duplication can lead to confusion and potential bugs. To ensure there's a single source of truth, the redundant calls later in the function should be removed.

Comment on lines +580 to +582
callableTrigger: {
...proto.pick(endpoint.callableTrigger, "genkitAction"),
},
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

As noted in the code comment, using proto.pick here is a bit verbose. Since the CallableTrigger type only contains the optional genkitAction property, you can achieve the same result more concisely and readably by using an object spread.

      callableTrigger: { ...endpoint.callableTrigger },

Comment on lines +113 to +144
export function convert<
SrcKey extends string,
DestKey extends string,
Source extends { [K in SrcKey]?: unknown },
Fn extends (s: Required<Source>[SrcKey]) => unknown,
>(
src: Source,
transform: Fn,
mapOrFirstKey: Record<SrcKey, DestKey> | SrcKey,
...restKeys: SrcKey[]
): Record<string, unknown> {
const dest: Record<string, unknown> = {};

if (typeof mapOrFirstKey === "string") {
if (mapOrFirstKey in src) {
dest[mapOrFirstKey] = transform(src[mapOrFirstKey]);
}
for (const key of restKeys) {
if (key in src) {
dest[key] = transform(src[key]);
}
}
return dest;
}

for (const srcKey of Object.keys(mapOrFirstKey) as SrcKey[]) {
if (srcKey in src) {
dest[mapOrFirstKey[srcKey]] = transform(src[srcKey]);
}
}
return dest;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The in operator checks for properties in the prototype chain, which can lead to unexpected behavior. For consistency with the pick function and to make this utility more robust, it's better to use Object.prototype.hasOwnProperty.call() to ensure only own properties of the source object are considered.

Additionally, since the transform function's signature suggests it expects a non-nullable value, it's good practice to use the non-null assertion operator (!) when passing the value, similar to how it's done in the pick function.

export function convert<
  SrcKey extends string,
  DestKey extends string,
  Source extends { [K in SrcKey]?: unknown },
  Fn extends (s: Required<Source>[SrcKey]) => unknown,
>(
  src: Source,
  transform: Fn,
  mapOrFirstKey: Record<SrcKey, DestKey> | SrcKey,
  ...restKeys: SrcKey[]
): Record<string, unknown> {
  const dest: Record<string, unknown> = {};

  if (typeof mapOrFirstKey === "string") {
    if (Object.prototype.hasOwnProperty.call(src, mapOrFirstKey)) {
      dest[mapOrFirstKey] = transform(src[mapOrFirstKey]!);
    }
    for (const key of restKeys) {
      if (Object.prototype.hasOwnProperty.call(src, key)) {
        dest[key] = transform(src[key]!);
      }
    }
    return dest;
  }

  for (const srcKey of Object.keys(mapOrFirstKey) as SrcKey[]) {
    if (Object.prototype.hasOwnProperty.call(src, srcKey)) {
      dest[mapOrFirstKey[srcKey]] = transform(src[srcKey]!);
    }
  }
  return dest;
}

@inlined inlined removed the request for review from taeold August 22, 2025 15:55
@inlined
Copy link
Member Author

inlined commented Aug 22, 2025

I think I also might just play with Zod

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.

1 participant