Re: [go-nuts] JSON dynamic keys
That is 99% of what I'm after cheers. I need to be able to set the _doc bit to be any random string if possible as its the type name. `json:"_doc"` On Monday, December 3, 2018 at 3:53:34 PM UTC, Burak Serdar wrote: > > On Mon, Dec 3, 2018 at 8:27 AM Jeffrey Smith > > wrote: > > > > I'm trying to set mapping in elastic search in go and want to generate > something like this. > > > > { "mappings": { "_doc": { "properties": { "title": { "type": "text", > "store": true }, "date": { "type": "date", "store": true }, "content": { > "type": "text" } } } } } > > > > Is this close to what you're trying to do: > > https://play.golang.org/p/wYOwf6wUai7 > > > > > The _doc, title,date and content are all keys that will change depending > on what mapping I am trying to generate. > > > > I have a basic struct layout but cant work out how to generate the > proper JSON. > > > > type mappingData struct { > > Mappings struct { > > Doc struct { > > Properties struct { > > Elements []Fields > > } `json:"properties"` > > } `json:"_doc"` > > } `json:"mappings"` > > } > > > > > > type Fields struct { > > Type string `json:"type"` > > Store bool `json:"store"` > > } > > > > _doc will be from a string and I have a vector of structs that has > different fields in. > > > > -- > > You received this message because you are subscribed to the Google > Groups "golang-nuts" group. > > To unsubscribe from this group and stop receiving emails from it, send > an email to golang-nuts...@googlegroups.com . > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] JSON dynamic keys
I'm trying to set mapping in elastic search in go and want to generate something like this. { "mappings": { "_doc": { "properties": { "title": { "type": "text", "store": true }, "date": { "type": "date", "store": true }, "content": { "type": "text" } } } } } The _doc, title,date and content are all keys that will change depending on what mapping I am trying to generate. I have a basic struct layout but cant work out how to generate the proper JSON. type mappingData struct { Mappings struct { Doc struct { Properties struct { Elements []Fields } `json:"properties"` } `json:"_doc"` } `json:"mappings"` } type Fields struct { Type string `json:"type"` Store bool `json:"store"` } _doc will be from a string and I have a vector of structs that has different fields in. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] dynamic xml
I have a lot of XML documents that have sections in that contain tags that can be of any name but will contain type,minOccurs,maxOccurs and have a description inside it. For instance name,description,definition and id below. description number1 user graphCreate API A friendly name to identify the graph Detailed description of the graph Specify the graph definition. i.e how this graph should be built. graph identifier. Whats the best way to parse this. So far I have but can't work out how to dynamically generate the struct to stuff this into. Any suggestions? type topLevel struct { Description string `xml:"description"` Rights rights `xml:"rights"` Method []method `xml:"method"` } type method struct { Namestring `xml:"name,attr"` Description string `xml:"description"` Rights []right `xml:"rights"` } type right struct { Right string `xml:"right"` } type request struct { } type rights struct { PrivLevel string `xml:"privLevel"` } -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] Unmarshal multiple similar XML Files
Thanks a lot for that, simplifies my code massively :-) -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Unmarshal multiple similar XML Files
I'm trying to Unmarshal multiple XML files that have differing top level elements but the rest of the document is identical. ... ... I have tried adding multiple tags in the struct but it only ever uses the first one. type EspFlowCodeOperation struct { XMLName xml.Name `xml:"espFlowCodeOperation" xml:"espBPMOperation" xml:"espFlowCodeEvent" xml:"espAROperation"` ... } This only picks up the first element espFlowCodeOperation, I get this error. expected element type but have I was forced to create an individual struct for each and then try Unmarshaling it 4 times for each type checking the errors and falling through if it matches one of the xml documents I want as there are lots of other xml files mixed in that I expect to fail to parse. flowcode := EspFlowCodeOperation{} err = xml.Unmarshal([]byte(b), ) if err != nil { if strings.Contains(err.Error(), "espFlowCodeEvent") || strings.Contains(err.Error(), "espBPMOperation") || strings.Contains(err. Error(), "espAROperation") || strings.Contains(err.Error(), "espFlowCodeOperation") { } else { fmt.Println(err) } } else { . } There has to be a better way to do this, any suggestions ? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Serve large files
I'm trying to server up a file that can be very large so would like to try and stream the content back. package main import ( "bytes" "fmt" //"io" "io/ioutil" "log" "net/http" ) func main() { http.HandleFunc("/", serverFile) if err := http.ListenAndServe(":8085", nil); err != nil { log.Fatal(err) } } func serverFile(w http.ResponseWriter, r *http.Request) { streamPDFbytes, err := ioutil.ReadFile("./large.txt") if err != nil { fmt.Println(err) return } b := bytes.NewBuffer(streamPDFbytes) w.Header().Set("Content-Disposition", "attachment;filename=large.txt") //io.Copy(w, b) if _, err := b.WriteTo(w); err != nil { fmt.Fprintf(w, "%s", err) } } I tried using this but I believe ioutil.readFile actually calls ioutil.readAll so sticks the whole content of the file into a byte array. What am i doing wrong? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] AST to parse contents of Structs
I'm trying to use the ast and parse packages to find the fields in two separate structs one that will be called Inputs and the other Outputs. these can change but as an example i'm trying to parse these package main import ( "fmt" ) type Inputs struct { Name string `json:"name"` Age int`json:"age"` } type Outputs struct { Number float `json:"number"` } func main() { fmt.Println("Hello World!") } I have the following which can pick out the two structs but i'm now lost on how to loop through the fields to get name, type and commenty bit `json:"name"` at the end. package main import ( "fmt" "go/ast" "go/parser" "go/token" "log" ) func main() { fset := token.NewFileSet() file, err := parser.ParseFile(fset, "sample/main.go", nil, 0) if err != nil { log.Fatal(err) } ast.Walk(VisitorFunc(FindTypes), file) } type VisitorFunc func(n ast.Node) ast.Visitor func (f VisitorFunc) Visit(n ast.Node) ast.Visitor { return f(n) } func FindTypes(n ast.Node) ast.Visitor { switch n := n.(type) { case *ast.Package: return VisitorFunc(FindTypes) case *ast.File: return VisitorFunc(FindTypes) case *ast.GenDecl: if n.Tok == token.TYPE { return VisitorFunc(FindTypes) } case *ast.TypeSpec: if n.Name.Name == "Inputs" { //We have hit the inputs struct fmt.Println(n.Name.Name) } if n.Name.Name == "Outputs" { //We have hit the outputs struct fmt.Println(n.Name.Name) } } return nil } Any suggestions as to how to do it? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] http2 questions
I have been playing around with http2 in the latetst 1.7RC today and had a few questions I still had to import "golang.org/x/net/http2" and then set http2.ConfigureTransport to actually use http2 when creating a client. I was under the impression this was fixed or was I wrong? https://github.com/golang/go/issues/14391 tr := { TLSClientConfig: {InsecureSkipVerify: true}, } http2.ConfigureTransport(tr) client := {Transport: tr} I have the below code but it appears to leak connections whenever the 20 second timeout is hit after connecting to a client, is there anyway to clean up the connection for reuse rather than creating a new one until I hit my open file limit? func worker(id int, jobs <-chan int, results chan<- Results) { tr := { TLSClientConfig: {InsecureSkipVerify: true}, MaxIdleConnsPerHost: 2, } http2.ConfigureTransport(tr) PostClient := {Transport: tr, Timeout: time.Duration(20 * time.Second)} closeBody := false for { closeBody = false t0 := time.Now() req, err := http.NewRequest("GET", Url+strconv.Itoa(<-jobs), nil) if err != nil { results <- Results{0, err, "0"} continue } resp, err := PostClient.Do(req) if resp != nil { closeBody = true //defer resp.Body.Close() } t1 := time.Now() if err != nil { results <- Results{0, err, fmt.Sprintf("%v", t1.Sub(t0))} if closeBody == true { io.Copy(ioutil.Discard, resp.Body) resp.Body.Close() } continue } //discard the body so we can reuse the connections io.Copy(ioutil.Discard, resp.Body) //Close the body so we can resuse resp.Body.Close() results <- Results{resp.StatusCode, err, fmt.Sprintf("%v", t1.Sub(t0))} } } Also does anyone have any recommendations with http2 should I be creating one connection per server and fire all requests down that (not even sure how I would do this in golang) or just create a global pool and allow the http client deal with it? -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.