Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvillacortac committed Mar 16, 2022
1 parent 9528077 commit b0f3e72
Show file tree
Hide file tree
Showing 18 changed files with 195 additions and 198 deletions.
17 changes: 13 additions & 4 deletions cmd/rosetta.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ var (
version = "0.0.0"
commit = "XXX"

config = flag.String("c", "config.yml", "path to the config file (json or yaml)")
output = flag.String("o", "", "path to the output base path")
schema = flag.String("s", "", "path to the schema file (overrides defined in json config file) (default \"schema.yml\" if not defined in config)")
showVersion = flag.Bool("v", false, "show version")
config = flag.String("c", "config.yml", "Path to the config file (json or yaml)")
output = flag.String("o", "", "Path to the output base path")
schema = flag.String("s", "", "Path to the schema file (overrides defined in json config file) (default \"schema.yml\" if not defined in config)")
rm = flag.Bool("rm", false, "Clean output path before generating")
showVersion = flag.Bool("v", false, "Show version")
verbose = flag.Bool("V", false, "Verbose output")
)

Expand Down Expand Up @@ -73,6 +74,14 @@ func run() int {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}

if *rm && p.OutputBasePath != "" {
if err := os.RemoveAll(p.OutputBasePath); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
}

for _, f := range files {
path := filepath.Dir(f.Filename)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions examples/angular/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ definitions:

generators:
- name: TS Models
ignore: model_ignore
template: templates/model.ts.go.tpl
output: models/[model-].ts
from: ts

- name: TS Service
ignore: service_ignore
template: templates/service.ts.go.tpl
output: services/[model-].service.ts
from: ts
2 changes: 1 addition & 1 deletion examples/angular/generate.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
go run ../.. -c config.yml
go run ../.. -rm
4 changes: 4 additions & 0 deletions examples/angular/generated/models/auxiliary-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export class AuxiliaryFilter {
id: number
}

This file was deleted.

22 changes: 7 additions & 15 deletions examples/angular/generated/services/auxiliary.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ export class AuxiliaryService {
private _httpClient: HttpClient,
private _httpHelpersService: HttpHelpersService
) {}

getAuxiliaryList(filter: AuxiliaryListFilter) {
getAuxiliaries(filter: AuxiliaryListFilter) {
return this._httpClient.get<Auxiliary[]>(
`/Auxiliary/list`,
'/auxiliaries',
{
params: this._httpHelpersService.getHttpParamsFromPlainObject(
filter,
Expand All @@ -24,20 +23,13 @@ export class AuxiliaryService {
}
);
}

getAuxiliary(filter: AuxiliaryFilter) {
return this._httpClient.get<Auxiliary>(
`/Auxiliary/`,
{
params: this._httpHelpersService.getHttpParamsFromPlainObject(
filter,
false
),
}
);
const { id } = filter
return this._httpClient.get<Auxiliary>(`/auxiliaries/${id}`);
}

postAuxiliary(model: Auxiliary) {
return this._httpClient.post<number>(`/Auxiliary/`, model);
return this._httpClient.post<number>('/auxiliaries', model);
}
}
29 changes: 1 addition & 28 deletions examples/angular/generated/services/foo.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Foo } from "../models/foo";
import { FooListFilter } from "../models/foo-list-filter";
import { FooFilter } from "../models/foo-filter";

@Injectable({
providedIn: "root",
Expand All @@ -12,32 +10,7 @@ export class FooService {
private _httpClient: HttpClient,
private _httpHelpersService: HttpHelpersService
) {}

getFooList(filter: FooListFilter) {
return this._httpClient.get<Foo[]>(
`/Foo/list`,
{
params: this._httpHelpersService.getHttpParamsFromPlainObject(
filter,
false
),
}
);
}

getFoo(filter: FooFilter) {
return this._httpClient.get<Foo>(
`/Foo/`,
{
params: this._httpHelpersService.getHttpParamsFromPlainObject(
filter,
false
),
}
);
}

postFoo(model: Foo) {
return this._httpClient.post<number>(`/Foo/`, model);
return this._httpClient.post<number>('/foos', model);
}
}
29 changes: 1 addition & 28 deletions examples/angular/generated/services/vector.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Vector } from "../models/vector";
import { VectorListFilter } from "../models/vector-list-filter";
import { VectorFilter } from "../models/vector-filter";

@Injectable({
providedIn: "root",
Expand All @@ -12,32 +10,7 @@ export class VectorService {
private _httpClient: HttpClient,
private _httpHelpersService: HttpHelpersService
) {}

getVectorList(filter: VectorListFilter) {
return this._httpClient.get<Vector[]>(
`/Vector/list`,
{
params: this._httpHelpersService.getHttpParamsFromPlainObject(
filter,
false
),
}
);
}

getVector(filter: VectorFilter) {
return this._httpClient.get<Vector>(
`/Vector/`,
{
params: this._httpHelpersService.getHttpParamsFromPlainObject(
filter,
false
),
}
);
}

postVector(model: Vector) {
return this._httpClient.post<number>(`/Vector/`, model);
return this._httpClient.post<number>('/vectors', model);
}
}
46 changes: 46 additions & 0 deletions examples/angular/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"Auxiliary": {
"id": {
"type": "int64",
"pk": true
},
"name": "string",
"active": "boolean",
"idUser": "int64",
"userName": "string",
"(list-filter)": "AuxiliaryListFilter",
"(filter)": "AuxiliaryFilter"
},
"AuxiliaryListFilter": {
"id": {
"type": "int64",
"default": -1
},
"active": {
"type": "int32",
"default": -1
},
"idUser": {
"type": "int64",
"default": -1
},
"(service_ignore)": true
},
"AuxiliaryFilter": {
"id": {
"type": "int64",
"pk": true
},
"(service_ignore)": true
},
"Vector": {
"x": "int64",
"y": "int64"
},
"Foo": {
"bar": "Auxiliary",
"vectors": {
"type": "Vector"
}
}
}
10 changes: 10 additions & 0 deletions examples/angular/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Auxiliary:
active: boolean
idUser: int64
userName: string
# OPTIONS
(list-filter): AuxiliaryListFilter
(filter): AuxiliaryFilter

AuxiliaryListFilter:
id:
Expand All @@ -17,6 +20,13 @@ AuxiliaryListFilter:
idUser:
type: int64
default: -1
# OPTIONS
(service_ignore): true

AuxiliaryFilter:
id: { type: int64, pk: true }
# OPTIONS
(service_ignore): true

Vector:
x: int64
Expand Down
37 changes: 20 additions & 17 deletions examples/angular/templates/service.ts.go.tpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{{- $listFilter := NodeOption .Model "list-filter" -}}
{{- $filter := NodeOption .Model "filter" -}}
import { HttpClient } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { {{ .Model.Name }} } from "../models/{{ .Model.Name | KebabCase }}";
import { {{ .Model.Name }}ListFilter } from "../models/{{ .Model.Name | KebabCase }}-list-filter";
import { {{ .Model.Name }}Filter } from "../models/{{ .Model.Name | KebabCase }}-filter";
{{- if $listFilter }}
import { {{ $listFilter }} } from "../models/{{ $listFilter | KebabCase }}";
{{- end }}
{{- if $filter }}
import { {{ $filter }} } from "../models/{{ $filter | KebabCase }}";
{{- end }}

@Injectable({
providedIn: "root",
Expand All @@ -13,9 +19,10 @@ export class {{ .Model.Name }}Service {
private _httpHelpersService: HttpHelpersService
) {}

get{{ .Model.Name }}List(filter: {{ .Model.Name }}ListFilter) {
{{- if $listFilter }}
get{{ .Model.Name | Plural }}(filter: {{ $listFilter }}) {
return this._httpClient.get<{{ .Model.Name }}[]>(
`/{{ .Model.Name }}/list`,
'/{{ .Model.Name | Plural | KebabCase }}',
{
params: this._httpHelpersService.getHttpParamsFromPlainObject(
filter,
Expand All @@ -24,20 +31,16 @@ export class {{ .Model.Name }}Service {
}
);
}

get{{ .Model.Name }}(filter: {{ .Model.Name }}Filter) {
return this._httpClient.get<{{ .Model.Name }}>(
`/{{ .Model.Name }}/`,
{
params: this._httpHelpersService.getHttpParamsFromPlainObject(
filter,
false
),
}
);
{{ end }}
{{- if $filter }}
{{- $filterModel := Model $filter}}
get{{ .Model.Name }}(filter: {{ $filter }}) {
{{- $pk := $filterModel.PKProp.Name }}
const { {{ $pk }} } = filter
return this._httpClient.get<{{ .Model.Name }}>(`/{{ .Model.Name | Plural | KebabCase }}/${ {{- $pk -}} }`);
}

{{ end }}
post{{ .Model.Name }}(model: {{ .Model.Name }}) {
return this._httpClient.post<number>(`/{{ .Model.Name }}/`, model);
return this._httpClient.post<number>('/{{ .Model.Name | Plural | KebabCase }}', model);
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module github.com/juanvillacortac/rosetta
go 1.17

require (
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/gertd/go-pluralize v0.2.0
github.com/iancoleman/strcase v0.2.0
github.com/yoheimuta/go-protoparser/v4 v4.5.4
gopkg.in/yaml.v2 v2.4.0
)
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
github.com/gertd/go-pluralize v0.2.0 h1:VzWNnxkUo3wkW2Nmp+3ieHSTQQ0LBHeSVxlKsQPQ+UY=
github.com/gertd/go-pluralize v0.2.0/go.mod h1:4ouO1Ndf/r7sZMorwp4Sbfw80lUni+sd+o3qJR8L9To=
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/yoheimuta/go-protoparser/v4 v4.5.4 h1:0/uEpkzBeSmC591cHIm+M7WIfHaP5FOyI4Y32Lef25I=
github.com/yoheimuta/go-protoparser/v4 v4.5.4/go.mod h1:AHNNnSWnb0UoL4QgHPiOAg2BniQceFscPI5X/BZNHl8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 5 additions & 3 deletions pkg/ast/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ func (models ModelMap) ModelDependencies(model interface{}) []*Model {
return deps
}

func GetNodeOption(r Node, optionName string) string {
func GetNodeOption(r Node, optionName string) *string {
o, ok := r.Options()[optionName]
if !ok {
return ""
return nil
} else {
o = strings.TrimPrefix(strings.TrimSuffix(o, "\""), "\"")
}
return strings.TrimPrefix(strings.TrimSuffix(o, "\""), "\"")
return &o
}
Loading

0 comments on commit b0f3e72

Please sign in to comment.