Skip to content

Commit

Permalink
feat:[#4] support boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Aug 11, 2023
1 parent 35093dc commit 158cb19
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 22 deletions.
1 change: 0 additions & 1 deletion pages/App/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export default function Chat() {
let totalTable: any = [];

const updateTableInfo = async (tableId: any, totalTable: []) => {
console.log(12321);

const currentTableMeta = totalTable.find(({ id }: { id: any }) => id === tableId) as any;
const currentTable = await bitable.base.getTableById(currentTableMeta?.id);
Expand Down
10 changes: 10 additions & 0 deletions pages/api/advice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ content: string;
sentiment: "negative" | "neutral" | "positive";
}
A: Well done today
Q: export interface book {
book_name: string;
is_novel: boolean;
}
A: harry potter
Q: ${ tsStr }
A: `;
}
Expand All @@ -46,6 +51,11 @@ content: string;
sentiment: "negative" | "neutral" | "positive";
}
A: 今天做的不错
Q: export interface book {
book_name: string;
is_novel: boolean;
}
A: 色戒
Q: ${ tsStr }
A: `;
}
Expand Down
13 changes: 13 additions & 0 deletions utils/BaseSchema/dataWriter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,22 @@ const tableInfo: TableProps = {
}],
},
},
//数字
{
id: 'fldueXn0k2', name: 'Height', property: {}, type: 2,
},
// 复选框
{
id: 'fldueXn0k2', name: 'is_novel', property: {}, type: 7,
}
],
};

const resultExample = { content: 'TypeChat is awesome!', sentiment: 'positive' };
const resultExample2 = { content: '我喜欢吃苹果和橘子', fruit: ['苹果', '橘子'] };
const resultExample3 = { Height: 180 };
const resultExample4 = { is_novel: true };

describe('dataWriter init', () => {
it('should init', function () {
expect(new DataWriter(tableInfo)).toBeDefined();
Expand Down Expand Up @@ -127,6 +134,12 @@ describe('parse number', () => {
});
})

describe('parse boolean', () => {
const core = new DataWriter(tableInfo);
it('should parse boolean field', () => {
expect(core.load(resultExample4).parseOneField(tableInfo.fields[4])).toEqual(true);
});
})
describe('parse all type', () => {
const core = new DataWriter(tableInfo);
it('should parse all', () => {
Expand Down
7 changes: 7 additions & 0 deletions utils/BaseSchema/dataWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export class DataWriter {
exportNumber(number: number) {
return number;
}
exportBoolean(boolean: boolean) {
return boolean;
}

exportSelect(label: string, labelId: string) {
return {
Expand Down Expand Up @@ -84,6 +87,10 @@ export class DataWriter {
})
return labelMeta.map((v: any) => this.exportSelect(v[0], v[1]));
}
// 复选框
if (field.type === 7) {
return this.exportBoolean(itemValue);
}
return null;
}

Expand Down
8 changes: 8 additions & 0 deletions utils/BaseSchema/tableParser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const tableInfo: TableProps = {
{
id: 'fldueXn0k2', name: 'Height', property: {}, type: 2,
},
// 单选框
{
id: '"fldueXn0k2"', name: 'is_novel', property: {}, type: 7,
}
],
};

Expand Down Expand Up @@ -95,6 +99,10 @@ describe('BaseSchema class format', () => {
expect(core.formatNumberField(tableInfo.fields[3])).toBe(`Height: number;`);
})

it('should format boolean field', function () {
expect(core.formatBooleanField(tableInfo.fields[4])).toBe(`is_novel: boolean;`);
})

it('should format title', function () {
expect(core.formatTitle()).toBe('export interface SentimentResponse {');
});
Expand Down
53 changes: 32 additions & 21 deletions utils/BaseSchema/tableParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {TableProps} from './table';

export class TableParser {
table: TableProps;

constructor(tableInfo: TableProps) {
this.table = tableInfo;
}
Expand All @@ -18,35 +19,45 @@ export class TableParser {
}

formatStringField(field: IBaseFieldMeta) {
return `${field.name}: string;`;
return `${ field.name }: string;`;
}

formatSelectField(field: IBaseFieldMeta) {
const property = field.property as any
if (!property || !property?.options) {{
return '';}
const property = field.property as any;
if (!property || !property?.options) {
{
return '';
}
}
const options = property.options as any
const options = property.options as any;
if (!options) {
return '';
}
const optionsStr = options.map((o: any) => `"${o.name}"`).join(' | ');
return `${field.name}: ${optionsStr};`;
const optionsStr = options.map((o: any) => `"${ o.name }"`).join(' | ');
return `${ field.name }: ${ optionsStr };`;
}

formatMultiSelectField(field: IBaseFieldMeta) {
const property = field.property as any
if (!property || !property?.options) {{
return '';}
const property = field.property as any;
if (!property || !property?.options) {
{
return '';
}
}
const options = property.options as any
const options = property.options as any;
if (!options) {
return '';
}
const optionsStr = options.map((o: any) => `"${o.name}"`).join(' | ');
return `${field.name}: (${optionsStr})[];`;
const optionsStr = options.map((o: any) => `"${ o.name }"`).join(' | ');
return `${ field.name }: (${ optionsStr })[];`;
}

formatNumberField(iBaseFieldMeta: IBaseFieldMeta) {
return `${iBaseFieldMeta.name}: number;`;
return `${ iBaseFieldMeta.name }: number;`;
}

formatBooleanField(iBaseFieldMeta: IBaseFieldMeta) {
return `${ iBaseFieldMeta.name }: boolean;`;
}

formatField(field: IBaseFieldMeta) {
Expand All @@ -59,30 +70,30 @@ export class TableParser {
return this.formatSelectField(field);
case 4:
return this.formatMultiSelectField(field);
case 7:
return this.formatBooleanField(field);
default:
return '';
}
}



formatTitle() {
return `export interface ${this.title} {`;
return `export interface ${ this.title } {`;
}

formatEnd() {
return `}`;
}

private formatAll(){
private formatAll() {
const fields = this.table.fields.map(f => this.formatField(f)).join('\n');
return `${this.formatTitle()}\n${fields}\n${this.formatEnd()}`;
return `${ this.formatTitle() }\n${ fields }\n${ this.formatEnd() }`;
}

get typeStr(){
return this.formatAll()
get typeStr() {
return this.formatAll();
}



}

0 comments on commit 158cb19

Please sign in to comment.