Quellcode durchsuchen

first erro details replace message of error

EUGENIO SOUZA CARVALHO vor 4 Jahren
Ursprung
Commit
9be0b8aba8
3 geänderte Dateien mit 135 neuen und 22 gelöschten Zeilen
  1. 45 18
      common/models.go
  2. 7 1
      translate/got/middleware_patch.go
  3. 83 3
      translate/got/schemas.go

+ 45 - 18
common/models.go

@@ -364,14 +364,24 @@ func RequestParams(args string, params map[string]*Parameter) func(ctx context.C
 
 			if param.Required && (value == "" || value == nil) {
 				invalidArgument := errs.InvalidArgument()
-				invalidArgument.Message = fmt.Sprintf("ParamRequired:'%s'", param.ID)
+				invalidArgument.Message = fmt.Sprintf(
+					"ParamRequired: param '%s' in '%s'",
+					param.ID,
+					param.Location,
+				)
 				return nil, invalidArgument
 			}
 
 			if param.ConvertTo != "" {
 				if value, err = convertValueByType(param.ConvertTo, value); err != nil {
 					invalidArgument := errs.InvalidArgument()
-					invalidArgument.Message = fmt.Sprintf("TypeConversionError:'%v'. Waiting a %s ", value, param.ConvertTo)
+					invalidArgument.Message = fmt.Sprintf(
+						"ParamTypeConversionError: param '%s' in '%s' with value '%v'. Waiting a %s ", 
+						param.ID,
+						param.Location,
+						value, 
+						param.ConvertTo,
+					)
 					return nil, invalidArgument
 				}
 			}
@@ -379,10 +389,8 @@ func RequestParams(args string, params map[string]*Parameter) func(ctx context.C
 			if param.Validation != nil {
 				for validator, args := range param.Validation {
 					if fn, found := validationParamFunctions[validator]; found {
-
 						ctx.Application().Logger().Info(fmt.Sprintf("validadete[%s][%s][%v]", validator, args, value))
-
-						if err = fn(value, args); err != nil {
+						if err = fn(param, value, args); err != nil {
 							return nil, err
 						}
 					}
@@ -406,8 +414,8 @@ var (
 		"number":   stringToFloat,
 	}
 
-	validationParamFunctions = map[string]func(interface{}, interface{}) *errs.Error{
-		"min": func(value interface{}, minString interface{}) *errs.Error {
+	validationParamFunctions = map[string]func(*Parameter,interface{}, interface{}) *errs.Error{
+		"min": func(param *Parameter, value interface{}, minString interface{}) *errs.Error {
 			var input float64
 
 			if v, ok := value.(int64); ok {
@@ -418,18 +426,26 @@ var (
 				input = float64(len(v))
 			} else {
 				invalidArgument := errs.InvalidArgument()
-				invalidArgument.Message = fmt.Sprintf("ValueRestriction: mim validation requires (int,float,string)")
+				invalidArgument.Message = fmt.Sprintf(
+					"[%s] ValueRestriction: mim validation requires (int,float,string)",
+					param.ID,
+				)
 				return invalidArgument
 			}
 
 			if min, convert := minString.(float64); !convert || input < min {
 				invalidArgument := errs.InvalidArgument()
-				invalidArgument.Message = fmt.Sprintf("ValueRestriction: value > %v. Received (%v)", minString, value)
+				invalidArgument.Message = fmt.Sprintf(
+					"[%s] ValueRestriction: value > %v. Received (%v)",
+					param.ID,
+					minString,
+					value,
+				)
 				return invalidArgument
 			}
 			return nil
 		},
-		"max": func(value interface{}, maxString interface{}) *errs.Error {
+		"max": func(param *Parameter, value interface{}, maxString interface{}) *errs.Error {
 
 			var input float64
 
@@ -441,19 +457,27 @@ var (
 				input = float64(len(v))
 			} else {
 				invalidArgument := errs.InvalidArgument()
-				invalidArgument.Message = fmt.Sprintf("ValueRestriction: mim validation requires (int,float,string)")
+				invalidArgument.Message = fmt.Sprintf(
+					"[%s] ValueRestriction: mim validation requires (int,float,string)",
+					param.ID,
+				)
 				return invalidArgument
 			}
 
 			if max, convert := maxString.(float64); !convert || input > max {
 				invalidArgument := errs.InvalidArgument()
-				invalidArgument.Message = fmt.Sprintf("ValueRestriction: value < %v. Received (%v)", maxString, value)
+				invalidArgument.Message = fmt.Sprintf(
+					"[%s] ValueRestriction: value < %v. Received (%v)",
+					param.ID,
+					maxString,
+					value,
+				)
 				return invalidArgument
 			}
 
 			return nil
 		},
-		"accept": func(input interface{}, accept interface{}) *errs.Error {
+		"accept": func(param *Parameter, input interface{}, accept interface{}) *errs.Error {
 			var (
 				acceptValues       = accept.([]interface{})
 				acceptValuesString = []string{}
@@ -469,13 +493,14 @@ var (
 
 			invalidArgument := errs.InvalidArgument()
 			invalidArgument.Message = fmt.Sprintf(
-				"ValueRestriction: '%s' isn't accept. Accept [%s]",
+				"[%s] ValueRestriction: '%s' isn't accept. Accept [%s]",
+				param.ID,
 				value,
 				strings.Join(acceptValuesString, ","),
 			)
 			return invalidArgument
 		},
-		"reject": func(input interface{}, reject interface{}) *errs.Error {
+		"reject": func(param *Parameter, input interface{}, reject interface{}) *errs.Error {
 			var (
 				rejectValues = reject.([]interface{})
 				value        = fmt.Sprintf("%v", input)
@@ -485,7 +510,8 @@ var (
 				if value == rejectValue.(string) {
 					invalidArgument := errs.InvalidArgument()
 					invalidArgument.Message = fmt.Sprintf(
-						"ValueRestriction: '%s' isn't accept",
+						"[%s] ValueRestriction: '%s' isn't accept",
+						param.ID,
 						value,
 					)
 					return invalidArgument
@@ -493,7 +519,7 @@ var (
 			}
 			return nil
 		},
-		"regex": func(input interface{}, regex interface{}) *errs.Error {
+		"regex": func(param *Parameter, input interface{}, regex interface{}) *errs.Error {
 			var (
 				regexString = regex.(string)
 				value       = input.(string)
@@ -505,7 +531,8 @@ var (
 
 				invalidArgument := errs.InvalidArgument()
 				invalidArgument.Message = fmt.Sprintf(
-					"ValueRestriction: '%s' isn't accept",
+					"[%s] ValueRestriction: '%s' isn't accept",
+					param.ID,
 					value,
 				)
 

+ 7 - 1
translate/got/middleware_patch.go

@@ -32,7 +32,10 @@ func init() {
 	user := values.Get("$user.ref").(*models.UserReference)
 	entity.UpdatedBy = user
 	values.Set("entity", entity)
-	{{if .entityAlias }} values.Set("{{.entityAlias}}", entity) {{end}}
+	{{if .entityAlias }} 
+	values.Set("{{.entityAlias}}", entity) 
+	values.Set("{{.entityAlias}}.patch", patchs)
+	{{end}}
 	
 	{{if .preconditions}}
 		if _, err = executeAction(
@@ -74,6 +77,9 @@ func init() {
 
 		return
 	}
+
+	values.Set("{{.entityAlias}}.after.patch", filter.Entity)
+
 	{{if .hasUpdateRelation}} // Cria uma thread que executa a atualizaca das referências.
 	go func() {
 		UpdateRelation{{.entity}}(filter)

+ 83 - 3
translate/got/schemas.go

@@ -66,6 +66,10 @@ func GenSchemas(p *Project) error {
 		return err
 	}
 
+	if err = GenEntityHelpers(p); err != nil {
+		return err
+	}
+
 	for _, entity := range entities {
 
 		entityInfo = p.ResponseEntity(entity.ID)
@@ -398,9 +402,11 @@ func GenSchemas(p *Project) error {
 			f.Func().Params(
 				G.Id("t").Op("*").Id(entityPatchName),
 			).Id("Patch").Params().Op("*").Qual(BSON, "A").Block(
-				G.Id("updt").Op(":=").Qual(BSON, "A").Values(
-					G.Qual(BSON, "M").Values(G.Lit("$set").Id(": t.Set")),
-				).Line(),
+				G.Id("updt").Op(":=").Qual(BSON, "A").Values().Line(),
+				G.Id(`if t.Set != nil {
+					updt = append(updt, bson.M{"$set": t.Set})
+				}`).Line(),
+
 				G.Do(func(y *G.Statement) {
 					if !hasAddOrRemove {
 						return
@@ -793,3 +799,77 @@ func generateIndexsEntity(file *G.File, entity *Entity) {
 	}
 
 }
+
+func GenEntityHelpers(project  *Project) error {
+
+	file := G.NewFile("models")
+	entities, err := Schemas(project)
+	if err != nil {
+		return err
+	}
+
+	file.Add(G.Id(`
+	import(
+		"git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
+		context "github.com/kataras/iris/v12/context"
+		"go.mongodb.org/mongo-driver/bson"
+		"go.mongodb.org/mongo-driver/bson/primitive"
+	)
+	`))
+
+	getStmtsTmpl, getStmtsErr := ParseTemplate(`
+	func Get{{.entity}}ByID(
+		ctx context.Context,
+		id interface{},
+	) (
+	   entity *{{.entity}}, 
+	   err *errs.Error,
+	) {
+		var errd error
+
+		if idString, ok := id.(string); ok {
+			if id, errd = primitive.ObjectIDFromHex(idString); errd != nil {
+				return
+			}
+		}
+
+		options := Filter{{.entity}}Options(ctx)
+		options.Query = &bson.M{"_id": id}
+		options.Entity = entity
+		
+		if _, err = Api.FindOne(options); err != nil {
+			return
+		}
+		return
+	}
+	`)
+
+	if getStmtsErr != nil {
+		return getStmtsErr
+	}
+
+	for _, entity := range entities {
+
+		entityInfo := project.ResponseEntity(entity.ID)
+
+		if entityInfo.IsGeneric || entity.Type != "object" {
+			continue
+		}
+
+		context := map[string]interface{}{
+			"entity": strings.Title(entity.ID),
+		}
+		out, _ := TemplateToString(getStmtsTmpl, context)
+		file.Add(G.Id(out).Line())
+	}
+	
+	if err := Write(
+		fmt.Sprintf("../project/include/go/models/get_helpers.go"),
+		file,
+	); err != nil {
+		return err
+	}
+
+	return nil
+}
+