Skip to content

chore: generate any interface as Deployment Option in TypeScript #9917

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

Merged
merged 4 commits into from
Sep 28, 2023
Merged
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
29 changes: 7 additions & 22 deletions scripts/apitypings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,28 +989,13 @@ func (g *Generator) typescriptType(ty types.Type) (TypescriptType, error) {
}, nil
}

// Do support "Stringer" interfaces, they likely can get string
// marshaled.
for i := 0; i < intf.NumMethods(); i++ {
meth := intf.Method(i)
if meth.Name() == "String" {
return TypescriptType{
ValueType: "string",
AboveTypeLine: indentedComment("actual value is an interface that implements 'String()'"),
Optional: false,
}, nil
}
}

// All complex interfaces should be named. So if we get here, that means
// we are using anonymous interfaces. Which is just weird and not supported.
// Example:
// type Foo struct {
// Bar interface {
// Baz() string
// }
// }
return TypescriptType{}, xerrors.New("only empty interface types are supported")
// Interfaces are difficult to determine the JSON type, so just return
// an 'any'.
return TypescriptType{
ValueType: "any",
AboveTypeLine: indentedComment("eslint-disable-next-line @typescript-eslint/no-explicit-any -- Golang interface, unable to resolve type."),
Optional: false,
}, nil
case *types.TypeParam:
_, ok := ty.Underlying().(*types.Interface)
if !ok {
Expand Down
22 changes: 1 addition & 21 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,29 +1016,9 @@ export const getDeploymentSSHConfig =
return response.data;
};

// The Deployment types are not generated on from the Go generator yet because
// it does not know how to generate OptionSet
export interface DeploymentGroup {
readonly name: string;
readonly parent?: DeploymentGroup;
readonly description: string;
readonly children: DeploymentGroup[];
}
export interface DeploymentOption {
readonly name: string;
readonly description: string;
readonly flag: string;
readonly flag_shorthand: string;
readonly value: unknown;
readonly hidden: boolean;
readonly group?: DeploymentGroup;
readonly env?: string;
readonly yaml?: string;
}

export type DeploymentConfig = {
readonly config: TypesGen.DeploymentValues;
readonly options: DeploymentOption[];
readonly options: TypesGen.ClibaseOption[];
};

export const getDeploymentConfig = async (): Promise<DeploymentConfig> => {
Expand Down
4 changes: 2 additions & 2 deletions site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions site/src/components/DeploySettingsLayout/OptionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
} from "components/DeploySettingsLayout/Option";
import { FC } from "react";
import { optionValue } from "./optionValue";
import { DeploymentOption } from "api/api";
import Box from "@mui/material/Box";
import { ClibaseOption } from "api/typesGenerated";

const OptionsTable: FC<{
options: DeploymentOption[];
options: ClibaseOption[];
}> = ({ options }) => {
const styles = useStyles();

Expand Down
6 changes: 3 additions & 3 deletions site/src/components/DeploySettingsLayout/optionValue.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DeploymentOption } from "api/api";
import { optionValue } from "./optionValue";
import { ClibaseOption } from "api/typesGenerated";

const defaultOption: DeploymentOption = {
const defaultOption: ClibaseOption = {
name: "",
description: "",
flag: "",
Expand All @@ -12,7 +12,7 @@ const defaultOption: DeploymentOption = {

describe("optionValue", () => {
it.each<{
option: DeploymentOption;
option: ClibaseOption;
expected: unknown;
}>([
{
Expand Down
4 changes: 2 additions & 2 deletions site/src/components/DeploySettingsLayout/optionValue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DeploymentOption } from "api/api";
import { ClibaseOption } from "api/typesGenerated";
import { intervalToDuration, formatDuration } from "date-fns";

// optionValue is a helper function to format the value of a specific deployment options
export function optionValue(option: DeploymentOption) {
export function optionValue(option: ClibaseOption) {
switch (option.name) {
case "Max Token Lifetime":
case "Session Duration":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Box from "@mui/material/Box";
import { DAUsResponse } from "api/typesGenerated";
import { ClibaseOption, DAUsResponse } from "api/typesGenerated";
import { ErrorAlert } from "components/Alert/ErrorAlert";
import { DAUChart, DAUTitle } from "components/DAUChart/DAUChart";
import { Header } from "components/DeploySettingsLayout/Header";
Expand All @@ -8,10 +8,9 @@ import { Stack } from "components/Stack/Stack";
import { ChartSection } from "./ChartSection";
import { useDeploymentOptions } from "utils/deployOptions";
import { docs } from "utils/docs";
import { DeploymentOption } from "api/api";

export type GeneralSettingsPageViewProps = {
deploymentOptions: DeploymentOption[];
deploymentOptions: ClibaseOption[];
deploymentDAUs?: DAUsResponse;
deploymentDAUsError: unknown;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { DeploymentGroup } from "api/api";
import { ClibaseGroup } from "api/typesGenerated";
import { NetworkSettingsPageView } from "./NetworkSettingsPageView";
import type { Meta, StoryObj } from "@storybook/react";

const group: DeploymentGroup = {
const group: ClibaseGroup = {
name: "Networking",
description: "",
children: [] as DeploymentGroup[],
};

const meta: Meta<typeof NetworkSettingsPageView> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeploymentOption } from "api/api";
import { ClibaseOption } from "api/typesGenerated";
import {
Badges,
EnabledBadge,
Expand All @@ -14,7 +14,7 @@ import {
import { docs } from "utils/docs";

export type NetworkSettingsPageViewProps = {
options: DeploymentOption[];
options: ClibaseOption[];
};

export const NetworkSettingsPageView = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { DeploymentGroup, DeploymentOption } from "api/api";
import { SecuritySettingsPageView } from "./SecuritySettingsPageView";
import type { Meta, StoryObj } from "@storybook/react";
import { ClibaseGroup, ClibaseOption } from "api/typesGenerated";

const group: DeploymentGroup = {
const group: ClibaseGroup = {
name: "Networking",
description: "",
children: [] as DeploymentGroup[],
};

const meta: Meta<typeof SecuritySettingsPageView> = {
Expand Down Expand Up @@ -64,15 +63,15 @@ export const NoTLS = {
{
name: "SSH Keygen Algorithm",
value: "1234",
} as DeploymentOption,
} as ClibaseOption,
{
name: "Disable Owner Workspace Access",
value: false,
} as DeploymentOption,
} as ClibaseOption,
{
name: "Secure Auth Cookie",
value: "1234",
} as DeploymentOption,
} as ClibaseOption,
],
},
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeploymentOption } from "api/api";
import { ClibaseOption } from "api/typesGenerated";
import {
Badges,
DisabledBadge,
Expand All @@ -15,7 +15,7 @@ import {
import { docs } from "utils/docs";

export type SecuritySettingsPageViewProps = {
options: DeploymentOption[];
options: ClibaseOption[];
featureAuditLogEnabled: boolean;
featureBrowserOnlyEnabled: boolean;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { DeploymentGroup } from "api/api";
import { ClibaseGroup } from "api/typesGenerated";
import { UserAuthSettingsPageView } from "./UserAuthSettingsPageView";
import type { Meta, StoryObj } from "@storybook/react";

const oidcGroup: DeploymentGroup = {
const oidcGroup: ClibaseGroup = {
name: "OIDC",
description: "",
children: [] as DeploymentGroup[],
};

const ghGroup: DeploymentGroup = {
const ghGroup: ClibaseGroup = {
name: "GitHub",
description: "",
children: [] as DeploymentGroup[],
};

const meta: Meta<typeof UserAuthSettingsPageView> = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeploymentOption } from "api/api";
import { ClibaseOption } from "api/typesGenerated";
import {
Badges,
DisabledBadge,
Expand All @@ -14,7 +14,7 @@ import {
import { docs } from "utils/docs";

export type UserAuthSettingsPageViewProps = {
options: DeploymentOption[];
options: ClibaseOption[];
};

export const UserAuthSettingsPageView = ({
Expand Down
14 changes: 7 additions & 7 deletions site/src/utils/deployOptions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DeploymentOption, DeploymentGroup } from "api/api";
import { ClibaseGroup, ClibaseOption } from "api/typesGenerated";
import { useMemo } from "react";

const deploymentOptions = (
options: DeploymentOption[],
options: ClibaseOption[],
...names: string[]
): DeploymentOption[] => {
const found: DeploymentOption[] = [];
): ClibaseOption[] => {
const found: ClibaseOption[] = [];
for (const name of names) {
const option = options.find((o) => o.name === name);
if (option) {
Expand All @@ -18,14 +18,14 @@ const deploymentOptions = (
};

export const useDeploymentOptions = (
options: DeploymentOption[],
options: ClibaseOption[],
...names: string[]
): DeploymentOption[] => {
): ClibaseOption[] => {
return useMemo(() => deploymentOptions(options, ...names), [options, names]);
};

export const deploymentGroupHasParent = (
group: DeploymentGroup | undefined,
group: ClibaseGroup | undefined,
parent: string,
): boolean => {
if (!group) {
Expand Down