-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add comments on service & method level (#66)
* add comments on service & method level * move comments to comment.ts * move method to comment.ts * clean * make arg look similar for method & service
- Loading branch information
1 parent
995deac
commit fd2abbb
Showing
5 changed files
with
251 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as plugin from '../../../pbjs-genfiles/plugin'; | ||
export interface Comments { | ||
[name: string]: string; | ||
} | ||
|
||
export class CommentsMap { | ||
comments: Comments = {}; | ||
|
||
constructor(fd: plugin.google.protobuf.IFileDescriptorProto) { | ||
const commentsMap: Comments = {}; | ||
if (fd && fd.sourceCodeInfo && fd.sourceCodeInfo.location) { | ||
const locations = fd.sourceCodeInfo.location; | ||
locations.forEach(location => { | ||
if (location.leadingComments !== null) { | ||
// p is an array with format [f1, i1, f2, i2, ...] | ||
// - f1 refers to the protobuf field tag | ||
// - if field refer to by f1 is a slice, i1 refers to an element in that slice | ||
// - f2 and i2 works recursively. | ||
// So, [6, x] refers to the xth service defined in the file, | ||
// since the field tag of Service is 6. | ||
// [6, x, 2, y] refers to the yth method in that service, | ||
// since the field tag of Method is 2. | ||
const p = location.path!; | ||
if (p.length === 2 && p[0] === 6) { | ||
if (fd.service && fd.service[p[1]] && fd.service[p[1]].name) { | ||
commentsMap[fd.service[p[1]].name! + 'Service'] = | ||
location.leadingComments || ''; | ||
} | ||
} else if (p.length === 4 && p[2] === 2) { | ||
if ( | ||
fd.service && | ||
fd.service[p[1]] && | ||
fd.service[p[1]].method && | ||
fd.service[p[1]].method![p[3]] && | ||
fd.service[p[1]].method![p[3]].name | ||
) { | ||
const name = fd.service[p[1]].method![p[3]].name!; | ||
if (!commentsMap[name]) { | ||
commentsMap[name] = location.leadingComments || ''; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
this.comments = commentsMap; | ||
} | ||
} | ||
getCommentsMap() { | ||
return this.comments; | ||
} | ||
getServiceComment(serviceName: string) { | ||
return this.comments[serviceName + 'Service'].trim(); | ||
} | ||
getMethodComments(methodName: string): string { | ||
return this.comments[methodName].trim(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.