resources.go 18 KB

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