Skip to content

Commit

Permalink
Fix bugs: better url encoding, handle no newline after rad block
Browse files Browse the repository at this point in the history
Also use Fprint instead of Fprintf in Printer where I can -- noticed
printing URLs would look very weird otherwise in the "Querying url: "
log, sometimes inserting "(MISSING)" tokens. Apparently that's from
doing Fprintf.
  • Loading branch information
amterp committed Sep 16, 2024
1 parent 67616a3 commit 9c663ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
17 changes: 13 additions & 4 deletions core/interpreter_rad.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/samber/lo"
"io"
"net/http"
"net/url"
)

type RadBlockInterpreter struct {
Expand Down Expand Up @@ -47,10 +48,9 @@ type radInvocation struct {
}

func (r *radInvocation) execute() {
RP.Print(fmt.Sprintf("Querying url: %s\n", r.url))
// todo encode url correctly, below doesn't work
// url = "http://url/?names=%{name}%" << the % needs to get encoded, for example
resp, err := http.Get(r.url)
urlToQuery := r.encodeUrl(r.url)
RP.Print(fmt.Sprintf("Querying url: %s\n", urlToQuery))
resp, err := http.Get(urlToQuery)
if err != nil {
r.error(fmt.Sprintf("Error on HTTP request: %v", err))
}
Expand Down Expand Up @@ -99,6 +99,15 @@ func (r *radInvocation) execute() {
tbl.Render()
}

// todo test this more, might need additional query param encoding
func (r *radInvocation) encodeUrl(rawUrl string) string {
parsedUrl, err := url.Parse(rawUrl)
if err != nil {
r.error(fmt.Sprintf("Error parsing URL: %v", err))
}
return parsedUrl.String()
}

func (r *radInvocation) error(msg string) {
r.ri.i.error(r.block.RadKeyword, msg)
}
4 changes: 2 additions & 2 deletions core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (p *Parser) radBlock() *RadBlock {
}
p.consumeNewlines()
var radStatements []RadStmt
for !p.matchAny(DEDENT) {
for !p.matchAny(DEDENT, EOF) {
radStatements = append(radStatements, p.radStatement())
p.consumeNewlines()
}
Expand Down Expand Up @@ -308,7 +308,7 @@ func (p *Parser) validateRadBlock(radBlock *RadBlock) {
func (p *Parser) radFieldsStatement() RadStmt {
var identifiers []Token
identifiers = append(identifiers, p.identifier())
for !p.matchAny(NEWLINE) {
for !p.matchAny(NEWLINE, DEDENT, EOF) {
p.consume(COMMA, "Expected ',' between identifiers")
identifiers = append(identifiers, p.identifier())
}
Expand Down
16 changes: 8 additions & 8 deletions core/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,27 @@ func (p *stdPrinter) Print(msg string) {
if p.isQuiet {
return
} else if p.isShellMode {
fmt.Fprintf(p.stdErr, msg)
fmt.Fprint(p.stdErr, msg)
} else {
fmt.Fprintf(p.stdOut, msg)
fmt.Fprint(p.stdOut, msg)
}
}

func (p *stdPrinter) PrintForShellEval(msg string) {
fmt.Fprintf(p.stdOut, "%s", msg)
fmt.Fprint(p.stdOut, msg)
}

func (p *stdPrinter) LexerErrorExit(msg string) {
if !p.isQuiet || p.isScriptDebug {
fmt.Fprintf(p.stdErr, msg)
fmt.Fprint(p.stdErr, msg)
}
p.printShellExitIfEnabled()
}

func (p *stdPrinter) TokenErrorExit(token Token, msg string) {
if !p.isQuiet || p.isScriptDebug {
if token == nil {
fmt.Fprintf(p.stdErr, msg)
fmt.Fprint(p.stdErr, msg)
} else {
lexeme := token.GetLexeme()
lexeme = strings.ReplaceAll(lexeme, "\n", "\\n")
Expand All @@ -135,14 +135,14 @@ func (p *stdPrinter) TokenErrorExit(token Token, msg string) {
}

func (p *stdPrinter) RadErrorExit(msg string) {
fmt.Fprintf(p.stdErr, msg)
fmt.Fprint(p.stdErr, msg)
p.printShellExitIfEnabled()
p.exit()
}

func (p *stdPrinter) RadTokenErrorExit(token Token, msg string) {
if token == nil {
fmt.Fprintf(p.stdErr, msg)
fmt.Fprint(p.stdErr, msg)
} else {
lexeme := token.GetLexeme()
lexeme = strings.ReplaceAll(lexeme, "\n", "\\n")
Expand All @@ -152,7 +152,7 @@ func (p *stdPrinter) RadTokenErrorExit(token Token, msg string) {
}

func (p *stdPrinter) UsageErrorExit(msg string) {
fmt.Fprintf(p.stdErr, msg)
fmt.Fprint(p.stdErr, msg)
p.cmd.Usage()
p.printShellExitIfEnabled()
p.exit()
Expand Down

0 comments on commit 9c663ae

Please sign in to comment.