Skip to content

Commit

Permalink
Added FORM
Browse files Browse the repository at this point in the history
  • Loading branch information
RijulTP committed Oct 1, 2023
1 parent 1efcf44 commit c024bcc
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 15 deletions.
7 changes: 5 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [


{
"type": "by-gdb",
"request": "launch",
Expand All @@ -20,7 +22,8 @@
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"args": ["-h"]
}
"args": ["testform1.l2"],
"dlvFlags": ["--check-go-version=false"]
},
]
}
Binary file added __debug_bin4205283238
Binary file not shown.
Binary file added archive/l2
Binary file not shown.
26 changes: 20 additions & 6 deletions cmdgen/cmdgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import (
"github.com/rs/zerolog/log"
)

func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, headers *gabs.Container, multipart bool, o *lama2cmd.Opts) ([]string, string) {
func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, headers *gabs.Container, multipart bool, form bool, o *lama2cmd.Opts) ([]string, string) {
command := make([]string, 0)
log.Info().
Str("Type", "Construct Command").
Str("httpv", httpv).
Str("url", url).
Bool("multipart", multipart).
Bool("form",form).
Msg(fmt.Sprint("Construct parameters"))

log.Debug().
Expand All @@ -39,7 +40,7 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header
}

jsonStr := ""
if jsonObj != nil && !multipart {
if jsonObj != nil && !multipart && !form {
dst := &bytes.Buffer{}
if err := json.Compact(dst, []byte(jsonObj.String())); err != nil {
log.Fatal().
Expand All @@ -60,8 +61,9 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header
if o.Nocolor {
command = append(command, "--pretty=none ")
}
if multipart {
command = append(command, "--headers", "--ignore-stdin", "--form")
if multipart || form {
command = append(command, //"--headers",
"--ignore-stdin", "--form")
}

command = append(command, httpv+" ")
Expand All @@ -76,6 +78,13 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header
}
}

if form {
for key, val := range jsonObj.Data().(*gabs.Container).ChildrenMap() {
command = append(command, "'"+key+"'='"+val.Data().(string)+"' ")
}

}

if headers != nil {
for key, val := range headers.Data().(*gabs.Container).ChildrenMap() {
command = append(command, key+":"+val.Data().(*gabs.Container).Data().(string))
Expand All @@ -86,7 +95,7 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header
cleanC := strings.TrimSpace(c)
cleanCommand = append(cleanCommand, cleanC)
}
if multipart {
if multipart || form {
return cleanCommand, ""
}
return cleanCommand, jsonStr
Expand All @@ -107,7 +116,12 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string,
if multipart != nil {
multipartBool = true
}
form := parsedInput.S("form","value")
formBool := false
if form !=nil {
formBool = true
}

res, stdinBody := assembleCmdString(httpv.Data().(string), url.Data().(string), jsonObj, headers, multipartBool, o)
res, stdinBody := assembleCmdString(httpv.Data().(string), url.Data().(string), jsonObj, headers, multipartBool,formBool, o)
return res, stdinBody
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/HexmosTech/lama2

go 1.19
go 1.18

require (
github.com/HexmosTech/gabs/v2 v2.6.5
Expand Down
Binary file added image.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions local_update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
make buildme
sudo mv build/l2 /usr/local/bin
19 changes: 19 additions & 0 deletions parser/lama2parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ func (p *Lama2Parser) Requester() (*gabs.Container, error) {
temp.Set(res, "multipart")
}

res, e = p.Match([]string{"Form"})
if e == nil {
temp.Set(res, "form")
}

res, e = p.Match([]string{"TheURL"})
if e == nil {
temp.Set(res, "url")
Expand Down Expand Up @@ -230,6 +235,7 @@ func (p *Lama2Parser) Multipart() (*gabs.Container, error) {
_, e := p.Keyword("multipart", true, true, true)
if e == nil {
temp := gabs.New()

temp.Set(true, "value")
temp.Set("Multipart", "type")
p.Context["multipart"] = true
Expand All @@ -239,6 +245,19 @@ func (p *Lama2Parser) Multipart() (*gabs.Container, error) {
"Expected 'multipart', but couldn't find", []string{})
}

func (p *Lama2Parser) Form() (*gabs.Container, error) {
_, e := p.Keyword("form", true, true, true)
if e == nil {
temp := gabs.New()
temp.Set(true, "value")
temp.Set("Form", "type")
p.Context["form"] = true
return temp, nil
}
return nil, utils.NewParseError(p.Pos+1, p.LineNum+1,
"Expected 'form', but couldn't find", []string{})
}

func (p *Lama2Parser) Details() (*gabs.Container, error) {
res, e := p.Match([]string{"HeaderData", "DataHeader"})
if e == nil {
Expand Down
13 changes: 9 additions & 4 deletions parser/varjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@ import (
)

// Method VarJSON behaves in two ways depending
// on whether `multipart` is true or not.
// on whether `multipart` or `form` is true or not.
// If there is no multipart, then VarJSON tries
// to match one or more VarJSONPairs
// However, if there is multipart, we try to match
// However, if there is multipart or form, we try to match
// zero or more VarJSON, followed by zero or more
// file fields (separated by `@`). If there is no match
// at all, we return a ParseError; otherwise the
// we return the parsed data.
func (p *Lama2Parser) VarJSON() (*gabs.Container, error) {
temp := gabs.New()
hasMultipart := false
hasForm := false
if val, ok := p.Context["multipart"]; ok {
hasMultipart = val
}

if val, ok := p.Context["form"]; ok {
hasForm = val
}

pair, e1 := p.Match([]string{"VarJSONPair"})
if e1 != nil && !hasMultipart {
if e1 != nil && (!hasMultipart || !hasForm) {
return nil, e1
}
temp.Merge(pair)
Expand All @@ -38,7 +43,7 @@ func (p *Lama2Parser) VarJSON() (*gabs.Container, error) {
temp.Merge(pair)
}

if hasMultipart {
if (hasMultipart) {
filesObj := gabs.New()
for {
pair, e1 = p.Match([]string{"FilesPair"})
Expand Down
2 changes: 1 addition & 1 deletion prettify/prettify.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Prettify(parsedAPI *gabs.Container, context map[string]bool, markRange map[
jsonObj := block.S("details", "ip_data")

res := content[:markRange["DataStart"]] + jsonObj.StringIndent("", " ") + "\n" + content[markRange["DataEnd"]:]
os.WriteFile(fPath, []byte(res), 0644)
os.WriteFile(fPath, []byte(res), 0o644)
}
}
}
8 changes: 8 additions & 0 deletions testform1.l2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
POST
FORM
http://httpbin.org/post

# DATA
first='second'


9 changes: 9 additions & 0 deletions testform2.l2
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
POST
MULTIPART
https://httpbin.org/post

# HEADERS
Cookie:"sessionid=foo;another-cookie=bar"

# DATA
hello=world
12 changes: 12 additions & 0 deletions testmulti.l2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
POST
MULTIPART
http://httpbin.org/post

'X-Parse-Application-Id':hello
X-Parse-REST-API-Key:"world"

# DATA
first=second

# FILES
myfile@./image.jpeg
2 changes: 1 addition & 1 deletion tests/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestSplitLangLib2(t *testing.T) {
}
}

func TestGenerateMultiStage(t *testing.T) {
func TestGenerateMultiStage(_ *testing.T) {
l2Path := "../examples/0009_processor_basic/0009_processor_basic.l2"
apiContent, _ := os.ReadFile(l2Path)
_, dir, _ := utils.GetFilePathComponents(l2Path)
Expand Down

0 comments on commit c024bcc

Please sign in to comment.