alecholmez/GolangToMarkdown

Code review

Opened this issue · 0 comments

Good start. Initial comments.

  1. Use a linter in Atom and do what it says when you get warnings. See setup here. You will probably need to launch atom from the command line if you don't already.
  2. We should not define a DSL that makes us write markdown in Go. Go provides "multi-line" comments, but they are in fact discouraged in source code itself. Check out this snippet from the Standard Library: https://golang.org/src/bufio/scan.go?s=9745:9823#L280 Comments everywhere, but all with //. I think given the metadata about where comments appear that Go can give us (see next comment) will let us avoid mixing data (comments about our functions, for instance) with presentation (H1, H2, p tags, etc).
  3. Instead of parsing the comments manually, try an approach that uses the built-in Golang parser. See this package: https://golang.org/pkg/go/ast/ And google "Golang ast package parsing". The ast package will take a go file as input, and give you back a data structure that reliably identifies comments (of different types), function names, package names, and more. This is a wheel you do not want to revinent.
  4. Write a test, and check in source files that validate that test. Possible project structure:
/GolangToMarkdown
  /g2markdown   <-- give your binary a nicer name
    main.go
  /genMarkdown
    genMarkdown.go
    genMarkdown_test.go
  /testfiles
    source.go      <-- parse this
    output.md      <-- this is generated. make some reasonable assertions about this in tests

Example genMarkdown_test.go:

package genMarkdown

import (
  "testing"
  "strings"
)

func TestGenMarkdown(t *testing.T) {

   err := ParseGoSource("../testfiles/source.go")
   if err != nil {
      t.Fail()
   }
   lines, err := ReadLines("../testfiles/output.md")
   if err != nil {
     t.Fail()
   }

   trimmed := strings.Trim(lines[3])
   if !strings.Contains(trimmed, "### FunctionName") {
      log.Println("Expected line to contain FunctionName")
      t.Fail()
   }
}
  1. Consider using the builtin text/template package for writing output. You can define templates and inject data into them.