-
-
Save viiftw/4a2987856ed278bbb042a2f0ac7cca2f to your computer and use it in GitHub Desktop.
Revisions
-
viiftw renamed this gist
Feb 17, 2022 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
viiftw revised this gist
Feb 17, 2022 . 1 changed file with 10 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,6 +6,16 @@ import ( "reflect" ) // nicest way to walk through on an unknown type and put data into // the Result is an array of JSON content // The Each method gets a type what it can use to translate this JSONs and basically returns the slice of translated data // https://gist.github.com/olivere/a37f828d2018fb4a74cec400de4ba931#file-example_test-go func main() { Example() } type Article struct { Title string `json:"title"` Content string `json:"content"` -
viiftw revised this gist
Feb 17, 2022 . No changes.There are no files selected for viewing
-
olivere revised this gist
May 3, 2018 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ type Article struct { } type Result struct { Hits []string } func Example() { @@ -21,10 +21,10 @@ func Example() { "content": "%s article's content" }` var result = Result{ Hits: []string{ fmt.Sprintf(formatString, "First", "First"), fmt.Sprintf(formatString, "Second", "Second"), fmt.Sprintf(formatString, "Third", "Third"), }, } @@ -39,11 +39,11 @@ func Example() { // Title: Third, Content: Third article's content } func (r Result) Each(typ reflect.Type) []interface{} { var slice []interface{} for _, hit := range r.Hits { v := reflect.New(typ).Elem() if err := json.Unmarshal([]byte(hit), v.Addr().Interface()); err == nil { slice = append(slice, v.Interface()) } } -
olivere created this gist
May 3, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ package main import ( "encoding/json" "fmt" "reflect" ) type Article struct { Title string `json:"title"` Content string `json:"content"` } type Result struct { Hits [][]byte } func Example() { const formatString = `{ "title": %q, "content": "%s article's content" }` var result = Result{ Hits: [][]byte{ []byte(fmt.Sprintf(formatString, "First", "First")), []byte(fmt.Sprintf(formatString, "Second", "Second")), []byte(fmt.Sprintf(formatString, "Third", "Third")), }, } articlesContainer := result.Each(reflect.TypeOf(Article{})) for _, article := range articlesContainer { a := article.(Article) fmt.Printf("Title: %s, Content: %s\n", a.Title, a.Content) } // Output: // Title: First, Content: First article's content // Title: Second, Content: Second article's content // Title: Third, Content: Third article's content } func (r *Result) Each(typ reflect.Type) []interface{} { var slice []interface{} for _, hit := range r.Hits { v := reflect.New(typ).Elem() if err := json.Unmarshal(hit, v.Addr().Interface()); err == nil { slice = append(slice, v.Interface()) } } return slice }