0

I am trying to create a response like this:

"GameController" => [
    "methods" => [
        "get",
        "post",
        "put",
        "delete"
    ],
]

Following is the script I wrote for this:

main.go

package main

import (
    "fmt"
    "openapi/parse"
)

func main() {

    controllers := []string{"GameController", "CardController", "ShuffleController"}

    fmt.Println(parse.GetHTTPMethods(controllers))
}

get_http_methods.go

package parse

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

    // GetHTTPMethods will return the available http methods of each controller
    func GetHTTPMethods(controllers []string) map[string][]string {
        allcontrollers := map[string][]string{}
        for _, filename := range controllers {
            allcontrollers[filename] = SearchMethod(filename)
        }
        
        return allcontrollers
    }
    //SearchMethod will read each controller and search for available http methods
    func SearchMethod(filename string) map[string][]string {
        path := "../hbapi/application/controllers/" + filename + ".php"
        method := map[string][]string{}
        f, err := os.Open(path)
        if err != nil {
            fmt.Println("Error : ", err)
            os.Exit(1)
        }
        defer f.Close()
        scanner := bufio.NewScanner(f)
        for scanner.Scan() {
            if strings.Contains(scanner.Text(), "Get") {
                method["method"] = append(method["method"], "get")
            }
            if strings.Contains(scanner.Text(), "Post") {
                method["method"] = append(method["method"], "post")
            }
            if strings.Contains(scanner.Text(), "Put") {
                method["method"] = append(method["method"], "put")
            }
            if strings.Contains(scanner.Text(), "Delete") {
                method["method"] = append(method["method"], "delete")
            }
        }
        if err := scanner.Err(); err != nil {
            fmt.Println("Error : ", err)
            os.Exit(1)
        }
        return method
    }

But this script is showing the following error. How can I fix this error? Please advice.

cannot use SearchMethod(filename) (type map[string][]string) as type []string in assignment go

the error is showing up in this line allcontrollers[filename] = SearchMethod(filename)

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Anish B
  • 23
  • 2
  • 10
  • In the last file there is no need to keep method as a map[string][]string{} as you only fill the `method` key. If you change the function signature to be `SearchMethod(filename string) []string` and `method := []string{}` and instead append to that it should work as intended. – Muhamed Keta Feb 08 '21 at 11:40
  • Alternatively you can only change: `allcontrollers[filename] = SearchMethod(filename)["method"]`, however you're not using the rest of the SearchMethod returned map keys. – Muhamed Keta Feb 08 '21 at 11:42
  • changing the var method to `method := []string{}` does not allow me to append like this: `method["method"] = append(method["method"], "post")`. It is displaying `non-integer slice index "method"` error – Anish B Feb 08 '21 at 12:11
  • This actually fix the error but the index "method" is missing in the final resposne. `allcontrollers[filename] = SearchMethod(filename)["method"]` Response: `map[BrandController:[get] BrowserController:[get post put delete] MemberController:[get post put]]` – Anish B Feb 08 '21 at 12:14
  • You can make another struct that holds an array of strings. See https://goplay.space/#WMFmtVimTiE – Muhamed Keta Feb 08 '21 at 14:08
  • Wow! That is a good solution. Thanks @MuhamedKeta! This also works for me. I am. keeping a note of this. – Anish B Feb 09 '21 at 09:14

1 Answers1

2

You are getting that error because of the way you are defining the var "allcontrollers". You should define it as the following:

allcontrollers := map[string](map[string][]string){}

With this, the problem will be solved.