Skip to content

Commit

Permalink
refactor: Query return Result
Browse files Browse the repository at this point in the history
  • Loading branch information
miaoyin committed Nov 14, 2023
1 parent 0b4aa16 commit 181ee7f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 34 deletions.
4 changes: 2 additions & 2 deletions app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (p *Service) recover() {
}
}

func (p *Service) Query(query *pb.Query) []*pb.Ack {
func (p *Service) Query(query *pb.Query) *pb.Result {
defer p.recover()

searcher := p.getSearcher(query)
Expand All @@ -118,7 +118,7 @@ func (p *Service) Query(query *pb.Query) []*pb.Ack {
items = append(items, ack)
}

return items
return &pb.Result{Acks: items, Query: query}
}

func (p *Service) AsyncQuery(query *pb.Query) *pb.Query {
Expand Down
12 changes: 11 additions & 1 deletion frontend/src/app/api/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,17 @@ export class ApiService {
this.size = loadSize;
const start = Date.now();

this._acks = await Query(this.query);
const res = await Query(this.query);
if (res) {
if (res.acks) {
this._acks = res.acks;
}

if (res.query) {
this.query = res.query;
}
}

this.time = `${Date.now() - start}ms`;
this.isRun = false;
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/wailsjs/go/app/Service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export function DelDir(arg1:string):Promise<void>;

export function Open(arg1:string):Promise<void>;

export function Query(arg1:pb.Query):Promise<Array<pb.Ack>>;
export function Query(arg1:pb.Query):Promise<pb.Result>;
93 changes: 63 additions & 30 deletions frontend/wailsjs/go/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
export namespace pb {

export class Query {
pattern?: string;
maxCount?: number;
searcher?: number;
paths?: string[];
rgTypes?: string[];
agTypes?: string[];
ugTypes?: string[];
grepType?: string;

static createFrom(source: any = {}) {
return new Query(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.pattern = source["pattern"];
this.maxCount = source["maxCount"];
this.searcher = source["searcher"];
this.paths = source["paths"];
this.rgTypes = source["rgTypes"];
this.agTypes = source["agTypes"];
this.ugTypes = source["ugTypes"];
this.grepType = source["grepType"];
}
}
export class Config {
query?: Query;
dirs?: string[];

static createFrom(source: any = {}) {
return new Config(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.query = this.convertValues(source["query"], Query);
this.dirs = source["dirs"];
}

convertValues(a: any, classs: any, asMap: boolean = false): any {
if (!a) {
return a;
}
if (a.slice) {
return (a as any[]).map(elem => this.convertValues(elem, classs));
} else if ("object" === typeof a) {
if (asMap) {
for (const key of Object.keys(a)) {
a[key] = new classs(a[key]);
}
return a;
}
return new classs(a);
}
return a;
}
}

export class Hit {
col?: number;
len?: number;
Expand Down Expand Up @@ -80,44 +139,18 @@ export namespace pb {
return a;
}
}
export class Query {
pattern?: string;
maxCount?: number;
searcher?: number;
paths?: string[];
rgTypes?: string[];
agTypes?: string[];
ugTypes?: string[];
grepType?: string;

static createFrom(source: any = {}) {
return new Query(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.pattern = source["pattern"];
this.maxCount = source["maxCount"];
this.searcher = source["searcher"];
this.paths = source["paths"];
this.rgTypes = source["rgTypes"];
this.agTypes = source["agTypes"];
this.ugTypes = source["ugTypes"];
this.grepType = source["grepType"];
}
}
export class Config {
export class Result {
query?: Query;
dirs?: string[];
acks?: Ack[];

static createFrom(source: any = {}) {
return new Config(source);
return new Result(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.query = this.convertValues(source["query"], Query);
this.dirs = source["dirs"];
this.acks = this.convertValues(source["acks"], Ack);
}

convertValues(a: any, classs: any, asMap: boolean = false): any {
Expand Down

0 comments on commit 181ee7f

Please sign in to comment.