Skip to content
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

read comments from fields of anonymous structs #32

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

arvidfm
Copy link
Contributor

@arvidfm arvidfm commented Jul 11, 2022

This PR adds support for reading documentation comments from fields of anonymous structs. The code keeps track of what fields it has traversed, and builds up a breadcrumb that includes every parent field.

Consider the following fields:

type A {
    Val1 int
    Val2 struct {
        StructVal int
    }
    Val3 []struct {
        SliceVal int
    }
    Val4 map[struct {
        MapKeyVal int
    }]struct {
        MapValueVal int
    }
}

These fields will, respectively, have the following breadcrumbs, used (along with the fully qualified name of the package) as the key in the comments map:

  • A.Val1
  • A.Val2.StructVal
  • A.Val3.[].SliceVal
  • A.Val4.[key].MapKeyVal
  • A.Val4.[value].MapValueVal

Note that any time the code encounters a non-anonymous type, it resets the breadcrumb to the type's fully qualified name.

Tested with a decently large codebase.

@samlown samlown added the needs tests Not enough tests for this to be accepted label Jul 21, 2022
@arvidfm
Copy link
Contributor Author

arvidfm commented Aug 4, 2022

@samlown I've added tests now

@arvidfm
Copy link
Contributor Author

arvidfm commented Jan 30, 2024

@samlown I cleaned up the breadcrumb tracking a bit to make the code more readable, and I also added a fix so that comments of generic classes are fetched correctly. Let me know if there's anything else I could do to help upstream this, since the lack of support for anonymous structs is forcing us to use a fork for our documentation at the moment.

Instead of explicitly passing the breadcrumb/path as a parameter everywhere, I also toyed with the idea of a pattern like:

r.withField(f.Name, func() {
	// f.Name is at the top of the breadcrumb stack here
	property = r.refOrReflectTypeToSchema(definitions, f.Type)
})
// f.Name has been popped from the stack here

This would greatly reduce the number of places where you need to interact with the breadcrumbs, but it's also a bit volatile since it's easier to forget to include it. Let me know if it looks a bit more palatable to you, though.

Alternatively, perhaps the cleanest solution would be to add thin wrappers around reflect.Type/reflect.StructField that automatically track how types are accessed when you call Elem()/Field()/etc and provide FullyQualifiedName() methods that works for anonymous structs too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs tests Not enough tests for this to be accepted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants