Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AdaptCaseWithDecisionsDto): decisions not adapted #442

Merged
merged 6 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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/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
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
20 changes: 10 additions & 10 deletions utils/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,34 @@ func TestMapErr_Nil(t *testing.T) {
assert.Nil(t, result, "should return nil 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"})
}

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.Nilf(t, MapValues[int](nil, dummy), "should return nil 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"})
}

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")
}
Loading