Better documentation generation

This commit is contained in:
Mike Farah 2020-11-13 21:22:05 +11:00
parent 019acfe456
commit d91b25840a
3 changed files with 72 additions and 42 deletions

View File

@ -6,43 +6,54 @@ import (
var collectOperatorScenarios = []expressionScenario{ var collectOperatorScenarios = []expressionScenario{
{ {
document: ``, description: "Collect empty",
expression: `[]`, document: ``,
expected: []string{}, expression: `[]`,
expected: []string{},
}, },
{ {
document: ``, description: "Collect single",
expression: `["cat"]`, document: ``,
expression: `["cat"]`,
expected: []string{ expected: []string{
"D0, P[], (!!seq)::- cat\n", "D0, P[], (!!seq)::- cat\n",
}, },
}, { }, {
document: ``, document: ``,
skipDoc: true,
expression: `[true]`, expression: `[true]`,
expected: []string{ expected: []string{
"D0, P[], (!!seq)::- true\n", "D0, P[], (!!seq)::- true\n",
}, },
}, { },
document: ``, {
expression: `["cat", "dog"]`, description: "Collect many",
document: `{a: cat, b: dog}`,
expression: `[.a, .b]`,
expected: []string{ expected: []string{
"D0, P[], (!!seq)::- cat\n- dog\n", "D0, P[], (!!seq)::- cat\n- dog\n",
}, },
}, { },
{
document: ``, document: ``,
skipDoc: true,
expression: `1 | collect`, expression: `1 | collect`,
expected: []string{ expected: []string{
"D0, P[], (!!seq)::- 1\n", "D0, P[], (!!seq)::- 1\n",
}, },
}, { },
{
document: `[1,2,3]`, document: `[1,2,3]`,
skipDoc: true,
expression: `[.[]]`, expression: `[.[]]`,
expected: []string{ expected: []string{
"D0, P[], (!!seq)::- 1\n- 2\n- 3\n", "D0, P[], (!!seq)::- 1\n- 2\n- 3\n",
}, },
}, { },
{
document: `a: {b: [1,2,3]}`, document: `a: {b: [1,2,3]}`,
expression: `[.a.b[]]`, expression: `[.a.b[]]`,
skipDoc: true,
expected: []string{ expected: []string{
"D0, P[a b], (!!seq)::- 1\n- 2\n- 3\n", "D0, P[a b], (!!seq)::- 1\n- 2\n- 3\n",
}, },

View File

@ -56,19 +56,34 @@ func writeOrPanic(w *bufio.Writer, text string) {
} }
} }
func copyFromHeader(title string, out *os.File) (bool, error) { func copyFromHeader(title string, out *os.File) error {
source := fmt.Sprintf("doc/headers/%v.md", title) source := fmt.Sprintf("doc/headers/%v.md", title)
_, err := os.Stat(source) _, err := os.Stat(source)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return false, nil return nil
} }
in, err := os.Open(source) // nolint gosec in, err := os.Open(source) // nolint gosec
if err != nil { if err != nil {
return false, err return err
} }
defer safelyCloseFile(in) defer safelyCloseFile(in)
_, err = io.Copy(out, in) _, err = io.Copy(out, in)
return true, err return err
}
func formatYaml(yaml string) string {
var output bytes.Buffer
printer := NewPrinter(bufio.NewWriter(&output), false, true, false, 2, true)
node, err := treeCreator.ParsePath(".. style= \"\"")
if err != nil {
panic(err)
}
err = EvaluateStream("sample.yaml", strings.NewReader(yaml), node, printer)
if err != nil {
panic(err)
}
return output.String()
} }
func documentScenarios(t *testing.T, title string, scenarios []expressionScenario) { func documentScenarios(t *testing.T, title string, scenarios []expressionScenario) {
@ -80,7 +95,7 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
} }
defer f.Close() defer f.Close()
hasHeader, err := copyFromHeader(title, f) err = copyFromHeader(title, f)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -88,26 +103,30 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
w := bufio.NewWriter(f) w := bufio.NewWriter(f)
if !hasHeader { writeOrPanic(w, "## Examples\n")
writeOrPanic(w, fmt.Sprintf("## %v\n", title))
}
for index, s := range scenarios { for index, s := range scenarios {
if !s.skipDoc { if !s.skipDoc {
if s.description != "" { if s.description != "" {
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description)) writeOrPanic(w, fmt.Sprintf("### %v\n", s.description))
} else { } else {
writeOrPanic(w, fmt.Sprintf("## Example %v\n", index)) writeOrPanic(w, fmt.Sprintf("### Example %v\n", index))
} }
if s.document != "" { if s.document != "" {
//TODO: pretty here //TODO: pretty here
writeOrPanic(w, "Given a sample.yml file of:\n") writeOrPanic(w, "Given a sample.yml file of:\n")
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.document))
} writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n", formatYaml(s.document)))
if s.expression != "" {
writeOrPanic(w, "then\n") writeOrPanic(w, "then\n")
writeOrPanic(w, fmt.Sprintf("```bash\nyq '%v' < sample.yml\n```\n", s.expression)) if s.expression != "" {
writeOrPanic(w, fmt.Sprintf("```bash\nyq eval '%v' sample.yml\n```\n", s.expression))
} else {
writeOrPanic(w, "```bash\nyq eval sample.yml\n```\n")
}
} else {
writeOrPanic(w, "Running\n")
writeOrPanic(w, fmt.Sprintf("```bash\nyq eval --null-input '%v'\n```\n", s.expression))
} }
writeOrPanic(w, "will output\n") writeOrPanic(w, "will output\n")
@ -132,7 +151,7 @@ func documentScenarios(t *testing.T, title string, scenarios []expressionScenari
} }
} }
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n", output.String())) writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String()))
} }

View File

@ -152,22 +152,22 @@ func EvaluateFileStreamsSequence(expression string, filenames []string, printer
// } // }
// thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang // thanks https://stackoverflow.com/questions/21060945/simple-way-to-copy-a-file-in-golang
func copyFileContents(src, dst string) (err error) { // func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src) // nolint gosec // in, err := os.Open(src) // nolint gosec
if err != nil { // if err != nil {
return err // return err
} // }
defer safelyCloseFile(in) // defer safelyCloseFile(in)
out, err := os.Create(dst) // out, err := os.Create(dst)
if err != nil { // if err != nil {
return err // return err
} // }
defer safelyCloseFile(out) // defer safelyCloseFile(out)
if _, err = io.Copy(out, in); err != nil { // if _, err = io.Copy(out, in); err != nil {
return err // return err
} // }
return out.Sync() // return out.Sync()
} // }
func safelyFlush(writer *bufio.Writer) { func safelyFlush(writer *bufio.Writer) {
if err := writer.Flush(); err != nil { if err := writer.Flush(); err != nil {