2929 headerSetPattern = regexp .MustCompile (`^([ \t]*)(\w+)\.Header\.Set\(([^,]+),\s*([^)]*)\)\s*$` )
3030 requestURIPattern = regexp .MustCompile (`^([ \t]*)(\w+)\.SetRequestURI\((.*)\)\s*$` )
3131 parseCallPattern = regexp .MustCompile (`^([ \t]*)if\s+err\s*:=\s*(\w+)\.Parse\(\);\s*err\s*!=\s*nil\s*{\s*$` )
32- requestBodyPattern = regexp .MustCompile (`^([ \t]*)(\w+)\.SetBody(?:String)?\((.*)\)\s*$` )
32+ requestBodyPattern = regexp .MustCompile (`^([ \t]*)(\w+)\.( SetBody(?:String)?) \((.*)\)\s*$` )
3333 structAssignPattern = regexp .MustCompile (`^([ \t]*)if\s+([^,]+?)\s*,\s*([^,]+?)\s*,\s*errs\s*([:=]?)=\s*(\w+)\.Struct\((.*)\);\s*len\(errs\)\s*>\s*0\s*{\s*$` )
3434 bytesAssignPattern = regexp .MustCompile (`^([ \t]*)([^,]+),\s*([^,]+),\s*errs\s*(=|:=)\s*(\w+)\.Bytes\(\)\s*$` )
3535 stringAssignPattern = regexp .MustCompile (`^([ \t]*)([^,]+),\s*([^,]+),\s*errs\s*(=|:=)\s*(\w+)\.String\(\)\s*$` )
@@ -175,6 +175,7 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
175175 uriExpr := ""
176176 headers := make (map [string ]string )
177177 bodyExpr := ""
178+ bodyIsString := false
178179
179180 j := reqLine + 1
180181 for ; j < len (lines ); j ++ {
@@ -195,7 +196,8 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
195196 continue
196197 }
197198 if m := requestBodyPattern .FindStringSubmatch (l ); len (m ) > 0 && m [2 ] == reqVar {
198- bodyExpr = strings .TrimSpace (m [3 ])
199+ bodyExpr = strings .TrimSpace (m [4 ])
200+ bodyIsString = m [3 ] == "SetBodyString"
199201 continue
200202 }
201203 if parseCallPattern .MatchString (l ) {
@@ -275,8 +277,6 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
275277 structMatch := structAssignPattern .FindStringSubmatch (lines [structStart ])
276278 bytesMatch := bytesAssignPattern .FindStringSubmatch (lines [structStart ])
277279 stringMatch := stringAssignPattern .FindStringSubmatch (lines [structStart ])
278- methodName := methodFromExpr (methodExpr )
279- configLine := buildConfig (headers , bodyExpr )
280280
281281 if len (structMatch ) == 0 && len (bytesMatch ) == 0 && len (stringMatch ) == 0 {
282282 structStart = parseEnd
@@ -286,6 +286,34 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
286286
287287 errName := chooseErrName (out , lines , i )
288288
289+ out = append (out , fmt .Sprintf ("%s%s := client.New()" , indent , agentVar ))
290+ out = append (out , fmt .Sprintf ("%s%s := %s.R()" , indent , reqVar , agentVar ))
291+ if methodExpr != "" {
292+ out = append (out , fmt .Sprintf ("%s%s.SetMethod(%s)" , indent , reqVar , methodLiteral (methodExpr )))
293+ }
294+ if uriExpr != "" {
295+ out = append (out , fmt .Sprintf ("%s%s.SetURL(%s)" , indent , reqVar , uriExpr ))
296+ }
297+
298+ if len (headers ) > 0 {
299+ keys := make ([]string , 0 , len (headers ))
300+ for k := range headers {
301+ keys = append (keys , k )
302+ }
303+ sort .Strings (keys )
304+ for _ , k := range keys {
305+ out = append (out , fmt .Sprintf ("%s%s.SetHeader(%s, %s)" , indent , reqVar , k , headers [k ]))
306+ }
307+ }
308+
309+ if bodyExpr != "" {
310+ rawBody := bodyExpr
311+ if bodyIsString {
312+ rawBody = fmt .Sprintf ("[]byte(%s)" , bodyExpr )
313+ }
314+ out = append (out , fmt .Sprintf ("%s%s.SetRawBody(%s)" , indent , reqVar , rawBody ))
315+ }
316+
289317 switch {
290318 case len (structMatch ) > 0 && structMatch [5 ] == agentVar :
291319 if len (preservedLines ) > 0 {
@@ -317,7 +345,7 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
317345 continue
318346 }
319347
320- respLine := fmt .Sprintf ("%sresp, %s %s client.%s(%s%s )" , indent , errName , errAssign , methodName , uriExpr , configLine )
348+ respLine := fmt .Sprintf ("%sresp, %s %s %s.Send( )" , indent , errName , errAssign , reqVar )
321349
322350 out = append (out , respLine )
323351 out = append (out , fmt .Sprintf ("%sif %s != nil {" , parseIndent , errName ))
@@ -365,7 +393,7 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
365393 statusDeclared := statusVar != "" && (identifierDeclaredInLines (preservedLines , statusVar ) || identifierDeclared (out , lines , i , statusVar ))
366394 bodyDeclared := bodyVar != "" && (identifierDeclaredInLines (preservedLines , bodyVar ) || identifierDeclared (out , lines , i , bodyVar ))
367395
368- respLine := fmt .Sprintf ("%sresp, %s %s client.%s(%s%s )" , indent , errName , errAssign , methodName , uriExpr , configLine )
396+ respLine := fmt .Sprintf ("%sresp, %s %s %s.Send( )" , indent , errName , errAssign , reqVar )
369397 out = append (out , respLine )
370398 out = append (out , fmt .Sprintf ("%sif %s != nil {" , parseIndent , errName ))
371399 out = append (out , replaceErrIdentifier (parseBody [:len (parseBody )- 1 ], errName , false )... )
@@ -404,7 +432,7 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
404432 statusDeclared := statusVar != "" && (identifierDeclaredInLines (preservedLines , statusVar ) || identifierDeclared (out , lines , i , statusVar ))
405433 bodyDeclared := bodyVar != "" && (identifierDeclaredInLines (preservedLines , bodyVar ) || identifierDeclared (out , lines , i , bodyVar ))
406434
407- respLine := fmt .Sprintf ("%sresp, %s %s client.%s(%s%s )" , indent , errName , errAssign , methodName , uriExpr , configLine )
435+ respLine := fmt .Sprintf ("%sresp, %s %s %s.Send( )" , indent , errName , errAssign , reqVar )
408436 out = append (out , respLine )
409437 out = append (out , fmt .Sprintf ("%sif %s != nil {" , parseIndent , errName ))
410438 out = append (out , replaceErrIdentifier (parseBody [:len (parseBody )- 1 ], errName , false )... )
@@ -432,7 +460,7 @@ func rewriteAcquireAgentBlocksWithAlias(content, alias string) (string, bool) {
432460 continue
433461 default :
434462 errAssign := errAssignmentOperator (errName , out , lines , i )
435- respLine := fmt .Sprintf ("%s_, %s %s client.%s(%s%s )" , indent , errName , errAssign , methodName , uriExpr , configLine )
463+ respLine := fmt .Sprintf ("%s_, %s %s %s.Send( )" , indent , errName , errAssign , reqVar )
436464 out = append (out , respLine )
437465 out = append (out , fmt .Sprintf ("%sif %s != nil {" , parseIndent , errName ))
438466 out = append (out , replaceErrIdentifier (parseBody [:len (parseBody )- 1 ], errName , false )... )
@@ -620,51 +648,26 @@ func declaredInVarBlockLine(name, trimmed string) bool {
620648 return true
621649}
622650
623- func methodFromExpr (expr string ) string {
651+ func methodLiteral (expr string ) string {
624652 lower := strings .ToLower (expr )
625653 switch {
654+ case strings .Contains (lower , "methodget" ):
655+ return "\" GET\" "
626656 case strings .Contains (lower , "methodpost" ):
627- return "Post "
657+ return "\" POST \" "
628658 case strings .Contains (lower , "methodput" ):
629- return "Put "
659+ return "\" PUT \" "
630660 case strings .Contains (lower , "methodpatch" ):
631- return "Patch "
661+ return "\" PATCH \" "
632662 case strings .Contains (lower , "methoddelete" ):
633- return "Delete "
663+ return "\" DELETE \" "
634664 case strings .Contains (lower , "methodhead" ):
635- return "Head "
665+ return "\" HEAD \" "
636666 default :
637- return "Get"
667+ return expr
638668 }
639669}
640670
641- func buildConfig (headers map [string ]string , body string ) string {
642- if len (headers ) == 0 && body == "" {
643- return ""
644- }
645-
646- var keys []string
647- for k := range headers {
648- keys = append (keys , k )
649- }
650- sort .Strings (keys )
651-
652- var parts []string
653- if len (keys ) > 0 {
654- var headerParts []string
655- for _ , k := range keys {
656- headerParts = append (headerParts , fmt .Sprintf ("%s: %s" , k , headers [k ]))
657- }
658- parts = append (parts , fmt .Sprintf ("Header: map[string]string{%s}" , strings .Join (headerParts , ", " )))
659- }
660-
661- if body != "" {
662- parts = append (parts , "Body: " + body )
663- }
664-
665- return fmt .Sprintf (", client.Config{%s}" , strings .Join (parts , ", " ))
666- }
667-
668671func rewriteClientExamplesWithAlias (content , alias string ) (string , bool ) {
669672 patterns := buildAliasPatterns (alias )
670673
0 commit comments