Skip to content

Instantly share code, notes, and snippets.

@tbillington
Created December 22, 2015 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbillington/c8cd735c02b11ffe4cac to your computer and use it in GitHub Desktop.
Save tbillington/c8cd735c02b11ffe4cac to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"io"
"os"
)
type parts struct {
Parts []part `json:parts`
}
type part struct {
Type string `json:type`
Args map[string]string `json:args`
Properties map[string]string `json:properties`
}
type binFunc func(interface{})
func main() {
argsWithoutProg := os.Args[1:]
fmt.Println(argsWithoutProg)
f, err := os.Open(os.Args[1])
if err != nil {
panic(err)
}
var jsonParts parts
dec := json.NewDecoder(f)
for {
//var m map[string]interface{}
if err := dec.Decode(&jsonParts); err == io.EOF {
break
} else if err != nil {
panic(err)
}
fmt.Println(jsonParts)
}
stuff := map[string]binFunc{}
for _, p := range jsonParts.Parts {
fmt.Println(p)
stuff[p.Type] = func(newPart part) {
return func(data interface{}) {
fmt.Println(newPart.Args)
fmt.Println(newPart.Properties)
}
}(p)
}
for k, t := range stuff {
fmt.Println(k)
t(nil)
}
}
@dhowden
Copy link

dhowden commented Dec 22, 2015

package main

import (
    "encoding/json"
    "fmt"
    "io"
    "os"
)

type parts struct {
    Parts []part `json:parts`
}

type part struct {
    Type       string            `json:type`
    Args       map[string]string `json:args`
    Properties map[string]string `json:properties`
}

type binFunc func(interface{})

func main() {
    argsWithoutProg := os.Args[1:]

    fmt.Println(argsWithoutProg)

    f, err := os.Open(os.Args[1])
    if err != nil {
        panic(err)
    }

    var jsonParts parts

    dec := json.NewDecoder(f)
    for {
        //var m map[string]interface{}
        if err := dec.Decode(&jsonParts); err == io.EOF {
            break
        } else if err != nil {
            panic(err)
        }
        fmt.Println(jsonParts)
    }

    stuff := map[string]binFunc{}

    for _, p := range jsonParts.Parts {
        fmt.Println(p)
        stuff[p.Type] = func(newPart part) func(interface{}) {
            return func(x interface{}) {
                fmt.Println(newPart.Args)
                fmt.Println(newPart.Properties)
            }
        }(p)
    }

    for k, t := range stuff {
        fmt.Println(k)
        t(nil)
    }

}```

@dhowden
Copy link

dhowden commented Dec 22, 2015

package main

import (
    "encoding/json"
    "fmt"
    "io"
    "os"
)

type parts struct {
    Parts []part `json:parts`
}

type part struct {
    Type       string            `json:type`
    Args       map[string]string `json:args`
    Properties map[string]string `json:properties`
}

type binFunc func()

func main() {
    argsWithoutProg := os.Args[1:]

    fmt.Println(argsWithoutProg)

    f, err := os.Open(os.Args[1])
    if err != nil {
        panic(err)
    }

    var jsonParts parts

    dec := json.NewDecoder(f)
    for {
        //var m map[string]interface{}
        if err := dec.Decode(&jsonParts); err == io.EOF {
            break
        } else if err != nil {
            panic(err)
        }
        fmt.Println(jsonParts)
    }

    stuff := map[string]binFunc{}

    for _, p := range jsonParts.Parts {
        fmt.Println(p)
        p2 := p
        stuff[p.Type] = func() {
            fmt.Println(p2.Args)
            fmt.Println(p2.Properties)
        }
    }

    for k, t := range stuff {
        fmt.Println(k)
        t()
    }

}```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment