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

Fixed mdox gen errors on command with = inside (common case). #41

Merged
merged 1 commit into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions pkg/mdformatter/mdgen/mdgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package mdgen

import (
"bytes"
"context"
"os/exec"
"strconv"
"strings"
Expand All @@ -17,8 +16,6 @@ import (
)

const (
infoStringKeyLang = "mdox-gen-lang"
infoStringKeyType = "mdox-gen-type"
infoStringKeyExec = "mdox-exec"
infoStringKeyExitCode = "mdox-expect-exit-code"
)
Expand All @@ -40,19 +37,22 @@ func (t *genCodeBlockTransformer) TransformCodeBlock(ctx mdformatter.SourceConte
}
infoStringAttr := map[string]string{}
for i, field := range infoFiels {
val := strings.Split(field, "=")
val := []string{field}
if i := strings.Index(field, "="); i != -1 {
val = []string{field[:i], field[i+1:]}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Now it is only split at first "=". But maybe instead of using Index this can be done with something like,

val := strings.SplitN(field, "=", 2)

This also would only split at first "=" and return slice of length 2. WDYT?🙂

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to use it, but found it confusing why 2 not 1.. (: will try thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 2 is the number of substrings returned...but yes it is not intuitive. This can be skipped then I guess.

}
if i == 0 && len(val) == 2 {
return nil, errors.Errorf("missing language info in fenced code block. Got info string %q", string(infoString))
}
switch val[0] {
case infoStringKeyExec, infoStringKeyExitCode:
case infoStringKeyExec:
if len(val) != 2 {
return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %q=<value2> %q=<value2>. Got info string %q", val[0], infoStringKeyExitCode, infoStringKeyExec, string(infoString))
return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %s=\"<value1>\" but got %s", val[0], infoStringKeyExec, string(infoString))
}
infoStringAttr[val[0]] = val[1]
case infoStringKeyLang, infoStringKeyType:
case infoStringKeyExitCode:
if len(val) != 2 {
return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %q=<value2> %q=<value2>. Got info string %q", val[0], infoStringKeyLang, infoStringKeyType, string(infoString))
return nil, errors.Errorf("got %q without variable. Expected format is e.g ```yaml %s=\"<value1>\" but got %s", val[0], infoStringKeyExitCode, string(infoString))
}
infoStringAttr[val[0]] = val[1]
}
Expand Down Expand Up @@ -82,30 +82,12 @@ func (t *genCodeBlockTransformer) TransformCodeBlock(ctx mdformatter.SourceConte
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == expectedCode {
return b.Bytes(), nil
}
return nil, errors.Wrapf(err, "run %v", execCmd)
return nil, errors.Wrapf(err, "run %v, out: %v", execCmd, b.String())
}
return b.Bytes(), nil
}

lang, langOk := infoStringAttr[infoStringKeyLang]
typePath, typOk := infoStringAttr[infoStringKeyType]
if typOk || langOk {
if typOk != langOk {
return nil, errors.Errorf("got ambiguous attributes: %v. Expected is e.g ```yaml %q=<value> %q=go . Got info string %q", infoStringAttr, infoStringKeyType, infoStringKeyLang, string(infoString))
}
switch lang {
case "go", "golang":
return genGo(ctx, "", typePath)
default:
return nil, errors.Errorf("expected language a first element of info string got %q; Got info string %q", lang, string(infoString))
}
}
panic("should never get here")
}

func (t *genCodeBlockTransformer) Close(ctx mdformatter.SourceContext) error { return nil }

func genGo(ctx context.Context, moduleRoot string, typePath string) ([]byte, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was to be done as mentioned in #23. Do you have something else in mind? 🙂

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part is not clearly understood yet. We don't know yet how it should look.

This snippet was mainly for the beginning to show you and Uche how we might want to do it.

Normally it's not the best to maintain code which is not used (YAGNI) so killed it (:

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay!

// TODO(bwplotka): To be done.
return nil, nil
}
9 changes: 5 additions & 4 deletions pkg/mdformatter/mdgen/testdata/mdgen_formatted.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
test output
```

```yaml mdox-gen-lang="go" mdox-gen-type="github.com/bwplotka/mdox/pkg/mdox/testdata.Config"
TO BE DONE
```

```yaml
abc
sad
Expand Down Expand Up @@ -40,3 +36,8 @@ test output3

echo "test output3"
```

```yaml mdox-exec="bash ./testdata/out2.sh --name=queryfrontend.InMemoryResponseCacheConfig"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice testcase! 💪🏼

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea. It was failing for us before

test output2
newline
```
7 changes: 3 additions & 4 deletions pkg/mdformatter/mdgen/testdata/mdgen_not_formatted.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ a
adf
```

```yaml mdox-gen-lang="go" mdox-gen-type="github.com/bwplotka/mdox/pkg/mdox/testdata.Config"
TO BE DONE
```

```yaml
abc
sad
Expand Down Expand Up @@ -41,3 +37,6 @@ abc

```bash mdox-exec="sed -n '1,3p' ./testdata/out3.sh"
```

```yaml mdox-exec="bash ./testdata/out2.sh --name=queryfrontend.InMemoryResponseCacheConfig"
```