~romuloalves/go-dsv

36588f9be3cef71660ebf876a000f9741834d813 — romuloalves 10 months ago ce8410e
Enables parser of dsv to struct to handle less fields than the dsv contains
3 files changed, 37 insertions(+), 6 deletions(-)

M Gopkg.lock
M dsv.go
M dsv_test.go
M Gopkg.lock => Gopkg.lock +14 -1
@@ 2,38 2,51 @@


[[projects]]
  digest = "1:56c130d885a4aacae1dd9c7b71cfe39912c7ebc1ff7d2b46083c8812996dc43b"
  name = "github.com/davecgh/go-spew"
  packages = ["spew"]
  pruneopts = ""
  revision = "346938d642f2ec3594ed81d874461961cd0faa76"
  version = "v1.1.0"

[[projects]]
  digest = "1:256484dbbcd271f9ecebc6795b2df8cad4c458dd0f5fd82a8c2fa0c29f233411"
  name = "github.com/pmezard/go-difflib"
  packages = ["difflib"]
  pruneopts = ""
  revision = "792786c7400a136282c1664665ae0a8db921c6c2"
  version = "v1.0.0"

[[projects]]
  branch = "master"
  digest = "1:ed7ac53c7d59041f27964d3f04e021b45ecb5f23c842c84d778a7f1fb67e2ce9"
  name = "github.com/stretchr/objx"
  packages = ["."]
  pruneopts = ""
  revision = "1a9d0bb9f541897e62256577b352fdbc1fb4fd94"

[[projects]]
  digest = "1:3926a4ec9a4ff1a072458451aa2d9b98acd059a45b38f7335d31e06c3d6a0159"
  name = "github.com/stretchr/testify"
  packages = ["assert"]
  pruneopts = ""
  revision = "69483b4bd14f5845b5a1e55bca19e954e827f1d0"
  version = "v1.1.4"

[[projects]]
  branch = "master"
  digest = "1:f88352c65d9b72a5e2ef705e6f716bc9beec0f08c6ddb61968626ce3667e6a2a"
  name = "github.com/tj/assert"
  packages = ["."]
  pruneopts = ""
  revision = "748ebc778a698c0df0eb9855caf3ef677ed99471"

[solve-meta]
  analyzer-name = "dep"
  analyzer-version = 1
  inputs-digest = "88621538508f6e32114336febe7381b586f297576c005d629499c1fbde5ce6e7"
  input-imports = [
    "github.com/stretchr/objx",
    "github.com/tj/assert",
  ]
  solver-name = "gps-cdcl"
  solver-version = 1

M dsv.go => dsv.go +0 -5
@@ 65,11 65,6 @@ func ToStruct(dsv string, i interface{}, separator string) (interface{}, error) 
	splittedData := strings.Split(dsv, separator)
	splittedDataLength := len(splittedData)

	// Validating the fields and the quantity of data in the dsv
	if splittedDataLength != fieldsLength {
		return nil, errors.New("Struct fields and dsv data have different lengths")
	}

	// Put data in the fields
	for dataIndex := 0; dataIndex < splittedDataLength; dataIndex++ {
		for fieldIndex := 0; fieldIndex < fieldsLength; fieldIndex++ {

M dsv_test.go => dsv_test.go +23 -0
@@ 127,3 127,26 @@ func TestDsvToStruct(t *testing.T) {

	assert.EqualValues(t, assertStruct, data, "Assert struct and returned struct from dsv should be equals")
}

func TestDsvToStructWithoutAllFields(t *testing.T) {
	type myStruct struct {
		FieldOne  string  `dsv:"0"`
		FieldTwo  int     `dsv:"1"`
		FieldFour float32 `dsv:"3"`
	}

	assertStruct := &myStruct{
		FieldOne:  "fieldOneData",
		FieldTwo:  123,
		FieldFour: 43.34,
	}

	dsv := "fieldOneData|123|false|43.34"

	data, err := ToStruct(dsv, &myStruct{}, "|")
	if err != nil {
		t.Fatal(err)
	}

	assert.EqualValues(t, assertStruct, data, "Assert struct and returned struct from dsv should be equals")
}