Skip to content

Commit

Permalink
Moving back to structs
Browse files Browse the repository at this point in the history
  • Loading branch information
samling committed May 1, 2018
1 parent c3b4e94 commit 253a60d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 41 deletions.
8 changes: 4 additions & 4 deletions Config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ Search:
URL: "https://santabarbara.craigslist.org/search/apa"

SMTP:
host:
port:
user:
pass:
host: test
port: test2
user: test3
pass:

Query:
#------ SEARCH OPTIONS ------#
Expand Down
Binary file modified main
Binary file not shown.
109 changes: 72 additions & 37 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,99 @@ import (
"io/ioutil"
"log"
"net/url"
"strings"
//"strings"

"gopkg.in/yaml.v2"
)

func main() {
m := getConfigMap()
q := createQueryString(m)
fmt.Println(q)
type Config struct {
Search struct {
URL string `yaml:"URL"`
} `yaml:"Search"`

SMTP struct {
Host string `yaml:"host,omitempty"`
Port string `yaml:"port,omitempty"`
User string `yaml:"user,omitempty"`
Pass string `yaml:"pass,omitempty"`
} `yaml:"SMTP,omitempty"`

Query struct {
HasPic string `yaml:"hasPic"`
} `yaml:"Query,omitempty"`
}

func getConfigMap() map[string]map[string]string {
// Since the nested values are also a map, need to do some mapception
m := make(map[string]map[string]string)
func main() {
c := Config{}
c.getConf()
fmt.Println(c)
//m := getConfigMap()
createQueryString(c)
//fmt.Println(q)
}

func (c *Config) getConf() *Config {
yamlFile, err := ioutil.ReadFile("Config.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%s ", err)
}

// Dynamically unmarshal key/value pairs into our map of string maps
err = yaml.Unmarshal(yamlFile, m)
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatal(err)
log.Fatalf("Unmarshal: %v", err)
}
fmt.Printf("%v", c)

return m
return c
}

func createQueryString(m map[string]map[string]string) *url.URL {
u, err := url.Parse(m["Search"]["URL"])
//func getConfigMap() map[string]map[string]string {
// // Since the nested values are also a map, need to do some mapception
// m := make(map[string]map[string]string)
//
// yamlFile, err := ioutil.ReadFile("Config.yaml")
// if err != nil {
// log.Printf("yamlFile.Get err #%s ", err)
// }
//
// // Dynamically unmarshal key/value pairs into our map of string maps
// err = yaml.Unmarshal(yamlFile, m)
// if err != nil {
// log.Fatal(err)
// }
//
// return m
//}

func createQueryString(c Config) *url.URL {
u, err := url.Parse(c.Search.URL)
if err != nil {
log.Fatal(err)
}

// Create an empty query string that we Set() key/value pairs on; results in "key=value&key=value&..."
q := u.Query()
for k, v := range m["Query"] {
// TODO: Handle sequence of values so that they result in a series of duplicate keys, e.g. housing=1&housing=2&housing=3
if strings.Contains(v, ",") { // Tried giving values in YAML file like: "housing: 1,2"
s := strings.Split(v, ",")
for i, j := range s {
q.Set(k, j)
fmt.Println(i)
fmt.Println(j)
// Guess there'd be a 'continue' here or something
}
fmt.Println(k)
fmt.Println(v)
}
q.Set(k, v) // But what to do about this if it's a sequence...
// Would rather set value in config like:
// housing: [1 2]
// Problem is map explicitly contains strings, so results in: "line 63: cannot unmarshal !!seq into string"
// Tried changing map to map[string]map[string]interface{} but then it gets messy with type conversions etc.
}

u.RawQuery = q.Encode()

// q := u.Query()
// for k, v := range m["Query"] {
// // TODO: Handle sequence of values so that they result in a series of duplicate keys, e.g. housing=1&housing=2&housing=3
// if strings.Contains(v, ",") { // Tried giving values in YAML file like: "housing: 1,2"
// s := strings.Split(v, ",")
// for i, j := range s {
// q.Set(k, j)
// fmt.Println(i)
// fmt.Println(j)
// // Guess there'd be a 'continue' here or something
// }
// fmt.Println(k)
// fmt.Println(v)
// }
// q.Set(k, v) // But what to do about this if it's a sequence...
// // Would rather set value in config like:
// // housing: [1 2]
// // Problem is map explicitly contains strings, so results in: "line 63: cannot unmarshal !!seq into string"
// // Tried changing map to map[string]map[string]interface{} but then it gets messy with type conversions etc.
// }
//
// u.RawQuery = q.Encode()
//
return u
}

0 comments on commit 253a60d

Please sign in to comment.