Skip to content

Commit

Permalink
Validated example generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
eyw520 committed Nov 25, 2024
1 parent c7c0b85 commit c959666
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6708,9 +6708,9 @@ func do() {
client.Create(
context.TODO(),
&acme.CreateRequest{
Decimal: 1.1,
Even: 1,
Name: "name",
Decimal: 2.2,
Even: 100,
Name: "aaaaaaaaaa",
Shape: acme.ShapeSquare,
},
)
Expand All @@ -6731,9 +6731,9 @@ func do() {
client.Get(
context.TODO(),
&acme.GetRequest{
Decimal: 1.1,
Even: 1,
Name: "name",
Decimal: 2.2,
Even: 100,
Name: "aaaaaaaaaa",
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "f894d7236e53b5a4907afb3242986edc936b7231",
"id": "ec81dc62fe34595a7d385d19d5dc5798e3f65388",
"url": "/create",
"name": null,
"endpointHeaders": [],
Expand Down Expand Up @@ -1524,10 +1524,10 @@
"type": "primitive",
"primitive": {
"type": "double",
"double": 1.1
"double": 2.2
}
},
"jsonExample": 1.1
"jsonExample": 2.2
}
},
{
Expand Down Expand Up @@ -1559,10 +1559,10 @@
"type": "primitive",
"primitive": {
"type": "integer",
"integer": 1
"integer": 100
}
},
"jsonExample": 1
"jsonExample": 100
}
},
{
Expand Down Expand Up @@ -1595,11 +1595,11 @@
"primitive": {
"type": "string",
"string": {
"original": "name"
"original": "aaaaaaaaaa"
}
}
},
"jsonExample": "name"
"jsonExample": "aaaaaaaaaa"
}
},
{
Expand Down Expand Up @@ -1687,9 +1687,9 @@
}
],
"jsonExample": {
"decimal": 1.1,
"even": 1,
"name": "name",
"decimal": 2.2,
"even": 100,
"name": "aaaaaaaaaa",
"shape": "SQUARE"
}
},
Expand Down Expand Up @@ -1758,10 +1758,10 @@
"type": "primitive",
"primitive": {
"type": "double",
"double": 1.1
"double": 2.2
}
},
"jsonExample": 1.1
"jsonExample": 2.2
}
},
{
Expand Down Expand Up @@ -1819,10 +1819,10 @@
"type": "primitive",
"primitive": {
"type": "integer",
"integer": 1
"integer": 100
}
},
"jsonExample": 1
"jsonExample": 100
}
},
{
Expand Down Expand Up @@ -1881,11 +1881,11 @@
"primitive": {
"type": "string",
"string": {
"original": "name"
"original": "aaaaaaaaaa"
}
}
},
"jsonExample": "name"
"jsonExample": "aaaaaaaaaa"
}
},
{
Expand Down Expand Up @@ -2028,9 +2028,9 @@
}
},
"jsonExample": {
"decimal": 1.1,
"even": 1,
"name": "name",
"decimal": 2.2,
"even": 100,
"name": "aaaaaaaaaa",
"shape": "SQUARE"
}
}
Expand Down Expand Up @@ -2327,7 +2327,7 @@
"autogeneratedExamples": [
{
"example": {
"id": "5ec5bb625677405238a5bb25cc6d0d168e9f5038",
"id": "fa2e3a521ef9a8a72af206492df27002793d4ea4",
"url": "",
"name": null,
"endpointHeaders": [],
Expand Down Expand Up @@ -2364,10 +2364,10 @@
"type": "primitive",
"primitive": {
"type": "double",
"double": 1.1
"double": 2.2
}
},
"jsonExample": 1.1
"jsonExample": 2.2
}
},
{
Expand Down Expand Up @@ -2401,10 +2401,10 @@
"type": "primitive",
"primitive": {
"type": "integer",
"integer": 1
"integer": 100
}
},
"jsonExample": 1
"jsonExample": 100
}
},
{
Expand Down Expand Up @@ -2439,11 +2439,11 @@
"primitive": {
"type": "string",
"string": {
"original": "name"
"original": "aaaaaaaaaa"
}
}
},
"jsonExample": "name"
"jsonExample": "aaaaaaaaaa"
}
}
],
Expand Down Expand Up @@ -2516,10 +2516,10 @@
"type": "primitive",
"primitive": {
"type": "double",
"double": 1.1
"double": 2.2
}
},
"jsonExample": 1.1
"jsonExample": 2.2
}
},
{
Expand Down Expand Up @@ -2577,10 +2577,10 @@
"type": "primitive",
"primitive": {
"type": "integer",
"integer": 1
"integer": 100
}
},
"jsonExample": 1
"jsonExample": 100
}
},
{
Expand Down Expand Up @@ -2639,11 +2639,11 @@
"primitive": {
"type": "string",
"string": {
"original": "name"
"original": "aaaaaaaaaa"
}
}
},
"jsonExample": "name"
"jsonExample": "aaaaaaaaaa"
}
},
{
Expand Down Expand Up @@ -2786,9 +2786,9 @@
}
},
"jsonExample": {
"decimal": 1.1,
"even": 1,
"name": "name",
"decimal": 2.2,
"even": 100,
"name": "aaaaaaaaaa",
"shape": "SQUARE"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { ExamplePrimitive, PrimitiveType } from "@fern-api/ir-sdk";
import {
ExamplePrimitive,
PrimitiveType,
PrimitiveTypeV2,
StringValidationRules,
DoubleValidationRules,
IntegerValidationRules
} from "@fern-api/ir-sdk";
import { assertNever, Examples } from "@fern-api/core-utils";
import { ExampleGenerationResult, ExampleGenerationSuccess } from "./ExampleGenerationResult";
import { ExampleGenerationSuccess } from "./ExampleGenerationResult";

export declare namespace generatePrimitiveExample {
interface Args {
Expand All @@ -16,8 +23,12 @@ export function generatePrimitiveExample({
}: generatePrimitiveExample.Args): ExampleGenerationSuccess<ExamplePrimitive> {
switch (primitiveType.v1) {
case "STRING": {
const jsonExample = fieldName ?? Examples.STRING;
return { type: "success", example: ExamplePrimitive.string({ original: jsonExample }), jsonExample };
let validation = undefined;
if (primitiveType.v2?.type === "string") {
const stringType = primitiveType.v2 as PrimitiveTypeV2.String;
validation = stringType.validation;
}
return generatePrimitiveStringExample(fieldName, validation);
}
case "BASE_64": {
return { type: "success", example: ExamplePrimitive.base64(Examples.BASE64), jsonExample: Examples.BASE64 };
Expand All @@ -43,13 +54,23 @@ export function generatePrimitiveExample({
};
}
case "DOUBLE": {
return { type: "success", example: ExamplePrimitive.double(Examples.DOUBLE), jsonExample: Examples.DOUBLE };
let validation = undefined;
if (primitiveType.v2?.type === "double") {
const doubleType = primitiveType.v2 as PrimitiveTypeV2.Double;
validation = doubleType.validation;
}
return generatePrimitiveDoubleExample(fieldName, validation);
}
case "FLOAT": {
return { type: "success", example: ExamplePrimitive.float(Examples.FLOAT), jsonExample: Examples.FLOAT };
}
case "INTEGER": {
return { type: "success", example: ExamplePrimitive.integer(Examples.INT), jsonExample: Examples.INT };
let validation = undefined;
if (primitiveType.v2?.type === "integer") {
const integerType = primitiveType.v2 as PrimitiveTypeV2.Integer;
validation = integerType.validation;
}
return generatePrimitiveIntegerExample(fieldName, validation);
}
case "UINT_64": {
return { type: "success", example: ExamplePrimitive.uint64(Examples.INT64), jsonExample: Examples.INT64 };
Expand All @@ -74,3 +95,64 @@ export function generatePrimitiveExample({
assertNever(primitiveType.v1);
}
}

function generatePrimitiveStringExample(
fieldName: string | undefined,
validation: StringValidationRules | undefined
): ExampleGenerationSuccess<ExamplePrimitive> {
if (validation) {
const minLength = validation.minLength;
const maxLength = validation.maxLength;
if (maxLength) {
const maxLengthExample = "a".repeat(maxLength);
return {
type: "success",
example: ExamplePrimitive.string({ original: maxLengthExample }),
jsonExample: maxLengthExample
};
} else if (minLength) {
const minLengthExample = "a".repeat(minLength);
return {
type: "success",
example: ExamplePrimitive.string({ original: minLengthExample }),
jsonExample: minLengthExample
};
}
}
const jsonExample = fieldName ?? Examples.STRING;
return { type: "success", example: ExamplePrimitive.string({ original: jsonExample }), jsonExample };
}

function generatePrimitiveDoubleExample(
fieldName: string | undefined,
validation: DoubleValidationRules | undefined
): ExampleGenerationSuccess<ExamplePrimitive> {
if (validation) {
const maximum = validation.max;
const minimum = validation.min;
if (maximum) {
return { type: "success", example: ExamplePrimitive.double(maximum), jsonExample: maximum };
} else if (minimum) {
return { type: "success", example: ExamplePrimitive.double(minimum), jsonExample: minimum };
}
}
const jsonExample = fieldName ?? Examples.DOUBLE;
return { type: "success", example: ExamplePrimitive.double(Examples.DOUBLE), jsonExample: Examples.DOUBLE };
}

function generatePrimitiveIntegerExample(
fieldName: string | undefined,
validation: IntegerValidationRules | undefined
): ExampleGenerationSuccess<ExamplePrimitive> {
if (validation) {
const maximum = validation.max;
const minimum = validation.min;
if (maximum) {
return { type: "success", example: ExamplePrimitive.integer(maximum), jsonExample: maximum };
} else if (minimum) {
return { type: "success", example: ExamplePrimitive.integer(minimum), jsonExample: minimum };
}
}
const jsonExample = fieldName ?? Examples.DOUBLE;
return { type: "success", example: ExamplePrimitive.integer(Examples.INT), jsonExample: Examples.INT };
}
Loading

0 comments on commit c959666

Please sign in to comment.