resources.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. package got
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. "strings"
  7. "sync"
  8. "time"
  9. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  10. G "github.com/dave/jennifer/jen"
  11. "github.com/davecgh/go-spew/spew"
  12. )
  13. type Middleware struct {
  14. Id string `json:"id"`
  15. Type string `json:"type"`
  16. Fn func(ctx *MiddlewareContext) error `json:"-"`
  17. }
  18. type MiddlewareContext struct {
  19. Project *Project
  20. Method *Method
  21. Statement *G.Statement
  22. Middleware *Middleware
  23. File *G.File
  24. }
  25. var (
  26. Middlewares = map[string]*Middleware{
  27. "post": GenCreateStmts,
  28. "put": GenUpdateStmts,
  29. "patch": GenPatchStmts,
  30. "delete": GenDeleteStmts,
  31. "get_one": GenGetStmtsOne,
  32. "pipe": GenPipeStmts,
  33. "filter": GenGetFilter,
  34. "get_list": GenGetStmtsList,
  35. "undelete": GenUndeleteStmts,
  36. "implement": GenImplement,
  37. }
  38. ResourceWG = sync.WaitGroup{}
  39. )
  40. func InitFile(file *G.File, resource *Resource, p *Project) {
  41. now := time.Now().UnixNano()
  42. // file.ImportAlias(
  43. // file.ImportName(
  44. // fmt.Sprintf("%s/%s/models", p.Custom["go.package.repository"].(string), p.Package),
  45. // "models",
  46. // )
  47. if _, defined := p.Custom["go.package.repository"]; !defined {
  48. panic("go.package.repository not defined in project.json")
  49. }
  50. file.Var().Defs(
  51. G.Id(fmt.Sprintf("_%s_bson_%d_ *", resource.Entity, now)).Qual(BSON, "M"),
  52. G.Id(fmt.Sprintf("_%s_error_%d_ *", resource.Entity, now)).Qual(API_ERROR, "Error"),
  53. G.Id(fmt.Sprintf("_%s_sync_%d_ *", resource.Entity, now)).Qual("sync", "WaitGroup"),
  54. G.Id(fmt.Sprintf("_%s_primitive_%d_ *", resource.Entity, now)).Qual("go.mongodb.org/mongo-driver/bson/primitive", "ObjectID"),
  55. G.Id(fmt.Sprintf("_%s_models_%d_ *", resource.Entity, now)).Qual(
  56. fmt.Sprintf("%s/build/%s/models", p.Custom["go.package.repository"].(string), p.Package),
  57. "Entity",
  58. ),
  59. G.Id(fmt.Sprintf("_%s_actions_%d_ *", resource.Entity, now)).Qual(
  60. fmt.Sprintf("%s/build/%s/actions", p.Custom["go.package.repository"].(string), p.Package),
  61. "Action",
  62. ),
  63. )
  64. file.Func().Id("init").Params().BlockFunc(func(s *G.Group) {
  65. for _, v := range resource.Formats {
  66. s.Add(G.Id("FormatSelection").Index(G.Lit(resource.ID + "." + v.Id)).Op("=").Lit(v.Fields))
  67. }
  68. })
  69. }
  70. func CreateDummy(p *Project, resource *Resource, method *Method) error {
  71. // Verifica se existe um arquivo na pasta de include.
  72. // Caso o arquivo não exista um novo arquivo é criado.
  73. // outputfile := fmt.Sprintf("%s/include/go/api_%s_%s.go", CurrentDirectory, strings.ToLower(resource.ID), strings.ToLower(method.ID))
  74. outputfile := fmt.Sprintf(
  75. "../project/include/go/api_%s_%s_gen.go",
  76. strings.ToLower(resource.ID),
  77. strings.ToLower(method.ID),
  78. )
  79. if _, err := os.Stat(outputfile); os.IsNotExist(err) {
  80. file := G.NewFile(p.Package)
  81. file.Comment(method.Description)
  82. if o, err := json.MarshalIndent(method, "", " "); err == nil {
  83. file.Comment(string(o))
  84. }
  85. file.Func().Params(
  86. G.Id("t").Op("*").Id(ResourceStructId(resource)),
  87. ).Id(method.ID).Params(
  88. G.Id("ctx").Qual(IRIS_CTX, "Context"),
  89. ).Params(
  90. G.Id("resp").Interface(),
  91. G.Id("err").Op("*").Qual(API_ERROR, "Error"),
  92. ).Block(G.Return())
  93. // if err = Write(path, file); err != nil {
  94. // return err
  95. // }
  96. return Write(outputfile, file)
  97. }
  98. return nil
  99. }
  100. func GenResources(p *Project) (err error) {
  101. var (
  102. file *G.File
  103. path string
  104. )
  105. if err = generateActionCommonFile(p); err != nil {
  106. return
  107. }
  108. for _, resource := range p.Resources {
  109. // Registra os campos retornados nos formatos do metodo list
  110. // FormatMap = map[string]string{}
  111. // Cria um novo arquivo para cada recurso
  112. file = G.NewFile(p.Package)
  113. InitFile(file, resource, p)
  114. // Inseri os comentarios de cada recurso no inicio do arquivo
  115. file.Comment(resource.Description).Line()
  116. // Define um tipo para registrar os metodos da api
  117. file.Type().Id(ResourceStructId(resource)).Struct().Line()
  118. for _, method := range resource.Methods {
  119. switch method.Template {
  120. case "", "implement":
  121. CreateDummy(p, resource, method)
  122. default:
  123. GenMethod(p, file, resource, method)
  124. }
  125. }
  126. // file.Var().DefsFunc(func(s *G.Group) {
  127. // values := G.Dict{}
  128. // for k, v := range FormatMap {
  129. // values[G.Lit(k)] = G.Lit(v)
  130. // }
  131. // s.Add(
  132. // G.Id("Format" + resource.Entity).Op("=").Map(G.String()).String().Values(values),
  133. // )
  134. // })
  135. file.Func().Params(
  136. G.Id("t").Op("*").Id(ResourceStructId((resource))),
  137. ).Id("Fields").Params(
  138. G.Id("filter").Op("*").Qual(API_URL, "Filter"),
  139. ).Params(
  140. G.Id("err").Op("*").Qual(API_ERROR, "Error"),
  141. ).Block(
  142. G.Id(`
  143. if filter.Fields != nil {
  144. return
  145. }
  146. if filter.Format == "full" {
  147. filter.Fields = nil
  148. return
  149. }
  150. if fields, has := FormatSelection[`).Lit(fmt.Sprintf("%s.", resource.ID)).Id(`+filter.Format]; has {
  151. filter.Fields = api.MgoFields(fields)
  152. } else {
  153. err = errs.InvalidArgument().Details(&errs.Detail{
  154. Message: `).Qual("fmt", "Sprintf").Id(`("Invalid value for projection '%s'",filter.Format),
  155. })
  156. }
  157. return
  158. `),
  159. // G.If(
  160. // // G.List(G.Id("fields"), G.Id("has")).Op(":=").Id("FormatSelection"+resource.Entity).Index(G.Id("filter").Dot("Format")),
  161. // G.List(G.Id("fields"), G.Id("has")).Op(":=").Id("FormatSelection").Index(
  162. // G.Lit(resource.ID+".").Op("+").Id("filter").Dot("Format"),
  163. // ),
  164. // G.Id("has"),
  165. // ).Block(
  166. // G.Id("filter").Dot("Fields").Op("=").Id("api").Dot("MgoFields").Call(G.Id("fields")),
  167. // ).Else().Block(
  168. // G.Id("err").Op("=").Qual(API_URL, "Error").Call(
  169. // G.Qual(API_URL, "ERR_INVALID_PARAM_VALUE"),
  170. // G.Lit("Invalid value for %s"),
  171. // ),
  172. // // .Dot("Add").Call(
  173. // // G.Op("&").Qual(API_URL, "ErrDescription").Values(G.Dict{
  174. // // G.Id("Message"): G.Lit(""),
  175. // // }),
  176. // // ),
  177. // ),
  178. )
  179. // Fim do loop de methods
  180. // GenCall(f, resource)
  181. path = fmt.Sprintf("%s/%s/api_%s_gen.go", p.OutPath, p.Package, strings.ToLower(resource.ID))
  182. if err = Write(path, file); err != nil {
  183. return err
  184. }
  185. }
  186. // Fim do loop de recursos
  187. ResourceWG.Add(2)
  188. go GenIndexApi(p)
  189. ResourceWG.Wait()
  190. return nil
  191. }
  192. func GenQueries(p *Project, resourcesIdMap map[string]bool) {
  193. defer func() { ResourceWG.Done() }()
  194. if len(p.Queries.Queries) == 0 {
  195. return
  196. }
  197. file := G.NewFile(p.Package)
  198. queries := func(s *G.Statement) {
  199. var (
  200. query string
  201. found bool
  202. )
  203. for key, _ := range resourcesIdMap {
  204. if query, found = p.Queries.Queries[fmt.Sprintf("go.%s", key)]; !found {
  205. query = "{}"
  206. }
  207. s.Add(G.Qual(API_URL, "RegisterQuery(").Lit(key).Id(",").Lit(query).Id(")").Line())
  208. }
  209. }
  210. file.Id(`
  211. func init() {`).Do(queries).Id(`}`)
  212. if err := Write(fmt.Sprintf("%s/%s/%s_gen.go", p.OutPath, p.Package, "queries"), file); err != nil {
  213. panic(err)
  214. }
  215. }
  216. func GenIndexApi(p *Project) error {
  217. defer func() { ResourceWG.Done() }()
  218. var (
  219. stmt *G.Statement
  220. params string
  221. statments G.Statement
  222. idString string
  223. middlewares []string
  224. Index = G.NewFile(p.Package)
  225. RequestParams = G.Id(`RequestParams := `).Qual(CODE_GEN_V2_COMMON, "RequestParams")
  226. jwt = G.Id(`JWT := `).Qual(CODE_GEN_V2_AUTHORIZATION, "Handler")
  227. addJwt = false
  228. queryIndexsMap = map[string]bool{}
  229. callActionId = "apply"
  230. )
  231. statments = append(statments, G.Id(fmt.Sprintf("%s := ", callActionId)).Qual(API_URL, "CallAction"))
  232. statments = append(statments, RequestParams)
  233. // statments = append(statments)
  234. // Inicializa o mapa de filtros da api
  235. Index.Id(`
  236. var (
  237. filtersApiReference = map[string]*`).Qual(CODE_GEN_V2_COMMON, "ApiFilter").Id(`{}
  238. FormatSelection = map[string]string{}
  239. )
  240. func init(){
  241. var (
  242. entity *common.ApiFilter
  243. files []`).Qual("os", "FileInfo").Id(`
  244. path = "./filters"
  245. err error
  246. )
  247. files, _ = `).Qual(CODE_GEN_V2_COMMON, "GetFiles").Id(`(path)
  248. for _, file := range files {
  249. if !file.IsDir() {
  250. entity = &common.ApiFilter{}
  251. if err = common.ParseJson(`).Qual("path/filepath", "Join").Id(`(path, file.Name()), entity); err != nil {
  252. panic(err)
  253. }
  254. filtersApiReference[entity.Id] = entity
  255. }
  256. }
  257. }
  258. func executeAction(ctx context.Context, actions ...func(context.Context) (interface{}, *`).Qual(API_ERROR, "Error").Id(`)) (resp interface{},err *errs.Error){
  259. var stopPropagation bool
  260. for _, action := range actions {
  261. resp, err = action(ctx)
  262. stopPropagation,_ = ctx.Values().GetBool("stop.propagation")
  263. switch {
  264. case stopPropagation, err != nil, resp != nil:
  265. return
  266. }
  267. }
  268. return
  269. }
  270. `)
  271. for rindex, resource := range p.Resources {
  272. stmt = G.Line().Id(resource.ID).Op(":=").Id(ResourceStructId(resource)).Values().Line()
  273. for k, method := range resource.Methods {
  274. // fmt.Println("----------------------------")
  275. args := []G.Code{
  276. G.Lit(method.HttpMethod),
  277. G.Lit(p.GetPath(method)),
  278. }
  279. middlewares = []string{}
  280. if len(method.ParametersString) > 0 {
  281. params = strings.Join(method.ParametersString, ",")
  282. // fmt.Println(fmt.Sprintf(`%s|RequestParams("%s", UserRequestParams)`, API_URL, params))
  283. stmt.Add(G.Line().Comment(
  284. "Lista de parametros a serem validados durante a requisição",
  285. ).Line().Id(fmt.Sprintf(`args%d%d := "%s"`, rindex, k, params)).Line())
  286. middlewares = append(middlewares, fmt.Sprintf(`RequestParams(args%d%d, UserRequestParams)`, rindex, k))
  287. }
  288. middlewares = append(middlewares, p.Middlewares...)
  289. middlewares = append(middlewares, method.Middlewares...)
  290. data := map[string]interface{}{
  291. "ResourceId": resource.ID,
  292. "MethodId": method.ID,
  293. }
  294. idString = fmt.Sprintf("%s:%s", resource.ID, method.ID)
  295. queryIndexsMap[idString] = true
  296. for _, m := range middlewares {
  297. if strings.Contains(m, "JWT") && !addJwt {
  298. addJwt = true
  299. RequestParams.Line().Add(jwt)
  300. }
  301. m = ResolveParams(m, data)
  302. parts := strings.Split(m, "|")
  303. // Quando parts possui tamanho maior que
  304. // significa que foi especificado um middleware de outro pacote.
  305. if len(parts) > 1 {
  306. args = append(args, G.Line().Id(callActionId).Call(
  307. G.Id(parts[1]),
  308. G.Qual(parts[0], parts[1]),
  309. ))
  310. } else {
  311. args = append(args, G.Line().Id(callActionId).Call(
  312. G.Lit(m),
  313. G.Id(m),
  314. ))
  315. }
  316. }
  317. args = append(args, G.Line().Id(callActionId).Call(
  318. G.Lit(fmt.Sprintf("%s.%s", resource.ID, method.ID)),
  319. G.Id(resource.ID).Dot(method.ID),
  320. ))
  321. if len(method.Postresponse) > 0 {
  322. args = append(args, G.Line().Id(method.Postresponse[0]))
  323. }
  324. stmt.Add(G.Line().Comment(method.Description).Line().Id("app").Dot("Handle").Call(args...).Line())
  325. }
  326. statments = append(statments, stmt)
  327. }
  328. // Cria a funcao que trata os filtros
  329. // statments = append(statments, G.Line().Comment("Filter request").Line().Id("app").Dot("Handle").Call(
  330. // G.Lit("GET"),
  331. // G.Lit(p.BasePath+"/filters/{id:string}"),
  332. // // G.Func().Params(G.Id("ctx").Qual(IRIS_CTX, "Context")).Block(),
  333. // G.Id(fmt.Sprintf(`FilterHandle("../api/%s/filters")`, p.Package)),
  334. // ))
  335. // Cria a funcao que trata os metodos options
  336. statments = append(statments, G.Line().Comment("Options request").Line().Id("app").Dot("Options").Call(
  337. G.Lit("/{url:path}"),
  338. G.Func().Params(G.Id("ctx").Qual(IRIS_CTX, "Context")).Block(
  339. G.Id(`ctx.ResponseWriter().Header().Set("Access-Control-Allow-Origin", ctx.GetHeader("Origin"))`),
  340. ),
  341. ))
  342. // Cria a funcao que registra as urls da api no arquivo api_index_gen.go
  343. Index.Func().Id("Register").Params(
  344. G.Id("app").Op("*").Qual(IRIS, "Application"),
  345. ).Block(
  346. statments...,
  347. ).Line()
  348. go GenQueries(p, queryIndexsMap)
  349. return Write(fmt.Sprintf("%s/%s/api_index_gen.go", p.OutPath, p.Package), Index)
  350. }
  351. func GenMethod(p *Project, f *G.File, r *Resource, method *Method) error {
  352. f.Comment(method.Description)
  353. if o, err := json.MarshalIndent(method, "", " "); err == nil {
  354. f.Comment(string(o))
  355. }
  356. stmt := f.Func().Params(
  357. // G.Id("t").Op("*").Id(strings.Title(r.ID)),
  358. G.Id("t").Op("*").Id(ResourceStructId(r)),
  359. ).Id(method.ID).Params(
  360. G.Id("ctx").Qual(IRIS_CTX, "Context"),
  361. // G.Id("resp").Op("*").Qual(API_URL, "ApiResponse"),
  362. ).Params(
  363. G.Id("resp").Interface(),
  364. G.Id("err").Op("*").Qual(API_ERROR, "Error"),
  365. )
  366. generateActionsFiles(p, f, r, method)
  367. if middle, found := Middlewares[method.Template]; found {
  368. ctx := &MiddlewareContext{
  369. Project: p,
  370. Method: method,
  371. Middleware: middle,
  372. Statement: stmt,
  373. File: f,
  374. }
  375. return middle.Fn(ctx)
  376. }
  377. return fmt.Errorf("Method '%s' template not defined!", method.ID)
  378. }
  379. func generateActionCommonFile(p *Project) (err error) {
  380. path := fmt.Sprintf(
  381. "%s/include/go/actions/index_gen.go",
  382. CurrentDirectory,
  383. )
  384. if _, fileErr := os.Stat(path); os.IsNotExist(fileErr) {
  385. file := G.NewFile("actions")
  386. file.Id(`
  387. type Action struct {
  388. ID string
  389. }
  390. `).Line()
  391. err = Write(path, file)
  392. }
  393. return
  394. }
  395. func generateActionsFiles(p *Project, f *G.File, r *Resource, method *Method) {
  396. actions := []Action{}
  397. if method.Preconditions != nil {
  398. actions = append(actions, method.Preconditions...)
  399. }
  400. if method.BeforeResponse != nil {
  401. actions = append(actions, method.BeforeResponse...)
  402. }
  403. if method.BeforeParseRequest != nil {
  404. actions = append(actions, method.BeforeParseRequest...)
  405. }
  406. for _, action := range actions {
  407. path := fmt.Sprintf(
  408. "%s/include/go/actions/%s_gen.go",
  409. CurrentDirectory,
  410. action.ID,
  411. // method.Entity,
  412. // method.ID,
  413. // hookId,
  414. )
  415. if _, fileErr := os.Stat(path); os.IsNotExist(fileErr) {
  416. // methodId := fmt.Sprintf("%s%s%s", strings.Title(hookId), method.Entity, strings.Title(method.ID))
  417. file := G.NewFile("actions")
  418. context := map[string]interface{}{
  419. "imports": map[string]string{
  420. "errs": "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs",
  421. "context": "github.com/kataras/iris/v12/context",
  422. },
  423. "function": strings.Title(action.ID),
  424. "hasContext": action.Context != nil,
  425. }
  426. spew.Dump(action.ID)
  427. spew.Dump(action.Context)
  428. out, _ := TemplateToString(hookStmtsTmpl, context)
  429. file.Id(out).Line()
  430. Write(path, file)
  431. }
  432. }
  433. // for hookId, _ := range method.Hooks {
  434. // // methodId := fmt.Sprintf("%s%s%s", strings.Title(hookId), method.Entity, strings.Title(method.ID))
  435. // path := fmt.Sprintf(
  436. // "%s/include/go/actions/%s_%s_%s_gen.go",
  437. // CurrentDirectory,
  438. // method.Entity,
  439. // method.ID,
  440. // hookId,
  441. // )
  442. // if _, fileErr := os.Stat(path); os.IsNotExist(fileErr) {
  443. // file := G.NewFile(p.Package)
  444. // context := map[string]interface{}{
  445. // "imports": map[string]string{
  446. // // "api": "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api",
  447. // "errs": "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs",
  448. // "context": "github.com/kataras/iris/v12/context",
  449. // },
  450. // }
  451. // out, _ := TemplateToString(hookStmtsTmpl, context)
  452. // file.Id(out).Line()
  453. // Write(path, file)
  454. // }
  455. // }
  456. }
  457. func GenFromGenericModel(p *Project, entity *EntityInfo) {
  458. var (
  459. posfix string
  460. propertie *G.Statement
  461. tproperties G.Statement
  462. // file = G.NewFile(p.Package)
  463. file = G.NewFile("models")
  464. cproperties = map[string]*G.Statement{}
  465. properties = G.Statement{}
  466. values = G.Dict{}
  467. model = p.GetSchema(entity.Name)
  468. entityName = entity.NewName
  469. // filename = "model_" + strings.ToLower(entityName)
  470. filename = "models/" + CamelToUnder(entityName)
  471. propName string
  472. )
  473. for _, meta := range model.Properties {
  474. propName = UpFirst(meta.ID)
  475. propertie = G.Id(propName)
  476. meta.FillTags(p, propName)
  477. posfix = ""
  478. // Registra a relaao entre as entidades
  479. if meta.Relation {
  480. posfix = "Ref"
  481. SR.Add(&Relation{
  482. Source: meta.GetType(),
  483. Target: model.ID,
  484. Attr: strings.Replace(meta.Tags["bson"], ",omitempty", "", 1),
  485. DB: model.DB,
  486. Collection: model.Collection,
  487. IsArray: meta.Array,
  488. })
  489. }
  490. if meta.Array {
  491. propertie.Index()
  492. }
  493. propertie.Id(entity.TranslateType(meta.Type) + posfix)
  494. // propertie.Id(meta.Type + posfix)
  495. // Adiciona as tags caso sejam definidas
  496. if meta.Tags != nil {
  497. propertie.Tag(meta.Tags)
  498. // if name, ok := meta.Tags["json"]; ok {
  499. // }
  500. }
  501. // Adiciona a crescricao como comentario
  502. if meta.Description != "" {
  503. propertie.Comment(meta.Description)
  504. }
  505. cproperties[meta.ID] = propertie
  506. if meta.ID == "ID" {
  507. values[G.Id("ID")] = G.Qual(BSON_PRIMITIVE, "NewObjectID").Call()
  508. }
  509. // Verifica se possui valor padrão
  510. if meta.Default != nil {
  511. values[G.Id(meta.ID)] = G.Lit(meta.Default)
  512. }
  513. properties = append(properties, propertie)
  514. }
  515. if model.Representations != nil {
  516. for posfix, rep := range model.Representations {
  517. tproperties = G.Statement{}
  518. for _, attr := range rep {
  519. tproperties = append(tproperties, cproperties[attr])
  520. }
  521. file.Comment(
  522. "Representação " + posfix,
  523. ).Line().Type().Id(
  524. model.ID + posfix,
  525. ).Struct(tproperties...)
  526. }
  527. }
  528. // Cria a entidade normal
  529. file.Line().Comment(model.Description).Line().Comment("Representação Completa")
  530. file.Type().Id(entityName).Struct(properties...)
  531. file.Comment(fmt.Sprintf("Cria uma instancia de %s.", entityName))
  532. // Cria a função de instanciar um novo elemento com os valores padrão determinados
  533. file.Func().Id("New" + entityName).Params().Op("*").Id(entityName).Block(
  534. G.Return(G.Op("&").Id(entityName).Values(values)),
  535. )
  536. // Salva o arquivo da entidade
  537. // if err := f.Save(fmt.Sprintf("%s/%s/%s_gen.go", p.OutPath, p.Package, filename)); err != nil {
  538. // fmt.Printf("%s/%s/%s_gen.go", p.OutPath, p.Package, filename)
  539. if err := Write(fmt.Sprintf("%s/%s/%s_gen.go", p.OutPath, p.Package, filename), file); err != nil {
  540. panic(err)
  541. }
  542. }
  543. func Write(path string, file *G.File) error {
  544. // fmt.Println(fmt.Sprintf("Write -> %#v", path))
  545. return FilePutContents(path, fmt.Sprintf("%#v", file), 0777)
  546. }
  547. func ResourceStructId(resource *Resource) string {
  548. return strings.Title(resource.ID) + "Resource"
  549. }