Skip to content

Commit

Permalink
fix(AdaptCaseWithDecisionsDto): decisions not adapted (#442)
Browse files Browse the repository at this point in the history
* fix(AdaptCaseWithDecisionsDto): decisions not adapted

* refactor(MapMap): rename to MapValues

* refactor(utils): remove nil special handling in Map

* fix tests

* fix order in test params

* refactor(tests): use assert.Empty

---------

Co-authored-by: Pascal Delange <[email protected]>
  • Loading branch information
balzdur and Pascal-Delange authored Jan 18, 2024
1 parent dee2a2c commit 5bb6a05
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 75 deletions.
28 changes: 7 additions & 21 deletions dto/case_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/checkmarble/marble-backend/models"
"github.com/checkmarble/marble-backend/utils"
)

type APICase struct {
Expand All @@ -25,39 +26,24 @@ type APICaseWithDecisions struct {
}

func AdaptCaseDto(c models.Case) APICase {
apiCase := APICase{
return APICase{
Id: c.Id,
Contributors: make([]APICaseContributor, len(c.Contributors)),
Contributors: utils.Map(c.Contributors, NewAPICaseContributor),
CreatedAt: c.CreatedAt,
DecisionsCount: c.DecisionsCount,
Events: make([]APICaseEvent, len(c.Events)),
Events: utils.Map(c.Events, NewAPICaseEvent),
InboxId: c.InboxId,
Name: c.Name,
Status: string(c.Status),
Tags: make([]APICaseTag, len(c.Tags)),
Files: make([]APICaseFile, len(c.Files)),
Tags: utils.Map(c.Tags, NewAPICaseTag),
Files: utils.Map(c.Files, NewAPICaseFile),
}

for i, event := range c.Events {
apiCase.Events[i] = NewAPICaseEvent(event)
}
for i, contributor := range c.Contributors {
apiCase.Contributors[i] = NewAPICaseContributor(contributor)
}
for i, tag := range c.Tags {
apiCase.Tags[i] = NewAPICaseTag(tag)
}
for i, file := range c.Files {
apiCase.Files[i] = NewAPICaseFile(file)
}

return apiCase
}

func AdaptCaseWithDecisionsDto(c models.Case) APICaseWithDecisions {
return APICaseWithDecisions{
APICase: AdaptCaseDto(c),
Decisions: make([]APIDecision, len(c.Decisions)),
Decisions: utils.Map(c.Decisions, NewAPIDecision),
}
}

Expand Down
6 changes: 3 additions & 3 deletions dto/data_model_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func AdaptTableDto(table models.Table) Table {
return Table{
Name: string(table.Name),
ID: table.ID,
Fields: utils.MapMap(table.Fields, func(field models.Field) Field {
Fields: utils.MapValues(table.Fields, func(field models.Field) Field {
return Field{
ID: field.ID,
DataType: field.DataType.String(),
Expand All @@ -87,7 +87,7 @@ func AdaptTableDto(table models.Table) Table {
Values: field.Values,
}
}),
LinksToSingle: utils.MapMap(table.LinksToSingle, func(linkToSingle models.LinkToSingle) LinkToSingle {
LinksToSingle: utils.MapValues(table.LinksToSingle, func(linkToSingle models.LinkToSingle) LinkToSingle {
return LinkToSingle{
LinkedTableName: linkToSingle.LinkedTableName,
ParentFieldName: linkToSingle.ParentFieldName,
Expand All @@ -102,6 +102,6 @@ func AdaptDataModelDto(dataModel models.DataModel) DataModel {
return DataModel{
Version: dataModel.Version,
Status: dataModel.Status.String(),
Tables: utils.MapMap(dataModel.Tables, AdaptTableDto),
Tables: utils.MapValues(dataModel.Tables, AdaptTableDto),
}
}
4 changes: 2 additions & 2 deletions dto/dto_ast_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func AdaptNodeDto(node ast.Node) (NodeDto, error) {
return NodeDto{}, err
}

namedChildrenDto, err := utils.MapMapErr(node.NamedChildren, AdaptNodeDto)
namedChildrenDto, err := utils.MapValuesErr(node.NamedChildren, AdaptNodeDto)
if err != nil {
return NodeDto{}, err
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func AdaptASTNode(dto NodeDto) (ast.Node, error) {
return ast.Node{}, err
}

namedChildren, err := utils.MapMapErr(dto.NamedChildren, AdaptASTNode)
namedChildren, err := utils.MapValuesErr(dto.NamedChildren, AdaptASTNode)
if err != nil {
return ast.Node{}, err
}
Expand Down
12 changes: 6 additions & 6 deletions dto/dto_ast_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@ func TestAdaptNodeDto(t *testing.T) {

assert.NoError(t, err)
assert.Equal(t,
dto,
NodeDto{
FuncName: ">",
Children: []NodeDto{{Constant: 1}},
NamedChildren: map[string]NodeDto{"named": {Constant: 2}},
Children: []NodeDto{{Constant: 1, Children: []NodeDto{}, NamedChildren: map[string]NodeDto{}}},
NamedChildren: map[string]NodeDto{"named": {Constant: 2, Children: []NodeDto{}, NamedChildren: map[string]NodeDto{}}},
},
dto,
)
}

func TestAdaptASTNode(t *testing.T) {

dto := NodeDto{
FuncName: "+",
Children: []NodeDto{{Constant: 1}},
Children: []NodeDto{{Constant: 1, Children: []NodeDto{}, NamedChildren: map[string]NodeDto{}}},
NamedChildren: map[string]NodeDto{
"named": {Constant: 2},
"named": {Constant: 2, Children: []NodeDto{}, NamedChildren: map[string]NodeDto{}},
},
}

node, err := AdaptASTNode(dto)

assert.NoError(t, err)
assert.Equal(t,
node,
ast.Node{Function: ast.FUNC_ADD}.
AddChild(ast.NewNodeConstant(1)).
AddNamedChild("named", ast.NewNodeConstant(2)),
node,
)
}

Expand Down
2 changes: 1 addition & 1 deletion dto/node_evaluation_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ func AdaptNodeEvaluationDto(evaluation ast.NodeEvaluation) NodeEvaluationDto {
ReturnValue: evaluation.ReturnValue,
Errors: utils.Map(evaluation.Errors, AdaptEvaluationErrorDto),
Children: utils.Map(evaluation.Children, AdaptNodeEvaluationDto),
NamedChildren: utils.MapMap(evaluation.NamedChildren, AdaptNodeEvaluationDto),
NamedChildren: utils.MapValues(evaluation.NamedChildren, AdaptNodeEvaluationDto),
}
}
2 changes: 1 addition & 1 deletion dto/node_evaluation_dto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ func TestAdaptAdaptNodeEvaluationDto_noevaluation(t *testing.T) {
result := encodeDecodeNodeEvaluation(t, ast.NodeEvaluation{
Errors: nil,
})
assert.Nil(t, result.Errors)
assert.Empty(t, result.Errors)
}
2 changes: 1 addition & 1 deletion dto/scenario_validation_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func AdaptScenarioValidationDto(s models.ScenarioValidation) ScenarioValidationD
},
Rules: rulesValidationDto{
Errors: utils.Map(s.Rules.Errors, AdaptScenarioValidationErrorDto),
Rules: utils.MapMap(s.Rules.Rules, func(ruleValidation models.RuleValidation) ruleValidationDto {
Rules: utils.MapValues(s.Rules.Rules, func(ruleValidation models.RuleValidation) ruleValidationDto {
return ruleValidationDto{
Errors: utils.Map(ruleValidation.Errors, AdaptScenarioValidationErrorDto),
RuleEvaluation: AdaptNodeEvaluationDto(ruleValidation.RuleEvaluation),
Expand Down
2 changes: 1 addition & 1 deletion models/ast/ast_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (f Function) DebugString() string {
// ======= Constant =======

func NewNodeConstant(value any) Node {
return Node{Function: FUNC_CONSTANT, Constant: value}
return Node{Function: FUNC_CONSTANT, Constant: value, Children: []Node{}, NamedChildren: map[string]Node{}}
}

// ======= DbAccess =======
Expand Down
4 changes: 2 additions & 2 deletions usecases/ast_eval/evaluate_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func EvaluateAst(ctx context.Context, environment AstEvaluationEnvironment, node
// eval each child
evaluation := ast.NodeEvaluation{
Children: utils.Map(node.Children, evalChild),
NamedChildren: utils.MapMap(node.NamedChildren, evalChild),
NamedChildren: utils.MapValues(node.NamedChildren, evalChild),
}

if childEvaluationFail {
Expand All @@ -46,7 +46,7 @@ func EvaluateAst(ctx context.Context, environment AstEvaluationEnvironment, node
getReturnValue := func(e ast.NodeEvaluation) any { return e.ReturnValue }
arguments := ast.Arguments{
Args: utils.Map(evaluation.Children, getReturnValue),
NamedArgs: utils.MapMap(evaluation.NamedChildren, getReturnValue),
NamedArgs: utils.MapValues(evaluation.NamedChildren, getReturnValue),
}

evaluator, err := environment.GetEvaluator(node.Function)
Expand Down
26 changes: 6 additions & 20 deletions utils/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ package utils
// The rational of why the Go team rejects it is explained in this wonderfull stack overflow answer.
// https://stackoverflow.com/questions/71624828/is-there-a-way-to-map-an-array-of-objects-in-golang

// MapErr returns a new slice with the same length as src, but with values transformed by f
// if src is nil, returns nil
// Map returns a new slice with the same length as src, but with values transformed by f
func Map[T, U any](src []T, f func(T) U) []U {
if src == nil {
return nil
}
us := make([]U, len(src))
for i := range src {
us[i] = f(src[i])
Expand All @@ -19,40 +15,30 @@ func Map[T, U any](src []T, f func(T) U) []U {

// MapErr returns a new slice with the same length as src, but with values transformed by f
// If f returns an error, the function stops and returns the error.
// if src is nil, returns nil
func MapErr[T, U any](src []T, f func(T) (U, error)) ([]U, error) {
if src == nil {
return nil, nil
}
us := make([]U, len(src))
for i := range src {
var err error
us[i], err = f(src[i])
if err != nil {
return us, err
return nil, err
}
}
return us, nil
}

// MapMap return a new map with the same keys as src, but with values transformed by f
func MapMap[Key comparable, T any, U any](src map[Key]T, f func(T) U) map[Key]U {
if src == nil {
return nil
}
// MapValues return a new map with the same keys as src, but with values transformed by f
func MapValues[Key comparable, T any, U any](src map[Key]T, f func(T) U) map[Key]U {
result := make(map[Key]U, len(src))
for key, value := range src {
result[key] = f(value)
}
return result
}

// MapMapErr return a new map with the same keys as src, but with values transformed by f
// MapValuesErr return a new map with the same keys as src, but with values transformed by f
// If f returns an error, the function stops and returns the error.
func MapMapErr[Key comparable, T any, U any](src map[Key]T, f func(T) (U, error)) (map[Key]U, error) {
if src == nil {
return nil, nil
}
func MapValuesErr[Key comparable, T any, U any](src map[Key]T, f func(T) (U, error)) (map[Key]U, error) {
result := make(map[Key]U, len(src))
for key, value := range src {
var err error
Expand Down
34 changes: 17 additions & 17 deletions utils/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func TestMap(t *testing.T) {
result := Map(values, func(v int) string {
return fmt.Sprintf("%d", v)
})
assert.Equal(t, result, []string{"1", "2"})
assert.Equal(t, []string{"1", "2"}, result)
}

func TestMap_Nil(t *testing.T) {
assert.Nilf(t, Map(nil, dummy), "should return nil when src is nil")
assert.Empty(t, Map(nil, dummy), "should return empty slice when src is nil")
}

func TestMapErr(t *testing.T) {
Expand All @@ -34,44 +34,44 @@ func TestMapErr(t *testing.T) {
}
return "2", errorForTesting
})
assert.Equal(t, result, []string{"1", "2", ""})
assert.Empty(t, result)
assert.ErrorIs(t, err, errorForTesting)
}

func TestMapErr_Nil(t *testing.T) {
result, err := MapErr(nil, dummyErr)
assert.NoError(t, err)
assert.Nil(t, result, "should return nil when src is nil")
assert.Empty(t, result, "should return empty slice when src is nil")
}

func TestMapMap(t *testing.T) {
func TestMapValues(t *testing.T) {

values := map[string]int{"a": 1, "b": 2, "c": 3}
result := MapMap(values, func(v int) string {
result := MapValues(values, func(v int) string {
return fmt.Sprintf("%d", v)
})
assert.Equal(t, result, map[string]string{"a": "1", "b": "2", "c": "3"})
assert.Equal(t, map[string]string{"a": "1", "b": "2", "c": "3"}, result)
}

func TestMapMap_Nil(t *testing.T) {
assert.Nilf(t, MapMap[int](nil, dummy), "should return nil when src is nil")
func TestMapValues_Nil(t *testing.T) {
assert.Empty(t, MapValues[int](nil, dummy), "should return empty map when src is nil")
}

func TestMapMapErr(t *testing.T) {
func TestMapValuesErr(t *testing.T) {

values := map[string]int{"a": 1, "b": 2, "c": 3}
result, err := MapMapErr(values, func(v int) (string, error) {
result, err := MapValuesErr(values, func(v int) (string, error) {
return fmt.Sprintf("%d", v), nil
})
assert.NoError(t, err)
assert.Equal(t, result, map[string]string{"a": "1", "b": "2", "c": "3"})
assert.Equal(t, map[string]string{"a": "1", "b": "2", "c": "3"}, result)
}

func TestMapMapErr_WithError(t *testing.T) {
func TestMapValuesErr_WithError(t *testing.T) {
errorForTesting := errors.New("testing error")

values := map[string]int{"a": 1, "b": 2, "c": 3}
result, err := MapMapErr(values, func(v int) (string, error) {
result, err := MapValuesErr(values, func(v int) (string, error) {
if v == 1 {
return "1", nil
}
Expand All @@ -81,8 +81,8 @@ func TestMapMapErr_WithError(t *testing.T) {
assert.ErrorIs(t, err, errorForTesting)
}

func TestMapMapErr_Nil(t *testing.T) {
result, err := MapMapErr[int](nil, dummyErr)
func TestMapValuesErr_Nil(t *testing.T) {
result, err := MapValuesErr[int](nil, dummyErr)
assert.NoError(t, err)
assert.Nilf(t, result, "should return nil when src is nil")
assert.Empty(t, result, "should return empty map when src is nil")
}

0 comments on commit 5bb6a05

Please sign in to comment.