Skip to content

Commit

Permalink
slice params value not set. added test (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobiverse authored Nov 21, 2024
1 parent 7b3d955 commit f840923
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
5 changes: 5 additions & 0 deletions ParamList.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ func AnyToParams(v any) ParamList {

// JSONToParams - load params from JSON
func JSONToParams(buf []byte) ParamList {

// to map
m := map[string]any{}
if err := json.Unmarshal(buf, &m); err != nil {
log.Printf("JSONToParams: %s", err)
return nil
}

// to filtered/clean map
params := mapToParams(m)
params.Walk(func(p *Param) {
Expand All @@ -57,15 +59,18 @@ func JSONToParams(buf []byte) ParamList {
func mapToParams(m map[string]any) ParamList {
var params ParamList
for mKey, mVal := range m {

p := NewParam(mKey)

switch v := mVal.(type) {
case map[string]any:
p.Type = StructParam
p.Params = mapToParams(v)
p.SetValue(v)
case []any:
p.Type = SliceParam
p.Params = sliceToParams(v)
p.SetValue(v)
case *Image:
imgVal, err := processImage(v)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/bobiverse/docxplate

go 1.23.1
go 1.23.3

require golang.org/x/text v0.18.0
require golang.org/x/text v0.20.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
12 changes: 9 additions & 3 deletions t.docx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ func TestPlaceholders(t *testing.T) {
t.Fatalf("[%s] ExportDocx: %s", inType, err)
}

lines := strings.Split(plaintext, "\n")
if len(lines) > 900 { // some loops have limit at 1000k iterations, so here is simple check
t.Fatalf("Plaintext too long: \n%s", tdoc.Plaintext())

}

// Check for "must remove" text
removedTexts := []string{
":empty",
Expand Down Expand Up @@ -243,13 +249,13 @@ func TestDepthStructToParams(t *testing.T) {
Friends: []*User{
{Name: "Bob", Age: 28, Motto: "Be\nbrave,\nbe\nbold.", Friends: []*User{
{Name: "Cecilia", Age: 29, Motto: "Chase\nyour\ndreams."},
{Name: "Sun", Age: 999, Motto: ""},
{Name: "Tony", Age: 999, Motto: ""},
{Name: "Sun (empty motto)", Age: 999, Motto: ""},
{Name: "Tony (empty motto)", Age: 999, Motto: ""},
}},
{Name: "Den", Age: 30, Motto: "Don't give up.", Friends: []*User{
{Name: "Ben", Age: 999, Motto: "Be brave, be bold as twice as Bob."},
{Name: "Edgar", Age: 31, Motto: "Embrace the moment."},
{Name: "Jouny", Age: 999, Motto: ""},
{Name: "Jouny (empty motto)", Age: 999, Motto: ""},
{Name: "Carrzy", Age: 999, Motto: "Chase your dreamzzz"},
}},
},
Expand Down
2 changes: 2 additions & 0 deletions t.params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestAnyToParamsMapStringAny(t *testing.T) {
"Name": "Bob",
"Age": uint(28),
"FavColor": dummyTestType(0xF00),
"Numbers": []int{1, 2, 3, 4, 5},
}

outParams := AnyToParams(inParams)
Expand All @@ -44,6 +45,7 @@ func TestAnyToParamsMapStringAny(t *testing.T) {

// check if all params exists
for k, v := range inParams {
// log.Printf("[%s] ==> [%v]", k, v)
val := outParams.Get(k)
if val == nil {
t.Fatalf("param `%s` not found", k)
Expand Down

0 comments on commit f840923

Please sign in to comment.