-
Notifications
You must be signed in to change notification settings - Fork 11
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ package mdgen | |
|
||
import ( | ||
"bytes" | ||
"context" | ||
"os/exec" | ||
"strconv" | ||
"strings" | ||
|
@@ -17,8 +16,6 @@ import ( | |
) | ||
|
||
const ( | ||
infoStringKeyLang = "mdox-gen-lang" | ||
infoStringKeyType = "mdox-gen-type" | ||
infoStringKeyExec = "mdox-exec" | ||
infoStringKeyExitCode = "mdox-expect-exit-code" | ||
) | ||
|
@@ -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:]} | ||
} | ||
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] | ||
} | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? 🙂 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 (: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh okay! |
||
// TODO(bwplotka): To be done. | ||
return nil, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -40,3 +36,8 @@ test output3 | |
|
||
echo "test output3" | ||
``` | ||
|
||
```yaml mdox-exec="bash ./testdata/out2.sh --name=queryfrontend.InMemoryResponseCacheConfig" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice testcase! 💪🏼 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea. It was failing for us before |
||
test output2 | ||
newline | ||
``` |
There was a problem hiding this comment.
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,This also would only split at first "=" and return slice of length 2. WDYT?🙂
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.