@@ -397,6 +397,27 @@ func TestFilter(t *testing.T) {
397397 },
398398 v )
399399 })
400+
401+ t .Run ("should filter by value" , func (t * testing.T ) {
402+ instance := mapify.Instance {
403+ Filter : func (path string , e mapify.Element ) bool {
404+ return e .String () == "keep it"
405+ },
406+ }
407+ // when
408+ v := instance .MapAny (
409+ struct { Field1 , Field2 string }{
410+ Field1 : "keep it" ,
411+ Field2 : "omit this" ,
412+ },
413+ )
414+ // then
415+ assert .Equal (t ,
416+ map [string ]interface {}{
417+ "Field1" : "keep it" ,
418+ },
419+ v )
420+ })
400421}
401422
402423func TestRename (t * testing.T ) {
@@ -420,3 +441,55 @@ func TestRename(t *testing.T) {
420441 v )
421442 })
422443}
444+
445+ func TestMapValue (t * testing.T ) {
446+ mappedValue := "str"
447+
448+ t .Run ("should map struct field" , func (t * testing.T ) {
449+ instance := mapify.Instance {
450+ MapValue : func (path string , e mapify.Element ) interface {} {
451+ if e .Name () == "Field1" {
452+ return mappedValue
453+ }
454+
455+ return e .Interface ()
456+ },
457+ }
458+ s := struct { Field1 , Field2 int }{
459+ Field1 : 1 , Field2 : 2 ,
460+ }
461+ // when
462+ v := instance .MapAny (s )
463+ // then
464+ assert .Equal (t ,
465+ map [string ]interface {}{
466+ "Field1" : mappedValue ,
467+ "Field2" : s .Field2 ,
468+ },
469+ v )
470+ })
471+
472+ t .Run ("should map struct field by path" , func (t * testing.T ) {
473+ instance := mapify.Instance {
474+ MapValue : func (path string , e mapify.Element ) interface {} {
475+ if path == ".Field1" {
476+ return mappedValue
477+ }
478+
479+ return e .Interface ()
480+ },
481+ }
482+ s := struct { Field1 , Field2 int }{
483+ Field1 : 1 , Field2 : 2 ,
484+ }
485+ // when
486+ v := instance .MapAny (s )
487+ // then
488+ assert .Equal (t ,
489+ map [string ]interface {}{
490+ "Field1" : mappedValue ,
491+ "Field2" : s .Field2 ,
492+ },
493+ v )
494+ })
495+ }
0 commit comments