go-python / gopy

gopy generates a CPython extension module from a go package.
BSD 3-Clause "New" or "Revised" License
2.05k stars 113 forks source link

main,bind: Extended gopy:name control, auto PEP8 naming, and property docstring support #252

Closed justinfx closed 3 years ago

justinfx commented 3 years ago

This merge improves the support for controlling the generates names of functions, methods, and properties in the python code. The "gopy:name" tag support has been extended to struct fields and updated to also support single line comments:

type MyStruct struct {
    // gopy_name: my_field
    // This is my field.
    MyField int

    // This is my second field
   // gopy_name: my_field2
   MyField2 int
}

A new optional feature flat -rename has been added to apply automatic PEP8 snake_case name transformations to functions, methods, properties, and var getter/setters. This makes it more convenient than having to annotate every symbol in Go source with a gopy:name tag if we only want CamelCase converted to snake_case to be more "pythonic".

And lastly, as part of parsing the comments for struct fields, the docstring is actually also applied to the python property getter in the generated code.

Objectives:

  1. Is the rename flag and auto naming feature useful and complete enough?
  2. Is the approach to parsing the struct field comments correct? I tried to review the existing way that method comments are propagated but it seemed fields were a bit different.
justinfx commented 3 years ago

@sbinet in hindsight I should have used struct tags. It makes more sense and probably would have saved me the trouble of trying to locate the comments for the field. I was only thinking of making them the same as the other types, but a struct tag is better suited for settings like this that would be removed from the doc string. Let me work up another commit to try and change this.

rcoreilly commented 3 years ago

Yeah I agree that on balance the field tag makes more sense. All kinds of impl stuff is leaked through those so I don't think this is any different, and is idiomatic in my experience.

justinfx commented 3 years ago

I've now added struct tag parsing of gopy:"new_name". Please let me know if you want me to remove the doc comment parsing logic entirely, or leave it in? I left it just for consistency with the other types, but the tag wins.

rcoreilly commented 3 years ago

the Go style is to have only one way to do things where possible, so probably remove it..

justinfx commented 3 years ago

I've removed the property getter/setter doc parsing for a name override. But I have left the struct field getDoc updates because they are needed to pull the comments to then set the property docstring in python

rcoreilly commented 3 years ago

Yep looks good to me too -- thanks for the contributions!