Skip to content

Commit 8b8736c

Browse files
authored
docs update (#319)
* update readme and spec * add custom xo docs * update
1 parent 76c6e35 commit 8b8736c

File tree

79 files changed

+309
-1114
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+309
-1114
lines changed

README.md

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,69 @@ bin/deploy-instrumentation
124124

125125
## Notes on code generation
126126

127-
Docs WIP:
127+
### `xo`
128+
129+
Db struct mappings can be extended with SQL column comments, joined with ` && `:
130+
- `properties`:`<p1>,<p2>,...`
131+
- `private`: exclude a field from JSON.
132+
- `not-required`: make a schema field not required.
133+
- `hidden`: exclude field from OpenAPI generation.
134+
- `refs-ignore`: generate a field whose constraints are ignored by the referenced table,
135+
i.e. no joins will be generated.
136+
- `share-ref-constraints`: for a FK column, it will generate the same M2O and M2M join fields the ref column has.
137+
- `type`: override the type annotation with an existing spec schema. Also allows
138+
complex JSON columns to be encoded and decoded thanks to `pgx`.
139+
- `cardinality`:`<O2O|M2O|M2M>` to generate/override joins explicitly. Only O2O is inferred.
140+
- `tags`: append literal struct tag strings.
141+
142+
### `rest` package
143+
144+
Structs in `models.spec.go` will be generated via `openapi-go` and replaced in
145+
the spec. Make sure they don't clash with existing `models` names or db tables.
146+
147+
### `models` package
148+
149+
Combines db codegen via a custom `xo` alongside spec schemas codegen for ease of use.
150+
151+
### CRUD + tests generation
152+
153+
See `project test.create-crud-gen`'s implementation for an example workflow to create a
154+
variety of implemented CRUD endpoints and relevant tests for a given database
155+
table.
156+
This is a one-off script per table.
157+
158+
### OpenAPI spec
159+
160+
#### Paths
161+
162+
Endpoints are exclusively managed through the spec and created/kept up to date
163+
in their respective files aggregated by `tags` (limited to one)
164+
via codegen and AST modifications. This ensures the server always implements the
165+
spec.
166+
167+
For endpoint implementation, only method bodies are to be modified.
168+
169+
#### Vendor extensions
170+
171+
Component schemas:
172+
- `x-gen-struct` generates a schema from a struct and keeps it up to date,
173+
which can be tagged with OpenAPI fields (via openapi-go).
174+
Possible structs to generate from are listed in `internal/codegen/structs.gen.go`
175+
indexed by `x-gen-struct`.
176+
Packages to generate structs from are added adhoc (currently rest and models packages only)
177+
Note that rest package structs are generated without prefix since they
178+
are the base of the spec - else prefix is the pascal cased package name.
179+
The value of `x-gen-struct` must match the schema name, to prevent duplicates and
180+
confusing definitions.
181+
To use a yet to be generated `rest` package struct in spec `paths`, simply use the `$ref`
182+
without creating any schema and it will be autogenerated later if it doesn't exist
183+
(only for `rest` package structs, since any other should not be used as request/response in the
184+
first place).
185+
- `x-spec-rest-type`: set to true to mark rest types (i.e. request, responses, query params...) that are manually defined in the spec instead of structs in `models.spec.go`.
186+
Path operations:
187+
- `x-required-scopes` defines required scopes for a request.
188+
- `x-required-role` defines the minimum required role for a request.
128189

129-
- Backend generation pipeline
130-
- Frontend generation pipeline
131-
- External tooling summary and upgrades
132190

133191
<!-- xo custom templates with cardinality, property comments for join and public model generation for embedding, schema from structs, spec sync -->
134192

@@ -138,14 +196,6 @@ Simplified:
138196

139197
![](.github/system-diagram.png)
140198

141-
## Changelog
142-
143-
- `v0.2`: discard `OpenAPITools/openapi-generator` with custom postgeneration for `deepmap/oapi-codegen`. Any
144-
change to fix broken generator functionality requires opening a PR or a disturbing
145-
amount of postgeneration code. Templates getting out of hand and also require
146-
a PR for custom functions. `oapi-codegen` much more extensible and idiomatic
147-
being already Go and properly maintained.
148-
149199
## Known issues
150200

151201
- Nested functions in `project`'s `x` functions will break automatic

bin/project

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -897,10 +897,6 @@ x.gen.client-server() {
897897
go-utils.find_all_types all_types "$REST_MODELS"
898898
rest_types_list=$(join_by "," ${all_types[*]})
899899

900-
# TODO should work for references that are not used in, e.g. Topics
901-
mapfile -t server_types < <(yq e '.components.schemas[] | select(.x-use-server-type == true) | key' $SPEC_PATH)
902-
server_types_list=$(join_by "," ${server_types[*]})
903-
904900
mapfile -t spec_rest_types < <(yq e '.components.schemas[] | select(.x-spec-rest-type == true) | key' $SPEC_PATH)
905901
spec_rest_types_list=$(join_by "," ${spec_rest_types[*]})
906902

@@ -972,7 +968,7 @@ EOF
972968
# TODO: we already have structs.gen.go indexed by "Models(models struct)" or plain rest type...
973969
# however this might be faster if the build is cached (templates unchanged). could construct internally as map set anyway
974970
oapi-codegen --config <(echo "$models_config") --types "$rest_types_list" "$SPEC_PATH" || err "Failed types generation" &
975-
oapi-codegen --config <(echo "$server_config") --spec-rest-types "$spec_rest_types_list" --models-pkg models --server-types "$server_types_list" --types "$rest_types_list" "$paths_file" || err "Failed server generation" &
971+
oapi-codegen --config <(echo "$server_config") --spec-rest-types "$spec_rest_types_list" --types "$rest_types_list" "$paths_file" || err "Failed server generation" &
976972
oapi-codegen --config <(echo "$test_client_config") --types "$rest_types_list" --spec-rest-types "$spec_rest_types_list" "$SPEC_PATH" || err "Failed client generation" &
977973
# not used now. may be useful for a cli at some point
978974
# oapi-codegen --config internal/client/oapi-codegen-client.yaml "$SPEC_PATH" || err "Failed client generation" &

frontend/src/client-validator/gen/dereferenced-schema.json

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3735,7 +3735,6 @@
37353735
"x-is-generated": true
37363736
}
37373737
],
3738-
"x-use-server-type": true,
37393738
"x-spec-rest-type": true
37403739
},
37413740
"Scopes": {
@@ -5866,8 +5865,7 @@
58665865
"filterMode"
58675866
]
58685867
}
5869-
],
5870-
"x-use-server-type": true
5868+
]
58715869
},
58725870
"Pagination": {
58735871
"type": "object",
@@ -5952,8 +5950,7 @@
59525950
"filterMode"
59535951
]
59545952
}
5955-
],
5956-
"x-use-server-type": true
5953+
]
59575954
},
59585955
"sort": {
59595956
"type": "string",
@@ -5962,8 +5959,7 @@
59625959
"desc"
59635960
]
59645961
}
5965-
},
5966-
"x-use-server-type": true
5962+
}
59675963
},
59685964
"PaginationItems": {
59695965
"type": "object",
@@ -6051,8 +6047,7 @@
60516047
"filterMode"
60526048
]
60536049
}
6054-
],
6055-
"x-use-server-type": true
6050+
]
60566051
},
60576052
"sort": {
60586053
"type": "string",
@@ -6061,8 +6056,7 @@
60616056
"desc"
60626057
]
60636058
}
6064-
},
6065-
"x-use-server-type": true
6059+
}
60666060
}
60676061
},
60686062
"PaginationCursor": {
@@ -6190,8 +6184,7 @@
61906184
"filterMode"
61916185
]
61926186
}
6193-
],
6194-
"x-use-server-type": true
6187+
]
61956188
},
61966189
"sort": {
61976190
"type": "string",
@@ -6200,8 +6193,7 @@
62006193
"desc"
62016194
]
62026195
}
6203-
},
6204-
"x-use-server-type": true
6196+
}
62056197
}
62066198
}
62076199
},
@@ -11130,7 +11122,6 @@
1113011122
"x-is-generated": true
1113111123
}
1113211124
],
11133-
"x-use-server-type": true,
1113411125
"x-spec-rest-type": true
1113511126
},
1113611127
"Scopes": {
@@ -13261,8 +13252,7 @@
1326113252
"filterMode"
1326213253
]
1326313254
}
13264-
],
13265-
"x-use-server-type": true
13255+
]
1326613256
},
1326713257
"Pagination": {
1326813258
"type": "object",
@@ -13347,8 +13337,7 @@
1334713337
"filterMode"
1334813338
]
1334913339
}
13350-
],
13351-
"x-use-server-type": true
13340+
]
1335213341
},
1335313342
"sort": {
1335413343
"type": "string",
@@ -13357,8 +13346,7 @@
1335713346
"desc"
1335813347
]
1335913348
}
13360-
},
13361-
"x-use-server-type": true
13349+
}
1336213350
},
1336313351
"PaginationItems": {
1336413352
"type": "object",
@@ -13446,8 +13434,7 @@
1344613434
"filterMode"
1344713435
]
1344813436
}
13449-
],
13450-
"x-use-server-type": true
13437+
]
1345113438
},
1345213439
"sort": {
1345313440
"type": "string",
@@ -13456,8 +13443,7 @@
1345613443
"desc"
1345713444
]
1345813445
}
13459-
},
13460-
"x-use-server-type": true
13446+
}
1346113447
}
1346213448
},
1346313449
"PaginationCursor": {
@@ -13585,8 +13571,7 @@
1358513571
"filterMode"
1358613572
]
1358713573
}
13588-
],
13589-
"x-use-server-type": true
13574+
]
1359013575
},
1359113576
"sort": {
1359213577
"type": "string",
@@ -13595,8 +13580,7 @@
1359513580
"desc"
1359613581
]
1359713582
}
13598-
},
13599-
"x-use-server-type": true
13583+
}
1360013584
}
1360113585
}
1360213586
},

frontend/src/client-validator/gen/schema.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,6 @@
14791479
"$ref": "#/definitions/DemoTwoWorkItemResponse"
14801480
}
14811481
],
1482-
"x-use-server-type": true,
14831482
"x-spec-rest-type": true
14841483
},
14851484
"Scopes": {
@@ -2089,8 +2088,7 @@
20892088
{
20902089
"$ref": "#/definitions/PaginationFilterArray"
20912090
}
2092-
],
2093-
"x-use-server-type": true
2091+
]
20942092
},
20952093
"Pagination": {
20962094
"type": "object",
@@ -2101,8 +2099,7 @@
21012099
"sort": {
21022100
"$ref": "#/definitions/Direction"
21032101
}
2104-
},
2105-
"x-use-server-type": true
2102+
}
21062103
},
21072104
"PaginationItems": {
21082105
"type": "object",

internal/repos/postgresql/gen/models/activity.xo.go

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/repos/postgresql/gen/models/cachedemotwoworkitem.xo.go

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/repos/postgresql/gen/models/cachedemoworkitem.xo.go

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/repos/postgresql/gen/models/demotwoworkitem.xo.go

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/repos/postgresql/gen/models/demoworkitem.xo.go

Lines changed: 0 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)