Skip to content

Commit

Permalink
Autocomplete Json body value for enum type
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-gupta-ij committed Apr 22, 2024
1 parent d69b2d7 commit eba4e08
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 177 deletions.
130 changes: 74 additions & 56 deletions src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,94 @@ import { AutocompleteTrie } from "./trie-completion.js";
import { partialParseJson } from "./json-parser.js";
import { tokenizeHeader } from "./parse-request-header.js";


export class OpenapiAutocomplete {
constructor(openapi, collections) {
this.openapi = openapi;
this.extractor = new OpenAPIExtractor(openapi);

this.methods = this.extractor.getAllMethods();

this.trieCompletion = new AutocompleteTrie();

let specialFoo = {
"{collection_name}": [
// match any word by regex
(token) => token == "{collection_name}" || token.match(/\w+/),
// complete with all words
(token) => collections.filter((collection) => collection.startsWith(token)),
]
}

for (let method of this.methods) {
let requestString = `${method.method.toUpperCase()} ${method.path}`;
let tokens = tokenizeHeader(requestString);
this.trieCompletion.addPath(tokens, specialFoo, method.body);
}
constructor(openapi, collections) {
this.openapi = openapi;
this.extractor = new OpenAPIExtractor(openapi);

this.methods = this.extractor.getAllMethods();

this.trieCompletion = new AutocompleteTrie();

let specialFoo = {
"{collection_name}": [
// match any word by regex
(token) => token == "{collection_name}" || token.match(/\w+/),
// complete with all words
(token) =>
collections.filter((collection) => collection.startsWith(token)),
],
};

for (let method of this.methods) {
let requestString = `${method.method.toUpperCase()} ${method.path}`;
let tokens = tokenizeHeader(requestString);
this.trieCompletion.addPath(tokens, specialFoo, method.body);
}
}

completeRequestHeader(requestString) {
let tokens = tokenizeHeader(requestString);
let completions = this.trieCompletion.autocomplete(tokens);
completeRequestHeader(requestString) {
let tokens = tokenizeHeader(requestString);
let completions = this.trieCompletion.autocomplete(tokens);

return completions
}
return completions;
}

/// Expect full request header, e.g. "POST /collections/my_collection/points/search"
/// And partial request body, e.g. '{"vectors": {'
completeRequestBody(requestHeader, requestJson) {
let tokens = tokenizeHeader(requestHeader);
let dataRef = this.trieCompletion.match(tokens);

/// Expect full request header, e.g. "POST /collections/my_collection/points/search"
/// And partial request body, e.g. '{"vectors": {'
completeRequestBody(requestHeader, requestJson) {
let tokens = tokenizeHeader(requestHeader);
let dataRef = this.trieCompletion.match(tokens);

return this.completeRequestBodyByDataRef(dataRef, requestJson);
return this.completeRequestBodyByDataRef(dataRef, requestJson);
}

/// Expect full dataRef, e.g. "#/components/schemas/FilterRequest"
/// And partial request body, e.g. '{"vectors": {'
completeRequestBodyByDataRef(dataRef, requestJson) {
if (!dataRef) {
return [];
}

/// Expect full dataRef, e.g. "#/components/schemas/FilterRequest"
/// And partial request body, e.g. '{"vectors": {'
completeRequestBodyByDataRef(dataRef, requestJson) {
if (!dataRef) {
return [];
}
let jsonParsedResult = partialParseJson(requestJson);

let jsonParsedResult = partialParseJson(requestJson);
let editingChunk = jsonParsedResult.getEditedChunk();

let editingChunk = jsonParsedResult.getEditedChunk();
if (editingChunk.editing === "value") {
let result = this.extractor.allEnumValues(
dataRef,
jsonParsedResult.path,
editingChunk.key || "",
editingChunk.value || ""
);

if (editingChunk.editing !== "key") {
// ToDo: also try to complete values
return [];
}
if (editingChunk.value === null) {
// If there is no value, then we should autocomplete with open quote
result = result.map((s) => '"' + s + '"');
} else {
// If there is a value, then we should autocomplete with closing quote only

let result = this.extractor.allProperties(dataRef, jsonParsedResult.path, editingChunk.key || "");
result = result.map((s) => s + '"');
}
return result;
}

if (editingChunk.key === null) {
// If there is no key, then we should autocomplete with open quote
let result = this.extractor.allProperties(
dataRef,
jsonParsedResult.path,
editingChunk.key || ""
);

result = result.map((s) => '"' + s + '": ');
} else {
// If there is a key, then we should autocomplete with closing quote only
if (editingChunk.key === null) {
// If there is no key, then we should autocomplete with open quote

result = result.map((s) => s + '": ');
}
result = result.map((s) => '"' + s + '": ');
} else {
// If there is a key, then we should autocomplete with closing quote only

return result;
result = result.map((s) => s + '": ');
}

return result;
}
}
Loading

0 comments on commit eba4e08

Please sign in to comment.