Skip to content

Commit

Permalink
improve fromJson utility and stop parsing strings into int,float,bool…
Browse files Browse the repository at this point in the history
…s. If value inside json is int,float, or bool treat it as such. If string contains number don't parse it keep it as string
  • Loading branch information
FL-K committed Jun 13, 2022
1 parent cfffdf1 commit 9ca4167
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,9 +418,25 @@ Usage of /var/folders/rd/2bkszcpx6xgcddpn7f3bhczn1m9fb7/T/go-build358407825/b001
goseeder - comma separated seeder names to run specific ones
```
## A note on `common` seeds
`common` is presented as one environment, but it is not such.
It is a special way of executing particular seeds always! in all environments, together with respective env seeds.
If you want to skip common executions, the means to do so are provided already in this documentation.
From cli with the flag
```
-gs-skip-common
```
and programmatically call `ShouldSkipCommon(true)`, like simple example:
```
err := goseeder.Execute(con, goseeder.ForEnv("test"), goseeder.ShouldSkipCommon(true))
```
## License
goseeder is released under the MIT Licence. See the bundled LICENSE file for details.
goseeder is released under the MIT Licence @kristijorgji. See the bundled LICENSE file for details.
Expand Down
34 changes: 29 additions & 5 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goseeder

import (
"fmt"
"log"
"reflect"
"runtime"
"strconv"
Expand Down Expand Up @@ -40,7 +41,7 @@ func findString(slice []string, val string) (int, bool) {
return -1, false
}

func prepareStatement(table string, row map[string]string) (strings.Builder, []interface{}) {
func prepareStatement(table string, row map[string]interface{}) (strings.Builder, []interface{}) {
var left strings.Builder
var right strings.Builder

Expand Down Expand Up @@ -71,14 +72,37 @@ func prepareStatement(table string, row map[string]string) (strings.Builder, []i
return left, args
}

func parseValue(value string) interface{} {
if parsed, err := strconv.ParseInt(value, 10, 64); err == nil {
func parseValue(value interface{}) interface{} {
if value == nil {
return value
}

switch v := value.(type) {
case bool:
return value.(bool)
case int:
return value.(int)
case int32:
return value.(int32)
case int64:
return value.(int64)
case float32:
return value.(float32)
case float64:
return value.(float64)
case string:
return value.(string)
default:
log.Printf("Don't know type : %v", v)
}

if parsed, err := strconv.ParseInt(value.(string), 10, 64); err == nil {
return parsed
}
if parsed, err := strconv.ParseFloat(value, 32); err == nil {
if parsed, err := strconv.ParseFloat(value.(string), 32); err == nil {
return parsed
}
if parsed, err := strconv.ParseBool(value); err == nil {
if parsed, err := strconv.ParseBool(value.(string)); err == nil {
return parsed
}

Expand Down
18 changes: 11 additions & 7 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestFindString(t *testing.T) {

func TestPrepareStatement(t *testing.T) {
table := "categories"
data := map[string]string{
data := map[string]interface{}{
"id": "100",
"name": "common",
}
Expand All @@ -37,21 +37,25 @@ func TestPrepareStatement(t *testing.T) {
require.Equal(
t,
[]interface{}{
int64(100),
"100",
"common"},
args,
)
}

var testCases = []struct {
name string
value string
value interface{}
expected interface{}
}{
{"bool", "true", true},
{"bool", "false", false},
{"int", "12", int64(12)},
{"float", "12.77", 12.770000457763672},
{"bool_string", "true", "true"},
{"bool_true", true, true},
{"bool_string", "false", "false"},
{"bool_false", false, false},
{"int_string", "12", "12"},
{"int", 12, int(12)},
{"float_string", "12.77", "12.77"},
{"float", 12.77, 12.77},
{"string", "justastring", "justastring"},
{"string_with_nr", "12justastring", "12justastring"},
}
Expand Down
2 changes: 1 addition & 1 deletion sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (s Seeder) FromJson(filename string) {
log.Fatal(err)
}

m := []map[string]string{}
m := []map[string]interface{}{}
err = json.Unmarshal(content, &m)
if err != nil {
panic(err)
Expand Down

0 comments on commit 9ca4167

Please sign in to comment.