-
Notifications
You must be signed in to change notification settings - Fork 1k
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 insrc/gcp/proto.ts
to selectively extract properties from an object. - Code Refactoring: Replaced numerous
copyIfPresent
calls with the newpick
function combined with object spread syntax (...
) acrosssrc/deploy/functions/build.ts
,src/deploy/functions/runtimes/discovery/v1alpha1.ts
, andsrc/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
-
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. ↩
There was a problem hiding this 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.
/gemini review |
There was a problem hiding this 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 insrc/gcp/proto.ts
, using thein
operator could lead to unexpected behavior. It would be safer to usehasOwnProperty
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 ofproto.pick
where they are equivalent.
...proto.pick(bdEndpoint, "environmentVariables", "secretEnvironmentVariables", "labels"), | ||
...proto.convert(bdEndpoint, r.resolveString, { | ||
serviceAccount: "serviceAccount", | ||
}), | ||
...proto.convert(bdEndpoint, r.resolveInt, { | ||
concurrency: "concurrency", | ||
minInstances: "minInstances", | ||
maxInstances: "maxInstances", | ||
}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
callableTrigger: { | ||
...proto.pick(endpoint.callableTrigger, "genkitAction"), | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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;
}
I think I also might just play with Zod |
Just having fun with ergonomics.