Browse Source

set params in context

EUGENIO SOUZA CARVALHO 4 years ago
parent
commit
87d0cbb44e

+ 2 - 2
api/sse/hub.go

@@ -152,7 +152,7 @@ func (hub *SSEHub) UpgradeConnection(ctx context.Context, channelId string) (err
 		err = errs.HTTPVersionNotSupported().Details(&errs.Detail{
 			Dominio:      "",
 			Reason:       "Streaming unsupported",
-			Location:     "hook.beforePersist/caracterizationForm",
+			Location:     "hook.beforePersist",
 			LocationType: "",
 		})
 		return
@@ -168,7 +168,7 @@ func (hub *SSEHub) UpgradeConnection(ctx context.Context, channelId string) (err
 		err = errs.Internal().Details(&errs.Detail{
 			Dominio:      "",
 			Reason:       "Fail on register channel",
-			Location:     "hook.beforePersist/caracterizationForm",
+			Location:     "hook.beforePersist",
 			LocationType: "",
 		})
 		return

+ 12 - 3
common/models.go

@@ -354,10 +354,10 @@ func RequestParams(args string, params map[string]*Parameter) func(ctx context.C
 			switch param.Location {
 
 			case "query":
-				id = "q." + arg
+				id = "query." + arg
 				value = api.Q(ctx, arg, param.Default)
 			case "path":
-				id = "p." + arg
+				id = "path." + arg
 				value = api.P(ctx, arg, param.Default)
 			}
 			sourceValue = value
@@ -399,7 +399,9 @@ func RequestParams(args string, params map[string]*Parameter) func(ctx context.C
 				}
 			}
 
-			values.Set(id, value)
+			values.Set(fmt.Sprintf("%s_conv", id), value)
+			values.Set(id, sourceValue)
+
 			paramsMap[fmt.Sprintf("%s_conv", arg)] = value
 			paramsMap[arg] = sourceValue
 		}
@@ -726,6 +728,13 @@ func (p *Project) GetCollection(entity string) string {
 	return "undefined"
 }
 
+func getCustom(options map[string]interface{}, path string) (resp interface{}) {
+	if options != nil {
+		resp = options[path]
+	}
+	return
+}
+
 func (p *Project) GetEntityDB(entity string) string {
 	if en, found := p.SchemasRef[entity]; found {
 		return en.DB + p.DataBaseSufix

+ 1 - 1
translate/got/middleware_delete.go

@@ -51,7 +51,7 @@ func init() {
 				Dominio:      "",
 				Reason:       "",
 				Location:     "middleware.path",
-				LocationType: "middleware.operation/Caracterization",
+				LocationType: "middleware.operation",
 			})
 		}
 

+ 3 - 1
translate/got/middleware_main.go

@@ -78,7 +78,9 @@ func CreateMainFile(project *Project) (err error) {
 		return
 	}
 
-	path := fmt.Sprintf("%s/v1/app_initialize.go", project.OutPath)
+	// path := fmt.Sprintf("%s/v1/app_initialize.go", project.OutPath)
+	path := fmt.Sprintf("../project/include/go/app_initialize.go")
+
 	if !utils.FileExists(path) {
 
 		AppInitialize := G.NewFile("v1")

+ 6 - 1
translate/got/resources.go

@@ -81,7 +81,12 @@ func CreateDummy(p *Project, resource *Resource, method *Method) error {
 	// Verifica se existe um arquivo na pasta de include.
 	// Caso o arquivo não exista um novo arquivo é criado.
 
-	outputfile := fmt.Sprintf("%s/include/go/%s_%s.go", CurrentDirectory, strings.ToLower(resource.ID), strings.ToLower(method.ID))
+	// outputfile := fmt.Sprintf("%s/include/go/api_%s_%s.go", CurrentDirectory, strings.ToLower(resource.ID), strings.ToLower(method.ID))
+	outputfile := fmt.Sprintf(
+		"../project/include/go/api_%s_%s_gen.go",
+		strings.ToLower(resource.ID),
+		strings.ToLower(method.ID),
+	)
 
 	if _, err := os.Stat(outputfile); os.IsNotExist(err) {
 

+ 7 - 0
translate/got/schemas.go

@@ -810,6 +810,7 @@ func GenEntityHelpers(project  *Project) error {
 
 	file.Add(G.Id(`
 	import(
+		"reflect"
 		"git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
 		context "github.com/kataras/iris/v12/context"
 		"go.mongodb.org/mongo-driver/bson"
@@ -826,6 +827,12 @@ func GenEntityHelpers(project  *Project) error {
 	   err *errs.Error,
 	) {
 		var errd error
+		entity = &{{.entity}}{}
+		value := reflect.ValueOf(id)
+
+		if value.Kind() == reflect.Ptr  {
+			id = value.Elem() 
+		}
 
 		if idString, ok := id.(string); ok {
 			if id, errd = primitive.ObjectIDFromHex(idString); errd != nil {

+ 14 - 1
translate/tst/resources.go

@@ -536,8 +536,21 @@ func GenAngularMethodStmt(p *Project, r *Resource, method *Method, methods *[]TS
 		panic(fmt.Sprintf("Method '%s' template not defined!", template))
 	}
 
+	path := method.Path
+	for _, parameter := range method.Parameters {
+		
+	 alias := getCustom(parameter.Custom, "ts.api.alias")
+	 if aliasString, ok := alias.(string);ok {
+		path = strings.ReplaceAll(
+			path,
+			fmt.Sprintf("{%s}", parameter.ID),
+			fmt.Sprintf("{%s}", aliasString),
+		)
+	 }
+	}
+	
 	// paramsHttp = append(paramsHttp, TS.Raw("this.Url(url, opt.params)"))
-	paramsHttp = append(paramsHttp, TS.Raw(fmt.Sprintf("`${this.api.apiOptions.BASE_URL}%s`", method.Path)))
+	paramsHttp = append(paramsHttp, TS.Raw(fmt.Sprintf("`${this.api.apiOptions.BASE_URL}%s`", path)))
 
 	switch typ {
 	case "post", "put", "patch":

+ 0 - 184
translate/tst/schemas.go

@@ -335,187 +335,3 @@ func appendBasePatchClass() {
 	}
 	`)
 }
-
-// 	public Patchs() {
-
-// 	  const patchs = {
-// 		AddMethods: [],
-// 		AddSamples: [],
-// 		AddCaracterizations: [],
-// 		RemoveMethods: [],
-// 		RemoveSamples: [],
-// 		RemoveCaracterizations: [],
-// 		$set: this.$set,
-// 	  };
-
-// 	  console.log('paths', this);
-
-// 	  this.__set(this._samples, patchs.AddSamples, patchs.RemoveSamples);
-// 	  this.__set(this._methods, patchs.AddMethods, patchs.RemoveMethods);
-
-// 	  patchs.RemoveSamples.map(s => {
-// 		this.RemoveCaracterizations(this._form.caracterizations.filter(c => {
-// 		  return c.sample === s.id;
-// 		}));
-// 	  });
-
-// 	  patchs.RemoveMethods.map(m => {
-// 		this.RemoveCaracterizations(this._form.caracterizations.filter(c => {
-// 		  return c.method === m.id;
-// 		}));
-// 	  });
-
-// 	  this.__set(this._caracterizations, patchs.AddCaracterizations, patchs.RemoveCaracterizations);
-
-// 	  return patchs;
-// 	}
-// public Patchs(patchs) {
-// 	const patchs = {
-// 		AddMethods: [],
-// 		AddSamples: [],
-// 		AddCaracterizations: [],
-// 		RemoveMethods: [],
-// 		RemoveSamples: [],
-// 		RemoveCaracterizations: [],
-// 		$set: this.$set,
-// 	};
-
-// 	console.log('paths', this);
-
-// 	this.__set(this._samples, patchs.AddSamples, patchs.RemoveSamples);
-// 	this.__set(this._methods, patchs.AddMethods, patchs.RemoveMethods);
-
-// 	patchs.RemoveSamples.map(s => {
-// 	this.RemoveCaracterizations(this._form.caracterizations.filter(c => {
-// 		return c.sample === s.id;
-// 	}));
-// 	});
-
-// 	patchs.RemoveMethods.map(m => {
-// 	this.RemoveCaracterizations(this._form.caracterizations.filter(c => {
-// 		return c.method === m.id;
-// 	}));
-// 	});
-
-// 	this.__set(this._caracterizations, patchs.AddCaracterizations, patchs.RemoveCaracterizations);
-
-// 	return patchs;
-// }
-
-// export class FormPatch {
-
-// 	$set = {};
-// 	_form: CaracterizationForm;
-// 	_samples = new Map<string, { item: Sample, action: string, type: string }>();
-// 	_methods = new Map<string, { item: CaracterizationMethod, action: string, type: string }>();
-// 	_caracterizations = new Map<string, { item: Caracterization, action: string, type: string }>();
-
-// 	setSolicitation(form: CaracterizationForm) {
-// 	  // this._set(this._methods, form.methods.map(m => { return { id: m.id } }), '');
-// 	  this._form = form;
-// 	  this._set(this._samples, form.samples, '');
-// 	  this._set(this._methods, form.methods, '');
-// 	  this._set(this._caracterizations, form.caracterizations, '');
-// 	}
-
-// 	protected __set(list, add, rem) {
-// 	  list.forEach((i) => {
-// 		switch (i.action) {
-// 		  case 'add':
-// 			add.push(i.item);
-// 			break;
-// 		  case 'remove':
-// 			(i.type !== 'new') && rem.push(i.item);
-// 			break;
-// 		}
-// 	  });
-// 	}
-
-// 	public Patchs() {
-
-// 	  const patchs = {
-// 		AddMethods: [],
-// 		AddSamples: [],
-// 		AddCaracterizations: [],
-// 		RemoveMethods: [],
-// 		RemoveSamples: [],
-// 		RemoveCaracterizations: [],
-// 		$set: this.$set,
-// 	  };
-
-// 	  console.log('paths', this);
-
-// 	  this.__set(this._samples, patchs.AddSamples, patchs.RemoveSamples);
-// 	  this.__set(this._methods, patchs.AddMethods, patchs.RemoveMethods);
-
-// 	  patchs.RemoveSamples.map(s => {
-// 		this.RemoveCaracterizations(this._form.caracterizations.filter(c => {
-// 		  return c.sample === s.id;
-// 		}));
-// 	  });
-
-// 	  patchs.RemoveMethods.map(m => {
-// 		this.RemoveCaracterizations(this._form.caracterizations.filter(c => {
-// 		  return c.method === m.id;
-// 		}));
-// 	  });
-
-// 	  this.__set(this._caracterizations, patchs.AddCaracterizations, patchs.RemoveCaracterizations);
-
-// 	  return patchs;
-// 	}
-
-// 	protected _set(_map: Map<string, any>, itens, action) {
-// 	  (itens || []).map(item => {
-// 		const id = item.id || (item.method + item.sample);
-// 		const has = _map.has(id)
-// 		  , el = _map.get(id);
-
-// 		switch (action) {
-// 		  case 'add':
-// 			if (!has) {
-// 			  _map.set(id, {
-// 				item: item,
-// 				action: 'add',
-// 				type: 'new',
-// 			  });
-// 			} else if (el.action === 'remove') {
-// 			  el.action = 'add';
-// 			}
-// 			break;
-// 		  case 'remove':
-// 			if (has) {
-// 			  el.action = 'remove';
-// 			}
-// 			break;
-// 		  default:
-// 			_map.set(id, {
-// 			  item: item,
-// 			  action: '',
-// 			  type: '',
-// 			});
-// 		}
-// 	  });
-// 	}
-
-// 	AddMethods(itens) {
-// 	  this._set(this._methods, itens, 'add');
-// 	}
-
-// 	AddSamples(itens) {
-// 	  this._set(this._samples, itens, 'add');
-// 	}
-// 	AddCaracterizations(itens) {
-// 	  this._set(this._caracterizations, itens, 'add');
-// 	}
-// 	RemoveMethods(itens) {
-// 	  this._set(this._methods, itens, 'remove');
-// 	}
-// 	RemoveSamples(itens) {
-// 	  this._set(this._samples, itens, 'remove');
-// 	}
-// 	RemoveCaracterizations(itens) {
-// 	  this._set(this._caracterizations, itens, 'remove');
-// 	}
-
-//   }

+ 7 - 0
translate/tst/translate.go

@@ -90,3 +90,10 @@ func imports(project *Project, file *TS.File) {
 	
 	`).Line()
 }
+
+func getCustom(options map[string]interface{}, path string) (resp interface{}) {
+	if options != nil {
+		resp = options[path]
+	}
+	return
+}