-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Writing a query to find declarations of nested structs in Golang. #17134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @aaaayush-n, It sounds like you're doing the right thing, but it's a bit hard to make suggestions with seeing the query code you currently have. Would you be able to share the current version of your query? |
Now I want to find the nested objects inside
but there is no such Suggest me some way to find out how to figure this out. |
Hi @aaaayush-n 👋 Thanks for sharing your query! What you have written looks a bit unusual and I am guessing you are coming from a background of object-oriented and imperative programming languages. If you haven't seen our documentation for the QL language yet, I'd recommend having a look for lots of advice for how to write good QL. In particular, you may find the section on recursion relevant to your question. The TypeDecl getObject(string objectName,string objDirPath){
exists(TypeName tn |
tn.toString()=objectName and
tn.getParent().getParent()=result and
result.getFile().toString().replaceAll(result.getFile().getBaseName().toString(), "").substring(0, result.getFile().toString().replaceAll(result.getFile().getBaseName().toString(), "").length()-1)=objDirPath
)
} Use concrete types rather than strings whenever possible. This can probably be further simplified as well, particularly the last line. In any case, the way I would break this problem down is to start with a predicate which, given some predicate isUsedInTypeDecl(TypeDecl decl, TypeName ty) {
result = ty.getParent()
} You can then extend/modify this to find all |
This would only give me the |
The definition I gave for |
Description of the issue
I want to write a query to find the nested structs in an struct in Golang.
Let's say my struct is something like:
and I want to write a query that gives me all the nested types in this struct ie.
statusCode,Response,Category,Time,Header,NewStruct
etc.The field types can be more complex than what is mentioned here.
My main problem statement is to find the declarations of these nested structs, which I will continue my query by finding all
typedecl
with theiridents
matching with the above mentionedidents
while matching the import path and the filepath for nested objects from different packages likedto.NewStruct
. If there is some better approach please suggest.Please suggest me an approach.
The text was updated successfully, but these errors were encountered: