Compare commits
49 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e997946c56 | |||
|
|
dd4bb73a78 | ||
|
|
1005576814 | ||
|
|
10c15f6f8e | ||
|
|
af2f33d4f6 | ||
|
|
1db2c3ee54 | ||
|
|
7e8a4c5c45 | ||
|
|
f6fe3d4fda | ||
|
|
eeb2602bcf | ||
|
|
565eec0292 | ||
|
|
a98fad32d3 | ||
|
|
10f0c0bd31 | ||
|
|
01552145a8 | ||
|
|
09ed341b97 | ||
|
|
5cb30e6783 | ||
|
|
ee8fbbca09 | ||
|
|
b9b21ac44b | ||
|
|
d403f84b6c | ||
|
|
dfeeb8cd4c | ||
|
|
135866e5b0 | ||
|
|
eef88b5375 | ||
|
|
8b9717bb74 | ||
|
|
9a7a66192b | ||
|
|
df950143b4 | ||
|
|
038c563843 | ||
|
|
4733e05c58 | ||
|
|
c9496f0973 | ||
|
|
31dffa14ce | ||
|
|
7459501c8f | ||
|
|
bc9fd08f7e | ||
|
|
be1cee5d6a | ||
|
|
c91f9ba43c | ||
|
|
357000ef13 | ||
|
|
6ba89a8b9c | ||
|
|
d5af1d19b8 | ||
|
|
ce1ce722d5 | ||
|
|
fa192e10b8 | ||
|
|
7ae3296ef5 | ||
|
|
a6ba22b0b1 | ||
|
|
d49e94e412 | ||
|
|
ff8ee3693c | ||
|
|
d43d4d8a9f | ||
|
|
6f814b5d09 | ||
|
|
5d3df608e1 | ||
|
|
86af42bf4a | ||
|
|
5869998f6e | ||
|
|
cbdc0fd313 | ||
|
|
bada9508ef | ||
|
|
7bc4da97ab |
@@ -31,7 +31,7 @@ fn do() ! {
|
|||||||
mut cmd := Command{
|
mut cmd := Command{
|
||||||
name: 'hero'
|
name: 'hero'
|
||||||
description: 'Your HERO toolset.'
|
description: 'Your HERO toolset.'
|
||||||
version: '1.0.6'
|
version: '1.0.11'
|
||||||
}
|
}
|
||||||
|
|
||||||
// herocmds.cmd_run_add_flags(mut cmd)
|
// herocmds.cmd_run_add_flags(mut cmd)
|
||||||
|
|||||||
3
examples/baobab/generator/.gitignore
vendored
Normal file
3
examples/baobab/generator/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
methods.v
|
||||||
|
pet_store_actor
|
||||||
|
docs
|
||||||
9
examples/baobab/generator/README.md
Normal file
9
examples/baobab/generator/README.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Actor Generation Examples
|
||||||
|
|
||||||
|
## `generate_methods.vsh`
|
||||||
|
|
||||||
|
This example generates actor method prototypes from an actor specification.
|
||||||
|
|
||||||
|
## `generate_actor_module.vsh`
|
||||||
|
|
||||||
|
This example generates an entire actor module from an actor specification with the support for the specified interfaces.
|
||||||
23
examples/baobab/generator/generate_actor_module.vsh
Executable file
23
examples/baobab/generator/generate_actor_module.vsh
Executable file
@@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.generator
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
import os
|
||||||
|
|
||||||
|
const example_dir = os.dir(@FILE)
|
||||||
|
const openrpc_spec_path = os.join_path(example_dir, 'openrpc.json')
|
||||||
|
|
||||||
|
// the actor specification obtained from the OpenRPC Specification
|
||||||
|
openrpc_spec := openrpc.new(path: openrpc_spec_path)!
|
||||||
|
actor_spec := specification.from_openrpc(openrpc_spec)!
|
||||||
|
|
||||||
|
actor_module := generator.generate_actor_module(
|
||||||
|
actor_spec,
|
||||||
|
interfaces: [.openrpc]
|
||||||
|
)!
|
||||||
|
|
||||||
|
actor_module.write(example_dir,
|
||||||
|
format: true
|
||||||
|
overwrite: true
|
||||||
|
)!
|
||||||
19
examples/baobab/generator/generate_methods.vsh
Executable file
19
examples/baobab/generator/generate_methods.vsh
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.generator
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
import os
|
||||||
|
|
||||||
|
const example_dir = os.dir(@FILE)
|
||||||
|
const openrpc_spec_path = os.join_path(example_dir, 'openrpc.json')
|
||||||
|
|
||||||
|
// the actor specification obtained from the OpenRPC Specification
|
||||||
|
openrpc_spec := openrpc.new(path: openrpc_spec_path)!
|
||||||
|
actor_spec := specification.from_openrpc(openrpc_spec)!
|
||||||
|
|
||||||
|
methods_file := generator.generate_methods_file(actor_spec)!
|
||||||
|
methods_file.write(example_dir,
|
||||||
|
format: true
|
||||||
|
overwrite: true
|
||||||
|
)!
|
||||||
19
examples/baobab/generator/generate_openrpc_file.vsh
Executable file
19
examples/baobab/generator/generate_openrpc_file.vsh
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.generator
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
import os
|
||||||
|
|
||||||
|
const example_dir = os.dir(@FILE)
|
||||||
|
const openrpc_spec_path = os.join_path(example_dir, 'openrpc.json')
|
||||||
|
|
||||||
|
// the actor specification obtained from the OpenRPC Specification
|
||||||
|
openrpc_spec_ := openrpc.new(path: openrpc_spec_path)!
|
||||||
|
actor_spec := specification.from_openrpc(openrpc_spec_)!
|
||||||
|
openrpc_spec := actor_spec.to_openrpc()
|
||||||
|
|
||||||
|
openrpc_file := generator.generate_openrpc_file(openrpc_spec)!
|
||||||
|
openrpc_file.write(os.join_path(example_dir,'docs'),
|
||||||
|
overwrite: true
|
||||||
|
)!
|
||||||
2
examples/baobab/generator/openapi_e2e/.gitignore
vendored
Normal file
2
examples/baobab/generator/openapi_e2e/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
methods.v
|
||||||
|
meeting_scheduler_actor
|
||||||
26
examples/baobab/generator/openapi_e2e/generate_actor_module.vsh
Executable file
26
examples/baobab/generator/openapi_e2e/generate_actor_module.vsh
Executable file
@@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.generator
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
import os
|
||||||
|
|
||||||
|
const example_dir = os.dir(@FILE)
|
||||||
|
const openapi_spec_path = os.join_path(example_dir, 'openapi.json')
|
||||||
|
|
||||||
|
// the actor specification obtained from the OpenRPC Specification
|
||||||
|
openapi_spec := openapi.new(path: openapi_spec_path)!
|
||||||
|
actor_spec := specification.from_openapi(openapi_spec)!
|
||||||
|
|
||||||
|
actor_module := generator.generate_actor_module(
|
||||||
|
actor_spec,
|
||||||
|
interfaces: [.openapi, .http]
|
||||||
|
)!
|
||||||
|
|
||||||
|
actor_module.write(example_dir,
|
||||||
|
format: true
|
||||||
|
overwrite: true
|
||||||
|
compile: true
|
||||||
|
)!
|
||||||
|
|
||||||
|
os.execvp('bash', ['${example_dir}/meeting_scheduler_actor/scripts/run.sh'])!
|
||||||
311
examples/baobab/generator/openapi_e2e/openapi.json
Normal file
311
examples/baobab/generator/openapi_e2e/openapi.json
Normal file
@@ -0,0 +1,311 @@
|
|||||||
|
{
|
||||||
|
"openapi": "3.0.0",
|
||||||
|
"info": {
|
||||||
|
"title": "Meeting Scheduler",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "An API for managing meetings, availability, and scheduling."
|
||||||
|
},
|
||||||
|
"servers": [
|
||||||
|
{
|
||||||
|
"url": "http://localhost:8080/openapi/v1",
|
||||||
|
"description": "Production server"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "http://localhost:8081/openapi/v1",
|
||||||
|
"description": "Example server"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"paths": {
|
||||||
|
"/users": {
|
||||||
|
"get": {
|
||||||
|
"summary": "List all users",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "A list of users",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/components/schemas/User"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"example": [
|
||||||
|
{
|
||||||
|
"id": "1",
|
||||||
|
"name": "Alice",
|
||||||
|
"email": "alice@example.com"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "2",
|
||||||
|
"name": "Bob",
|
||||||
|
"email": "bob@example.com"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/users/{userId}": {
|
||||||
|
"get": {
|
||||||
|
"operationId": "get_user",
|
||||||
|
"summary": "Get user by ID",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "userId",
|
||||||
|
"in": "path",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "uint32"
|
||||||
|
},
|
||||||
|
"description": "The ID of the user",
|
||||||
|
"example": 1
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "User details",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/User"
|
||||||
|
},
|
||||||
|
"example": {
|
||||||
|
"id": "1",
|
||||||
|
"name": "Alice",
|
||||||
|
"email": "alice@example.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "User not found"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/events": {
|
||||||
|
"post": {
|
||||||
|
"summary": "Create an event",
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Event"
|
||||||
|
},
|
||||||
|
"example": {
|
||||||
|
"title": "Team Meeting",
|
||||||
|
"description": "Weekly sync",
|
||||||
|
"startTime": "2023-10-10T10:00:00Z",
|
||||||
|
"endTime": "2023-10-10T11:00:00Z",
|
||||||
|
"userId": "1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "Event created",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Event"
|
||||||
|
},
|
||||||
|
"example": {
|
||||||
|
"id": "101",
|
||||||
|
"title": "Team Meeting",
|
||||||
|
"description": "Weekly sync",
|
||||||
|
"startTime": "2023-10-10T10:00:00Z",
|
||||||
|
"endTime": "2023-10-10T11:00:00Z",
|
||||||
|
"userId": "1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/availability": {
|
||||||
|
"get": {
|
||||||
|
"summary": "Get availability for a user",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "userId",
|
||||||
|
"in": "query",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"description": "The ID of the user",
|
||||||
|
"example": "1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "date",
|
||||||
|
"in": "query",
|
||||||
|
"required": false,
|
||||||
|
"schema": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "date"
|
||||||
|
},
|
||||||
|
"description": "The date to check availability (YYYY-MM-DD)",
|
||||||
|
"example": "2023-10-10"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Availability details",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/components/schemas/TimeSlot"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"example": [
|
||||||
|
{
|
||||||
|
"startTime": "10:00:00",
|
||||||
|
"endTime": "11:00:00",
|
||||||
|
"available": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"startTime": "11:00:00",
|
||||||
|
"endTime": "12:00:00",
|
||||||
|
"available": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/bookings": {
|
||||||
|
"post": {
|
||||||
|
"summary": "Book a meeting",
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Booking"
|
||||||
|
},
|
||||||
|
"example": {
|
||||||
|
"userId": "1",
|
||||||
|
"eventId": "101",
|
||||||
|
"timeSlot": {
|
||||||
|
"startTime": "10:00:00",
|
||||||
|
"endTime": "11:00:00",
|
||||||
|
"available": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "Booking created",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Booking"
|
||||||
|
},
|
||||||
|
"example": {
|
||||||
|
"id": "5001",
|
||||||
|
"userId": "1",
|
||||||
|
"eventId": "101",
|
||||||
|
"timeSlot": {
|
||||||
|
"startTime": "10:00:00",
|
||||||
|
"endTime": "11:00:00",
|
||||||
|
"available": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"User": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"email": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "email"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Event": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"title": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"description": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"startTime": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "date-time"
|
||||||
|
},
|
||||||
|
"endTime": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "date-time"
|
||||||
|
},
|
||||||
|
"userId": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"TimeSlot": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"startTime": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "time"
|
||||||
|
},
|
||||||
|
"endTime": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "time"
|
||||||
|
},
|
||||||
|
"available": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Booking": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"userId": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"eventId": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"timeSlot": {
|
||||||
|
"$ref": "#/components/schemas/TimeSlot"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
132
examples/baobab/generator/openrpc.json
Normal file
132
examples/baobab/generator/openrpc.json
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
{
|
||||||
|
"openrpc": "1.0.0",
|
||||||
|
"info": {
|
||||||
|
"title": "PetStore",
|
||||||
|
"version": "1.0.0"
|
||||||
|
},
|
||||||
|
"methods": [
|
||||||
|
{
|
||||||
|
"name": "GetPets",
|
||||||
|
"description": "finds pets in the system that the user has access to by tags and within a limit",
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"name": "tags",
|
||||||
|
"description": "tags to filter by",
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "limit",
|
||||||
|
"description": "maximum number of results to return",
|
||||||
|
"schema": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"result": {
|
||||||
|
"name": "pet_list",
|
||||||
|
"description": "all pets from the system, that mathes the tags",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#\/components\/schemas\/Pet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "CreatePet",
|
||||||
|
"description": "creates a new pet in the store. Duplicates are allowed.",
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"name": "new_pet",
|
||||||
|
"description": "Pet to add to the store.",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#\/components\/schemas\/NewPet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"result": {
|
||||||
|
"name": "pet",
|
||||||
|
"description": "the newly created pet",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#\/components\/schemas\/Pet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "GetPetById",
|
||||||
|
"description": "gets a pet based on a single ID, if the user has access to the pet",
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"description": "ID of pet to fetch",
|
||||||
|
"schema": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"result": {
|
||||||
|
"name": "pet",
|
||||||
|
"description": "pet response",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#\/components\/schemas\/Pet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "DeletePetById",
|
||||||
|
"description": "deletes a single pet based on the ID supplied",
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"description": "ID of pet to delete",
|
||||||
|
"schema": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"result": {
|
||||||
|
"name": "pet",
|
||||||
|
"description": "pet deleted",
|
||||||
|
"schema": {
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"NewPet": {
|
||||||
|
"title": "NewPet",
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"tag": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Pet": {
|
||||||
|
"title": "Pet",
|
||||||
|
"description": "a pet struct that represents a pet",
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"description": "name of the pet",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"tag": {
|
||||||
|
"description": "a tag of the pet, helps finding pet",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"description": "unique indentifier",
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
examples/baobab/specification/README.md
Normal file
3
examples/baobab/specification/README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Actor Specification Examples
|
||||||
|
|
||||||
|
These examples show how `OpenRPC` and `OpenAPI` specifications can be translated back and forth into an `ActorSpecification`. This is an important step of actor generation as actor code is generated from actor specification.
|
||||||
346
examples/baobab/specification/openapi.json
Normal file
346
examples/baobab/specification/openapi.json
Normal file
@@ -0,0 +1,346 @@
|
|||||||
|
{
|
||||||
|
"openapi": "3.0.3",
|
||||||
|
"info": {
|
||||||
|
"title": "Pet Store API",
|
||||||
|
"description": "A sample API for a pet store",
|
||||||
|
"version": "1.0.0"
|
||||||
|
},
|
||||||
|
"servers": [
|
||||||
|
{
|
||||||
|
"url": "https://api.petstore.example.com/v1",
|
||||||
|
"description": "Production server"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://staging.petstore.example.com/v1",
|
||||||
|
"description": "Staging server"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"paths": {
|
||||||
|
"/pets": {
|
||||||
|
"get": {
|
||||||
|
"summary": "List all pets",
|
||||||
|
"operationId": "listPets",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "limit",
|
||||||
|
"in": "query",
|
||||||
|
"description": "Maximum number of pets to return",
|
||||||
|
"required": false,
|
||||||
|
"schema": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "A paginated list of pets",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Pets"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"400": {
|
||||||
|
"description": "Invalid request"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"post": {
|
||||||
|
"summary": "Create a new pet",
|
||||||
|
"operationId": "createPet",
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/NewPet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "Pet created",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Pet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"400": {
|
||||||
|
"description": "Invalid input"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/pets/{petId}": {
|
||||||
|
"get": {
|
||||||
|
"summary": "Get a pet by ID",
|
||||||
|
"operationId": "getPet",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "petId",
|
||||||
|
"in": "path",
|
||||||
|
"description": "ID of the pet to retrieve",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "A pet",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Pet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "Pet not found"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"summary": "Delete a pet by ID",
|
||||||
|
"operationId": "deletePet",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "petId",
|
||||||
|
"in": "path",
|
||||||
|
"description": "ID of the pet to delete",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"204": {
|
||||||
|
"description": "Pet deleted"
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "Pet not found"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/orders": {
|
||||||
|
"get": {
|
||||||
|
"summary": "List all orders",
|
||||||
|
"operationId": "listOrders",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "A list of orders",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/components/schemas/Order"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/orders/{orderId}": {
|
||||||
|
"get": {
|
||||||
|
"summary": "Get an order by ID",
|
||||||
|
"operationId": "getOrder",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "orderId",
|
||||||
|
"in": "path",
|
||||||
|
"description": "ID of the order to retrieve",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "An order",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Order"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "Order not found"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"delete": {
|
||||||
|
"summary": "Delete an order by ID",
|
||||||
|
"operationId": "deleteOrder",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "orderId",
|
||||||
|
"in": "path",
|
||||||
|
"description": "ID of the order to delete",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"204": {
|
||||||
|
"description": "Order deleted"
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "Order not found"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/users": {
|
||||||
|
"post": {
|
||||||
|
"summary": "Create a user",
|
||||||
|
"operationId": "createUser",
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/NewUser"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "User created",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/User"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"Pet": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["id", "name"],
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"tag": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"NewPet": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["name"],
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"tag": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Pets": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/components/schemas/Pet"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Order": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["id", "petId", "quantity", "shipDate"],
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64"
|
||||||
|
},
|
||||||
|
"petId": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64"
|
||||||
|
},
|
||||||
|
"quantity": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
},
|
||||||
|
"shipDate": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "date-time"
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["placed", "approved", "delivered"]
|
||||||
|
},
|
||||||
|
"complete": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"User": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["id", "username"],
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int64"
|
||||||
|
},
|
||||||
|
"username": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"email": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"phone": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"NewUser": {
|
||||||
|
"type": "object",
|
||||||
|
"required": ["username"],
|
||||||
|
"properties": {
|
||||||
|
"username": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"email": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"phone": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
examples/baobab/specification/openapi_to_specification.vsh
Executable file
13
examples/baobab/specification/openapi_to_specification.vsh
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
import os
|
||||||
|
|
||||||
|
const example_dir = os.dir(@FILE)
|
||||||
|
const openapi_spec_path = os.join_path(example_dir, 'openapi.json')
|
||||||
|
|
||||||
|
// the actor specification obtained from the OpenRPC Specification
|
||||||
|
openapi_spec := openapi.new(path: openapi_spec_path)!
|
||||||
|
actor_specification := specification.from_openapi(openapi_spec)!
|
||||||
|
println(actor_specification)
|
||||||
132
examples/baobab/specification/openrpc.json
Normal file
132
examples/baobab/specification/openrpc.json
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
{
|
||||||
|
"openrpc": "1.0.0",
|
||||||
|
"info": {
|
||||||
|
"title": "PetStore",
|
||||||
|
"version": "1.0.0"
|
||||||
|
},
|
||||||
|
"methods": [
|
||||||
|
{
|
||||||
|
"name": "GetPets",
|
||||||
|
"description": "finds pets in the system that the user has access to by tags and within a limit",
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"name": "tags",
|
||||||
|
"description": "tags to filter by",
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "limit",
|
||||||
|
"description": "maximum number of results to return",
|
||||||
|
"schema": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"result": {
|
||||||
|
"name": "pet_list",
|
||||||
|
"description": "all pets from the system, that mathes the tags",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#\/components\/schemas\/Pet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "CreatePet",
|
||||||
|
"description": "creates a new pet in the store. Duplicates are allowed.",
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"name": "new_pet",
|
||||||
|
"description": "Pet to add to the store.",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#\/components\/schemas\/NewPet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"result": {
|
||||||
|
"name": "pet",
|
||||||
|
"description": "the newly created pet",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#\/components\/schemas\/Pet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "GetPetById",
|
||||||
|
"description": "gets a pet based on a single ID, if the user has access to the pet",
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"description": "ID of pet to fetch",
|
||||||
|
"schema": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"result": {
|
||||||
|
"name": "pet",
|
||||||
|
"description": "pet response",
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#\/components\/schemas\/Pet"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "DeletePetById",
|
||||||
|
"description": "deletes a single pet based on the ID supplied",
|
||||||
|
"params": [
|
||||||
|
{
|
||||||
|
"name": "id",
|
||||||
|
"description": "ID of pet to delete",
|
||||||
|
"schema": {
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"result": {
|
||||||
|
"name": "pet",
|
||||||
|
"description": "pet deleted",
|
||||||
|
"schema": {
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"NewPet": {
|
||||||
|
"title": "NewPet",
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"tag": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Pet": {
|
||||||
|
"title": "Pet",
|
||||||
|
"description": "a pet struct that represents a pet",
|
||||||
|
"properties": {
|
||||||
|
"name": {
|
||||||
|
"description": "name of the pet",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"tag": {
|
||||||
|
"description": "a tag of the pet, helps finding pet",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"description": "unique indentifier",
|
||||||
|
"type": "integer"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
examples/baobab/specification/openrpc_to_specification.vsh
Executable file
13
examples/baobab/specification/openrpc_to_specification.vsh
Executable file
@@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
import os
|
||||||
|
|
||||||
|
const example_dir = os.dir(@FILE)
|
||||||
|
const openrpc_spec_path = os.join_path(example_dir, 'openrpc.json')
|
||||||
|
|
||||||
|
// the actor specification obtained from the OpenRPC Specification
|
||||||
|
openrpc_spec := openrpc.new(path: openrpc_spec_path)!
|
||||||
|
actor_specification := specification.from_openrpc(openrpc_spec)!
|
||||||
|
println(actor_specification)
|
||||||
107
examples/baobab/specification/specification_to_openapi.vsh
Executable file
107
examples/baobab/specification/specification_to_openapi.vsh
Executable file
@@ -0,0 +1,107 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import json
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
import os
|
||||||
|
|
||||||
|
const actor_specification = specification.ActorSpecification{
|
||||||
|
name: 'PetStore'
|
||||||
|
interfaces: [.openrpc]
|
||||||
|
methods: [
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'GetPets'
|
||||||
|
description: 'finds pets in the system that the user has access to by tags and within a limit'
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'tags'
|
||||||
|
description: 'tags to filter by'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'array'
|
||||||
|
items: jsonschema.Items(jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'limit'
|
||||||
|
description: 'maximum number of results to return'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet_list'
|
||||||
|
description: 'all pets from the system, that matches the tags'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'CreatePet'
|
||||||
|
description: 'creates a new pet in the store. Duplicates are allowed.'
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'new_pet'
|
||||||
|
description: 'Pet to add to the store.'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/NewPet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'the newly created pet'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'GetPetById'
|
||||||
|
description: 'gets a pet based on a single ID, if the user has access to the pet'
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'id'
|
||||||
|
description: 'ID of pet to fetch'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'pet response'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'DeletePetById'
|
||||||
|
description: 'deletes a single pet based on the ID supplied'
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'id'
|
||||||
|
description: 'ID of pet to delete'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'pet deleted'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'null'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
openapi_specification := actor_specification.to_openapi()
|
||||||
|
println(json.encode_pretty(openapi_specification))
|
||||||
109
examples/baobab/specification/specification_to_openrpc.vsh
Executable file
109
examples/baobab/specification/specification_to_openrpc.vsh
Executable file
@@ -0,0 +1,109 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import json
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.core.code
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
import os
|
||||||
|
|
||||||
|
const actor_specification = specification.ActorSpecification{
|
||||||
|
name: 'PetStore'
|
||||||
|
structure: code.Struct{}
|
||||||
|
interfaces: [.openrpc]
|
||||||
|
methods: [
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'GetPets'
|
||||||
|
description: 'finds pets in the system that the user has access to by tags and within a limit'
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'tags'
|
||||||
|
description: 'tags to filter by'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'array'
|
||||||
|
items: jsonschema.Items(jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'limit'
|
||||||
|
description: 'maximum number of results to return'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet_list'
|
||||||
|
description: 'all pets from the system, that matches the tags'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'CreatePet'
|
||||||
|
description: 'creates a new pet in the store. Duplicates are allowed.'
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'new_pet'
|
||||||
|
description: 'Pet to add to the store.'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/NewPet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'the newly created pet'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'GetPetById'
|
||||||
|
description: 'gets a pet based on a single ID, if the user has access to the pet'
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'id'
|
||||||
|
description: 'ID of pet to fetch'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'pet response'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'DeletePetById'
|
||||||
|
description: 'deletes a single pet based on the ID supplied'
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'id'
|
||||||
|
description: 'ID of pet to delete'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'pet deleted'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'null'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
openrpc_specification := actor_specification.to_openrpc()
|
||||||
|
println(json.encode_pretty(openrpc_specification))
|
||||||
@@ -7,7 +7,7 @@ import os
|
|||||||
const testpath3 = os.dir(@FILE) + '/../..'
|
const testpath3 = os.dir(@FILE) + '/../..'
|
||||||
|
|
||||||
// if we return True then it means the dir or file is processed
|
// if we return True then it means the dir or file is processed
|
||||||
fn filter_1(mut path pathlib.Path, mut params paramsparser.Params) !bool {
|
fn filter_1(mut path pathlib.Path) !bool {
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
if path.path.ends_with('.dSYM') {
|
if path.path.ends_with('.dSYM') {
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
module example_actor
|
module example_actor
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import freeflowuniverse.herolib.hero.baobab.actor { IActor, RunParams }
|
import freeflowuniverse.herolib.hero.baobab.stage {IActor, RunParams}
|
||||||
import freeflowuniverse.herolib.web.openapi
|
import freeflowuniverse.herolib.web.openapi
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -10,11 +10,13 @@ const openapi_spec_json = os.read_file(openapi_spec_path) or { panic(err) }
|
|||||||
const openapi_specification = openapi.json_decode(openapi_spec_json)!
|
const openapi_specification = openapi.json_decode(openapi_spec_json)!
|
||||||
|
|
||||||
struct ExampleActor {
|
struct ExampleActor {
|
||||||
actor.Actor
|
stage.Actor
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new() !ExampleActor {
|
fn new() !ExampleActor {
|
||||||
return ExampleActor{actor.new('example')}
|
return ExampleActor{
|
||||||
|
stage.new_actor('example')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run() ! {
|
pub fn run() ! {
|
||||||
|
|||||||
@@ -70,7 +70,6 @@ fn (mut actor Actor) listen() ! {
|
|||||||
|
|
||||||
// Handle method invocations
|
// Handle method invocations
|
||||||
fn (mut actor Actor) handle_method(cmd string, data string) !string {
|
fn (mut actor Actor) handle_method(cmd string, data string) !string {
|
||||||
println('debugzo received rpc ${cmd}:${data}')
|
|
||||||
param_anys := json2.raw_decode(data)!.arr()
|
param_anys := json2.raw_decode(data)!.arr()
|
||||||
match cmd {
|
match cmd {
|
||||||
'listPets' {
|
'listPets' {
|
||||||
@@ -108,47 +107,35 @@ fn (mut actor Actor) handle_method(cmd string, data string) !string {
|
|||||||
return json.encode(response)
|
return json.encode(response)
|
||||||
}
|
}
|
||||||
'deletePet' {
|
'deletePet' {
|
||||||
params := json.decode(map[string]int, data) or {
|
params := json.decode(map[string]int, data) or { return error('Invalid params: $err') }
|
||||||
return error('Invalid params: ${err}')
|
actor.data_store.delete_pet(params['petId']) or { return error('Pet not found: $err') }
|
||||||
}
|
return json.encode({'message': 'Pet deleted'})
|
||||||
actor.data_store.delete_pet(params['petId']) or {
|
|
||||||
return error('Pet not found: ${err}')
|
|
||||||
}
|
|
||||||
return json.encode({
|
|
||||||
'message': 'Pet deleted'
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
'listOrders' {
|
'listOrders' {
|
||||||
orders := actor.data_store.list_orders()
|
orders := actor.data_store.list_orders()
|
||||||
return json.encode(orders)
|
return json.encode(orders)
|
||||||
}
|
}
|
||||||
'getOrder' {
|
'getOrder' {
|
||||||
params := json.decode(map[string]int, data) or {
|
params := json.decode(map[string]int, data) or { return error('Invalid params: $err') }
|
||||||
return error('Invalid params: ${err}')
|
|
||||||
}
|
|
||||||
order := actor.data_store.get_order(params['orderId']) or {
|
order := actor.data_store.get_order(params['orderId']) or {
|
||||||
return error('Order not found: ${err}')
|
return error('Order not found: $err')
|
||||||
}
|
}
|
||||||
return json.encode(order)
|
return json.encode(order)
|
||||||
}
|
}
|
||||||
'deleteOrder' {
|
'deleteOrder' {
|
||||||
params := json.decode(map[string]int, data) or {
|
params := json.decode(map[string]int, data) or { return error('Invalid params: $err') }
|
||||||
return error('Invalid params: ${err}')
|
|
||||||
}
|
|
||||||
actor.data_store.delete_order(params['orderId']) or {
|
actor.data_store.delete_order(params['orderId']) or {
|
||||||
return error('Order not found: ${err}')
|
return error('Order not found: $err')
|
||||||
}
|
}
|
||||||
return json.encode({
|
return json.encode({'message': 'Order deleted'})
|
||||||
'message': 'Order deleted'
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
'createUser' {
|
'createUser' {
|
||||||
user := json.decode(NewUser, data) or { return error('Invalid user data: ${err}') }
|
user := json.decode(NewUser, data) or { return error('Invalid user data: $err') }
|
||||||
created_user := actor.data_store.create_user(user)
|
created_user := actor.data_store.create_user(user)
|
||||||
return json.encode(created_user)
|
return json.encode(created_user)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return error('Unknown method: ${cmd}')
|
return error('Unknown method: $cmd')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
examples/schemas/openapi/codegen/.gitignore
vendored
Normal file
1
examples/schemas/openapi/codegen/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
client_typescript
|
||||||
19
examples/schemas/openapi/codegen/calendar_ts_client.vsh
Executable file
19
examples/schemas/openapi/codegen/calendar_ts_client.vsh
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/env -S v -n -w -enable-globals run
|
||||||
|
|
||||||
|
// Calendar Typescript Client Generation Example
|
||||||
|
// This example demonstrates how to generate a typescript client
|
||||||
|
// from a given OpenAPI Specification using the `openapi/codegen` module.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi.codegen
|
||||||
|
|
||||||
|
const dir = os.dir(@FILE)
|
||||||
|
const specification = openapi.new(path: '${dir}/meeting_api.json') or {
|
||||||
|
panic('this should never happen ${err}')
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate typescript client folder and write it in dir
|
||||||
|
codegen.ts_client_folder(specification)!.write(dir, overwrite: true)!
|
||||||
|
|
||||||
|
|
||||||
245
examples/schemas/openapi/codegen/meeting_api.json
Normal file
245
examples/schemas/openapi/codegen/meeting_api.json
Normal file
@@ -0,0 +1,245 @@
|
|||||||
|
{
|
||||||
|
"openapi": "3.0.0",
|
||||||
|
"info": {
|
||||||
|
"title": "Meeting Scheduler API",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "An API for managing meetings, availability, and scheduling."
|
||||||
|
},
|
||||||
|
"servers": [
|
||||||
|
{
|
||||||
|
"url": "https://api.meetingscheduler.com/v1",
|
||||||
|
"description": "Production server"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://sandbox.api.meetingscheduler.com/v1",
|
||||||
|
"description": "Sandbox server"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"paths": {
|
||||||
|
"/users": {
|
||||||
|
"get": {
|
||||||
|
"summary": "List all users",
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "A list of users",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/components/schemas/User"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/users/{userId}": {
|
||||||
|
"get": {
|
||||||
|
"operationId": "get_user",
|
||||||
|
"summary": "Get user by ID",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "userId",
|
||||||
|
"in": "path",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"description": "The ID of the user"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "User details",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/User"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"404": {
|
||||||
|
"description": "User not found"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/events": {
|
||||||
|
"post": {
|
||||||
|
"summary": "Create an event",
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Event"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "Event created",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Event"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/availability": {
|
||||||
|
"get": {
|
||||||
|
"summary": "Get availability for a user",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "userId",
|
||||||
|
"in": "query",
|
||||||
|
"required": true,
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"description": "The ID of the user"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "date",
|
||||||
|
"in": "query",
|
||||||
|
"required": false,
|
||||||
|
"schema": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "date"
|
||||||
|
},
|
||||||
|
"description": "The date to check availability (YYYY-MM-DD)"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"description": "Availability details",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"$ref": "#/components/schemas/TimeSlot"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"/bookings": {
|
||||||
|
"post": {
|
||||||
|
"summary": "Book a meeting",
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Booking"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": "Booking created",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/Booking"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"components": {
|
||||||
|
"schemas": {
|
||||||
|
"User": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"name": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"email": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "email"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Event": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"title": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"description": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"startTime": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "date-time"
|
||||||
|
},
|
||||||
|
"endTime": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "date-time"
|
||||||
|
},
|
||||||
|
"userId": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"TimeSlot": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"startTime": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "time"
|
||||||
|
},
|
||||||
|
"endTime": {
|
||||||
|
"type": "string",
|
||||||
|
"format": "time"
|
||||||
|
},
|
||||||
|
"available": {
|
||||||
|
"type": "boolean"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Booking": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"userId": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"eventId": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"timeSlot": {
|
||||||
|
"$ref": "#/components/schemas/TimeSlot"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -4,7 +4,7 @@ set -e
|
|||||||
|
|
||||||
os_name="$(uname -s)"
|
os_name="$(uname -s)"
|
||||||
arch_name="$(uname -m)"
|
arch_name="$(uname -m)"
|
||||||
version='1.0.6'
|
version='1.0.11'
|
||||||
|
|
||||||
|
|
||||||
# Base URL for GitHub releases
|
# Base URL for GitHub releases
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
module actor
|
module actor
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import freeflowuniverse.herolib.core.redisclient
|
import freeflowuniverse.herolib.clients.redisclient
|
||||||
import freeflowuniverse.herolib.hero.baobab.action { ProcedureCall, ProcedureResponse }
|
import freeflowuniverse.herolib.baobab.action { ProcedureCall, ProcedureResponse }
|
||||||
|
|
||||||
// Processor struct for managing procedure calls
|
// Processor struct for managing procedure calls
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
@@ -14,7 +14,7 @@ pub mut:
|
|||||||
@[params]
|
@[params]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
pub:
|
pub:
|
||||||
timeout int // Timeout in seconds
|
timeout int = 60 // Timeout in seconds
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ClientConfig {
|
pub struct ClientConfig {
|
||||||
@@ -62,8 +62,6 @@ pub fn (mut p Client) call_to_action(action Procedure, params Params) !Procedure
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
println('resp data ${response_data}')
|
|
||||||
|
|
||||||
return ProcedureResponse{
|
return ProcedureResponse{
|
||||||
result: response_data
|
result: response_data
|
||||||
}
|
}
|
||||||
118
lib/baobab/generator/client_typescript.v
Normal file
118
lib/baobab/generator/client_typescript.v
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code {Folder, File}
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema.codegen { schema_to_struct }
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc.codegen as openrpc_codegen { content_descriptor_to_parameter }
|
||||||
|
import freeflowuniverse.herolib.baobab.specification {ActorSpecification, ActorMethod, BaseObject}
|
||||||
|
import net.http
|
||||||
|
|
||||||
|
// pub enum BaseObjectMethodType {
|
||||||
|
// new
|
||||||
|
// get
|
||||||
|
// set
|
||||||
|
// delete
|
||||||
|
// list
|
||||||
|
// other
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub struct BaseObjectMethod {
|
||||||
|
// pub:
|
||||||
|
// typ BaseObjectMethodType
|
||||||
|
// object string // the name of the base object
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn ts_client_get_fn(object string, params TSClientFunctionParams) string {
|
||||||
|
// name_snake := texttools.snake_case(object)
|
||||||
|
// name_pascal := texttools.name_fix_pascal(object)
|
||||||
|
// root := get_endpoint_root(params.endpoint)
|
||||||
|
|
||||||
|
// return "async get${name_pascal}(id: string): Promise<${name_pascal}> {\n return this.restClient.get<${name_pascal}>(`/${root}/${name_snake}/\${id}`);\n }"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn ts_client_set_fn(object string, params TSClientFunctionParams) string {
|
||||||
|
// name_snake := texttools.snake_case(object)
|
||||||
|
// name_pascal := texttools.name_fix_pascal(object)
|
||||||
|
// root := get_endpoint_root(params.endpoint)
|
||||||
|
|
||||||
|
// return "async set${name_pascal}(id: string, ${name_snake}: Partial<${name_pascal}>): Promise<${name_pascal}> {\n return this.restClient.put<${name_pascal}>(`/${root}/${name_snake}/\${id}`, ${name_snake});\n }"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn ts_client_delete_fn(object string, params TSClientFunctionParams) string {
|
||||||
|
// name_snake := texttools.snake_case(object)
|
||||||
|
// name_pascal := texttools.name_fix_pascal(object)
|
||||||
|
// root := get_endpoint_root(params.endpoint)
|
||||||
|
|
||||||
|
// return "async delete${name_pascal}(id: string): Promise<void> {\n return this.restClient.delete<void>(`/${root}/${name_snake}/\${id}`);\n }"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn ts_client_list_fn(object string, params TSClientFunctionParams) string {
|
||||||
|
// name_snake := texttools.snake_case(object)
|
||||||
|
// name_pascal := texttools.name_fix_pascal(object)
|
||||||
|
// root := get_endpoint_root(params.endpoint)
|
||||||
|
|
||||||
|
// return "async list${name_pascal}(): Promise<${name_pascal}[]> {\n return this.restClient.get<${name_pascal}[]>(`/${root}/${name_snake}`);\n }"
|
||||||
|
// }
|
||||||
|
|
||||||
|
fn get_endpoint_root(root string) string {
|
||||||
|
return if root == '' {
|
||||||
|
''
|
||||||
|
} else {
|
||||||
|
"/${root.trim('/')}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// // generates a Base Object's `create` method
|
||||||
|
// pub fn ts_client_new_fn(object string, params TSClientFunctionParams) string {
|
||||||
|
// name_snake := texttools.snake_case(object)
|
||||||
|
// name_pascal := texttools.name_fix_pascal(object)
|
||||||
|
// root := get_endpoint_root(params.endpoint)
|
||||||
|
|
||||||
|
// return "async create${name_snake}(object: Omit<${name_pascal}, 'id'>): Promise<${name_pascal}> {
|
||||||
|
// return this.restClient.post<${name_pascal}>('${root}/${name_snake}', board);
|
||||||
|
// }"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn ts_client_get_fn(object string, params TSClientFunctionParams) string {
|
||||||
|
// name_snake := texttools.snake_case(object)
|
||||||
|
// name_pascal := texttools.name_fix_pascal(object)
|
||||||
|
// root := get_endpoint_root(params.endpoint)
|
||||||
|
|
||||||
|
// return "async get${name_pascal}(id: string): Promise<${name_pascal}> {\n return this.restClient.get<${name_pascal}>(`/${root}/${name_snake}/\${id}`);\n }"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn ts_client_set_fn(object string, params TSClientFunctionParams) string {
|
||||||
|
// name_snake := texttools.snake_case(object)
|
||||||
|
// name_pascal := texttools.name_fix_pascal(object)
|
||||||
|
// root := get_endpoint_root(params.endpoint)
|
||||||
|
|
||||||
|
// return "async set${name_pascal}(id: string, ${name_snake}: Partial<${name_pascal}>): Promise<${name_pascal}> {\n return this.restClient.put<${name_pascal}>(`/${root}/${name_snake}/\${id}`, ${name_snake});\n }"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn ts_client_delete_fn(object string, params TSClientFunctionParams) string {
|
||||||
|
// name_snake := texttools.snake_case(object)
|
||||||
|
// name_pascal := texttools.name_fix_pascal(object)
|
||||||
|
// root := get_endpoint_root(params.endpoint)
|
||||||
|
|
||||||
|
// return "async delete${name_pascal}(id: string): Promise<void> {\n return this.restClient.delete<void>(`/${root}/${name_snake}/\${id}`);\n }"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn ts_client_list_fn(object string, params TSClientFunctionParams) string {
|
||||||
|
// name_snake := texttools.snake_case(object)
|
||||||
|
// name_pascal := texttools.name_fix_pascal(object)
|
||||||
|
// root := get_endpoint_root(params.endpoint)
|
||||||
|
|
||||||
|
// return "async list${name_pascal}(): Promise<${name_pascal}[]> {\n return this.restClient.get<${name_pascal}[]>(`/${root}/${name_snake}`);\n }"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generates a function prototype given an `ActorMethod`
|
||||||
|
// pub fn ts_client_fn_prototype(method ActorMethod) string {
|
||||||
|
// name := texttools.name_fix_pascal(method.name)
|
||||||
|
// params := method.parameters
|
||||||
|
// .map(content_descriptor_to_parameter(it) or {panic(err)})
|
||||||
|
// .map(it.typescript())
|
||||||
|
// .join(', ')
|
||||||
|
|
||||||
|
// return_type := content_descriptor_to_parameter(method.result) or {panic(err)}.typ.typescript()
|
||||||
|
// return 'async ${name}(${params}): Promise<${return_type}> {}'
|
||||||
|
// }
|
||||||
202
lib/baobab/generator/client_typescript_test.v
Normal file
202
lib/baobab/generator/client_typescript_test.v
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import x.json2 as json
|
||||||
|
import arrays
|
||||||
|
import freeflowuniverse.herolib.core.code
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema
|
||||||
|
|
||||||
|
const specification = specification.ActorSpecification{
|
||||||
|
name: 'Pet Store'
|
||||||
|
description: 'A sample API for a pet store'
|
||||||
|
structure: code.Struct{}
|
||||||
|
interfaces: [.openapi]
|
||||||
|
methods: [
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'listPets'
|
||||||
|
summary: 'List all pets'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
params: [
|
||||||
|
openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example limit'
|
||||||
|
description: 'Example Maximum number of pets to return'
|
||||||
|
value: 10
|
||||||
|
})
|
||||||
|
]
|
||||||
|
result: openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example response'
|
||||||
|
value: json.raw_decode('[
|
||||||
|
{"id": 1, "name": "Fluffy", "tag": "dog"},
|
||||||
|
{"id": 2, "name": "Whiskers", "tag": "cat"}
|
||||||
|
]')!
|
||||||
|
})
|
||||||
|
}
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'limit'
|
||||||
|
summary: 'Maximum number of pets to return'
|
||||||
|
description: 'Maximum number of pets to return'
|
||||||
|
required: false
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
...jsonschema.schema_u32,
|
||||||
|
example: 10
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pets'
|
||||||
|
description: 'A paged array of pets'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'array'
|
||||||
|
items: jsonschema.Items(jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
id: 'pet'
|
||||||
|
title: 'Pet'
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 400
|
||||||
|
message: 'Invalid request'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'createPet'
|
||||||
|
summary: 'Create a new pet'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
result: openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example response'
|
||||||
|
value: '[]'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 400
|
||||||
|
message: 'Invalid input'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'getPet'
|
||||||
|
summary: 'Get a pet by ID'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
params: [
|
||||||
|
openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example petId'
|
||||||
|
description: 'Example ID of the pet to retrieve'
|
||||||
|
value: 1
|
||||||
|
})
|
||||||
|
]
|
||||||
|
result: openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example response'
|
||||||
|
value: json.raw_decode('{"id": 1, "name": "Fluffy", "tag": "dog"}')!
|
||||||
|
})
|
||||||
|
}
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'petId'
|
||||||
|
summary: 'ID of the pet to retrieve'
|
||||||
|
description: 'ID of the pet to retrieve'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
...jsonschema.schema_u32,
|
||||||
|
format:'uint32'
|
||||||
|
example: 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 404
|
||||||
|
message: 'Pet not found'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'deletePet'
|
||||||
|
summary: 'Delete a pet by ID'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
params: [
|
||||||
|
openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example petId'
|
||||||
|
description: 'Example ID of the pet to delete'
|
||||||
|
value: 1
|
||||||
|
})
|
||||||
|
]
|
||||||
|
}
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'petId'
|
||||||
|
summary: 'ID of the pet to delete'
|
||||||
|
description: 'ID of the pet to delete'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
...jsonschema.schema_u32,
|
||||||
|
example: 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 404
|
||||||
|
message: 'Pet not found'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
objects: [
|
||||||
|
specification.BaseObject{
|
||||||
|
schema: jsonschema.Schema{
|
||||||
|
title: 'Pet'
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.schema_u32,
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_typescript_client_folder() {
|
||||||
|
client := typescript_client_folder(specification)
|
||||||
|
}
|
||||||
192
lib/baobab/generator/generate_act.v
Normal file
192
lib/baobab/generator/generate_act.v
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { Result, Object, Param, Folder, IFile, VFile, CodeItem, File, Function, Import, Module, Struct, CustomCode }
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc {Example, ContentDescriptor}
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema.codegen {schemaref_to_type}
|
||||||
|
import freeflowuniverse.herolib.baobab.specification {ActorMethod, ActorSpecification}
|
||||||
|
|
||||||
|
fn generate_handle_example_file(spec ActorSpecification) !VFile {
|
||||||
|
mut items := []CodeItem{}
|
||||||
|
items << CustomCode{generate_handle_example_function(spec)}
|
||||||
|
for method in spec.methods {
|
||||||
|
items << generate_example_method_handle(spec.name, method)!
|
||||||
|
}
|
||||||
|
return VFile {
|
||||||
|
name: 'act'
|
||||||
|
imports: [
|
||||||
|
Import{mod:'freeflowuniverse.herolib.baobab.stage' types:['Action']}
|
||||||
|
Import{mod:'x.json2 as json'}
|
||||||
|
]
|
||||||
|
items: items
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_handle_file(spec ActorSpecification) !VFile {
|
||||||
|
mut items := []CodeItem{}
|
||||||
|
items << CustomCode{generate_handle_function(spec)}
|
||||||
|
for method in spec.methods {
|
||||||
|
items << generate_method_handle(spec.name, method)!
|
||||||
|
}
|
||||||
|
return VFile {
|
||||||
|
name: 'act'
|
||||||
|
imports: [
|
||||||
|
Import{mod:'freeflowuniverse.herolib.baobab.stage' types:['Action']}
|
||||||
|
Import{mod:'x.json2 as json'}
|
||||||
|
]
|
||||||
|
items: items
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_handle_function(spec ActorSpecification) string {
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(spec.name)
|
||||||
|
mut operation_handlers := []string{}
|
||||||
|
mut routes := []string{}
|
||||||
|
|
||||||
|
// Iterate over OpenAPI paths and operations
|
||||||
|
for method in spec.methods {
|
||||||
|
operation_id := method.name
|
||||||
|
params := method.parameters.map(it.name).join(', ')
|
||||||
|
|
||||||
|
// Generate route case
|
||||||
|
route := generate_route_case(operation_id, 'handle_${operation_id}')
|
||||||
|
routes << route
|
||||||
|
}
|
||||||
|
|
||||||
|
// Combine the generated handlers and main router into a single file
|
||||||
|
return [
|
||||||
|
'// AUTO-GENERATED FILE - DO NOT EDIT MANUALLY',
|
||||||
|
'',
|
||||||
|
'pub fn (mut actor ${actor_name_pascal}Actor) act(action Action) !Action {',
|
||||||
|
' return match action.name {',
|
||||||
|
routes.join('\n'),
|
||||||
|
' else {',
|
||||||
|
' return error("Unknown operation: \${action.name}")',
|
||||||
|
' }',
|
||||||
|
' }',
|
||||||
|
'}',
|
||||||
|
].join('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_handle_example_function(spec ActorSpecification) string {
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(spec.name)
|
||||||
|
mut operation_handlers := []string{}
|
||||||
|
mut routes := []string{}
|
||||||
|
|
||||||
|
// Iterate over OpenAPI paths and operations
|
||||||
|
for method in spec.methods {
|
||||||
|
operation_id := method.name
|
||||||
|
params := method.parameters.map(it.name).join(', ')
|
||||||
|
|
||||||
|
// Generate route case
|
||||||
|
route := generate_route_case(operation_id, 'handle_${operation_id}_example')
|
||||||
|
routes << route
|
||||||
|
}
|
||||||
|
|
||||||
|
// Combine the generated handlers and main router into a single file
|
||||||
|
return [
|
||||||
|
'// AUTO-GENERATED FILE - DO NOT EDIT MANUALLY',
|
||||||
|
'',
|
||||||
|
'pub fn (mut actor ${actor_name_pascal}Actor) act(action Action) !Action {',
|
||||||
|
' return match action.name {',
|
||||||
|
routes.join('\n'),
|
||||||
|
' else {',
|
||||||
|
' return error("Unknown operation: \${action.name}")',
|
||||||
|
' }',
|
||||||
|
' }',
|
||||||
|
'}',
|
||||||
|
].join('\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_method_handle(actor_name string, method ActorMethod) !Function {
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(actor_name)
|
||||||
|
name_fixed := texttools.snake_case(method.name)
|
||||||
|
mut body := ''
|
||||||
|
if method.parameters.len == 1 {
|
||||||
|
param := method.parameters[0]
|
||||||
|
param_name := texttools.snake_case(param.name)
|
||||||
|
decode_stmt := generate_decode_stmt('action.params', param)!
|
||||||
|
body += '${param_name} := ${decode_stmt}\n'
|
||||||
|
}
|
||||||
|
if method.parameters.len > 1 {
|
||||||
|
body += 'params_arr := json.raw_decode(action.params)!.arr()\n'
|
||||||
|
for i, param in method.parameters {
|
||||||
|
param_name := texttools.snake_case(param.name)
|
||||||
|
decode_stmt := generate_decode_stmt('params_arr[${i}]', param)!
|
||||||
|
body += '${param_name} := ${decode_stmt}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
call_stmt := generate_call_stmt(method)!
|
||||||
|
body += '${call_stmt}\n'
|
||||||
|
body += '${generate_return_stmt(method)!}\n'
|
||||||
|
return Function {
|
||||||
|
name: 'handle_${name_fixed}'
|
||||||
|
description: '// Handler for ${name_fixed}\n'
|
||||||
|
receiver: Param{name: 'actor', mutable: true, typ: Object{'${actor_name_pascal}Actor'}}
|
||||||
|
params: [Param{name: 'action', typ: Object{'Action'}}]
|
||||||
|
result: Param{typ: Result{Object{'Action'}}}
|
||||||
|
body: body
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn method_is_void(method ActorMethod) !bool {
|
||||||
|
return schemaref_to_type(method.result.schema).vgen().trim_space() == ''
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_example_method_handle(actor_name string, method ActorMethod) !Function {
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(actor_name)
|
||||||
|
name_fixed := texttools.snake_case(method.name)
|
||||||
|
body := if !method_is_void(method)! {
|
||||||
|
if method.example.result is Example {
|
||||||
|
'return Action{...action, result: json.encode(\'${method.example.result.value}\')}'
|
||||||
|
} else {
|
||||||
|
"return action"
|
||||||
|
}
|
||||||
|
} else { "return action" }
|
||||||
|
return Function {
|
||||||
|
name: 'handle_${name_fixed}_example'
|
||||||
|
description: '// Handler for ${name_fixed}\n'
|
||||||
|
receiver: Param{name: 'actor', mutable: true, typ: Object{'${actor_name_pascal}Actor'}}
|
||||||
|
params: [Param{name: 'action', typ: Object{'Action'}}]
|
||||||
|
result: Param{typ: Result{Object{'Action'}}}
|
||||||
|
body: body
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_call_stmt(method ActorMethod) !string {
|
||||||
|
mut call_stmt := if schemaref_to_type(method.result.schema).vgen().trim_space() != '' {
|
||||||
|
'${texttools.snake_case(method.result.name)} := '
|
||||||
|
} else {''}
|
||||||
|
name_fixed := texttools.snake_case(method.name)
|
||||||
|
param_names := method.parameters.map(texttools.snake_case(it.name))
|
||||||
|
call_stmt += 'actor.${name_fixed}(${param_names.join(", ")})!'
|
||||||
|
return call_stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_return_stmt(method ActorMethod) !string {
|
||||||
|
if schemaref_to_type(method.result.schema).vgen().trim_space() != '' {
|
||||||
|
return 'return Action{...action, result: json.encode(${texttools.snake_case(method.result.name)})}'
|
||||||
|
}
|
||||||
|
return "return action"
|
||||||
|
}
|
||||||
|
|
||||||
|
// generates decode statement for variable with given name
|
||||||
|
fn generate_decode_stmt(name string, param ContentDescriptor) !string {
|
||||||
|
param_type := schemaref_to_type(param.schema)
|
||||||
|
if param_type is Object {
|
||||||
|
return 'json.decode[${schemaref_to_type(param.schema).vgen()}](${name})!'
|
||||||
|
}
|
||||||
|
// else if param.schema.typ == 'array' {
|
||||||
|
// return 'json2.decode[${schemaref_to_type(param.schema)!.vgen()}](${name})'
|
||||||
|
// }
|
||||||
|
param_symbol := param_type.vgen()
|
||||||
|
return if param_symbol == 'string' {
|
||||||
|
'${name}.str()'
|
||||||
|
} else {'${name}.${param_type.vgen()}()'}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to generate a case block for the main router
|
||||||
|
fn generate_route_case(case string, handler_name string) string {
|
||||||
|
name_fixed := texttools.snake_case(handler_name)
|
||||||
|
return "'${texttools.snake_case(case)}' {actor.${name_fixed}(action)}"
|
||||||
|
}
|
||||||
239
lib/baobab/generator/generate_actor.v
Normal file
239
lib/baobab/generator/generate_actor.v
Normal file
@@ -0,0 +1,239 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { Folder, IFolder, IFile, VFile, CodeItem, File, Function, Import, Module, Struct, CustomCode }
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
import freeflowuniverse.herolib.baobab.specification {ActorMethod, ActorSpecification, ActorInterface}
|
||||||
|
import json
|
||||||
|
|
||||||
|
@[params]
|
||||||
|
pub struct Params {
|
||||||
|
pub:
|
||||||
|
interfaces []ActorInterface // the interfaces to be supported
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_actor_module(spec ActorSpecification, params Params) !Module {
|
||||||
|
mut files := []IFile{}
|
||||||
|
mut folders := []IFolder{}
|
||||||
|
|
||||||
|
files = [
|
||||||
|
generate_readme_file(spec)!,
|
||||||
|
generate_actor_file(spec)!,
|
||||||
|
generate_actor_test_file(spec)!,
|
||||||
|
generate_specs_file(spec.name, params.interfaces)!,
|
||||||
|
generate_handle_file(spec)!,
|
||||||
|
generate_methods_file(spec)!
|
||||||
|
generate_client_file(spec)!
|
||||||
|
generate_model_file(spec)!
|
||||||
|
]
|
||||||
|
|
||||||
|
mut docs_files := []IFile{}
|
||||||
|
|
||||||
|
// generate code files for supported interfaces
|
||||||
|
for iface in params.interfaces {
|
||||||
|
match iface {
|
||||||
|
.openrpc {
|
||||||
|
// convert actor spec to openrpc spec
|
||||||
|
openrpc_spec := spec.to_openrpc()
|
||||||
|
|
||||||
|
// generate openrpc code files
|
||||||
|
// files << generate_openrpc_client_file(openrpc_spec)!
|
||||||
|
// files << generate_openrpc_client_test_file(openrpc_spec)!
|
||||||
|
iface_file, iface_test_file := generate_openrpc_interface_files(params.interfaces)
|
||||||
|
files << iface_file
|
||||||
|
files << iface_test_file
|
||||||
|
|
||||||
|
// add openrpc.json to docs
|
||||||
|
// TODO
|
||||||
|
docs_files << generate_openrpc_file(openrpc_spec)!
|
||||||
|
}
|
||||||
|
.openapi {
|
||||||
|
// convert actor spec to openrpc spec
|
||||||
|
openapi_spec_raw := spec.to_openapi()
|
||||||
|
docs_files << generate_openapi_file(openapi_spec_raw)!
|
||||||
|
|
||||||
|
openapi_spec := openapi.process(openapi_spec_raw)!
|
||||||
|
// generate openrpc code files
|
||||||
|
iface_file, iface_test_file := generate_openapi_interface_files(params.interfaces)
|
||||||
|
files << iface_file
|
||||||
|
files << iface_test_file
|
||||||
|
|
||||||
|
// add openapi.json to docs
|
||||||
|
folders << generate_openapi_ts_client(openapi_spec)!
|
||||||
|
}
|
||||||
|
.http {
|
||||||
|
// interfaces that have http controllers
|
||||||
|
controllers := params.interfaces.filter(it == .openrpc || it == .openapi)
|
||||||
|
// generate openrpc code files
|
||||||
|
iface_file, iface_test_file := generate_http_interface_files(controllers)
|
||||||
|
files << iface_file
|
||||||
|
files << iface_test_file
|
||||||
|
}
|
||||||
|
.command {
|
||||||
|
files << generate_command_file(spec)!
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return error('unsupported interface ${iface}')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// folder with docs
|
||||||
|
folders << Folder {
|
||||||
|
name: 'docs'
|
||||||
|
files: docs_files
|
||||||
|
}
|
||||||
|
folders << generate_scripts_folder(spec.name, false)
|
||||||
|
folders << generate_examples_folder(spec, params)!
|
||||||
|
|
||||||
|
// create module with code files and docs folder
|
||||||
|
name_fixed := texttools.snake_case(spec.name)
|
||||||
|
return code.new_module(
|
||||||
|
name: '${name_fixed}_actor'
|
||||||
|
description: spec.description
|
||||||
|
files: files
|
||||||
|
folders: folders
|
||||||
|
in_src: true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_readme_file(spec ActorSpecification) !File {
|
||||||
|
return File{
|
||||||
|
name: 'README'
|
||||||
|
extension: 'md'
|
||||||
|
content: '# ${spec.name}\n${spec.description}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_actor_file(spec ActorSpecification) !VFile {
|
||||||
|
dollar := '$'
|
||||||
|
actor_name_snake := texttools.snake_case(spec.name)
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(spec.name)
|
||||||
|
actor_code := $tmpl('./templates/actor.v.template')
|
||||||
|
return VFile {
|
||||||
|
name: 'actor'
|
||||||
|
items: [CustomCode{actor_code}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_actor_example_file(spec ActorSpecification) !VFile {
|
||||||
|
dollar := '$'
|
||||||
|
actor_name_snake := texttools.snake_case(spec.name)
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(spec.name)
|
||||||
|
actor_code := $tmpl('./templates/actor_example.v.template')
|
||||||
|
return VFile {
|
||||||
|
name: 'actor_example'
|
||||||
|
items: [CustomCode{actor_code}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_actor_test_file(spec ActorSpecification) !VFile {
|
||||||
|
dollar := '$'
|
||||||
|
actor_name_snake := texttools.snake_case(spec.name)
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(spec.name)
|
||||||
|
actor_test_code := $tmpl('./templates/actor_test.v.template')
|
||||||
|
return VFile {
|
||||||
|
name: 'actor_test'
|
||||||
|
items: [CustomCode{actor_test_code}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_specs_file(name string, interfaces []ActorInterface) !VFile {
|
||||||
|
support_openrpc := ActorInterface.openrpc in interfaces
|
||||||
|
support_openapi := ActorInterface.openapi in interfaces
|
||||||
|
dollar := '$'
|
||||||
|
actor_name_snake := texttools.snake_case(name)
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(name)
|
||||||
|
actor_code := $tmpl('./templates/specifications.v.template')
|
||||||
|
return VFile {
|
||||||
|
name: 'specifications'
|
||||||
|
items: [CustomCode{actor_code}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_examples_folder(spec ActorSpecification, params Params) !Folder {
|
||||||
|
return Folder {
|
||||||
|
name: 'examples'
|
||||||
|
modules: [generate_example_actor_module(spec, params)!]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_example_actor_module(spec ActorSpecification, params Params) !Module {
|
||||||
|
mut files := []IFile{}
|
||||||
|
mut folders := []IFolder{}
|
||||||
|
|
||||||
|
files = [
|
||||||
|
generate_readme_file(spec)!,
|
||||||
|
generate_actor_example_file(spec)!,
|
||||||
|
generate_specs_file(spec.name, params.interfaces)!,
|
||||||
|
generate_handle_example_file(spec)!,
|
||||||
|
generate_example_client_file(spec)!
|
||||||
|
generate_model_file(spec)!
|
||||||
|
]
|
||||||
|
|
||||||
|
mut docs_files := []IFile{}
|
||||||
|
|
||||||
|
// generate code files for supported interfaces
|
||||||
|
for iface in params.interfaces {
|
||||||
|
match iface {
|
||||||
|
.openrpc {
|
||||||
|
// convert actor spec to openrpc spec
|
||||||
|
openrpc_spec := spec.to_openrpc()
|
||||||
|
|
||||||
|
// generate openrpc code files
|
||||||
|
// files << generate_openrpc_client_file(openrpc_spec)!
|
||||||
|
// files << generate_openrpc_client_test_file(openrpc_spec)!
|
||||||
|
iface_file, iface_test_file := generate_openrpc_interface_files(params.interfaces)
|
||||||
|
files << iface_file
|
||||||
|
files << iface_test_file
|
||||||
|
|
||||||
|
// add openrpc.json to docs
|
||||||
|
// TODO
|
||||||
|
docs_files << generate_openrpc_file(openrpc_spec)!
|
||||||
|
}
|
||||||
|
.openapi {
|
||||||
|
// convert actor spec to openrpc spec
|
||||||
|
openapi_spec := spec.to_openapi()
|
||||||
|
|
||||||
|
// generate openrpc code files
|
||||||
|
iface_file, iface_test_file := generate_openapi_interface_files(params.interfaces)
|
||||||
|
files << iface_file
|
||||||
|
files << iface_test_file
|
||||||
|
|
||||||
|
// add openapi.json to docs
|
||||||
|
docs_files << generate_openapi_file(openapi_spec)!
|
||||||
|
}
|
||||||
|
.http {
|
||||||
|
// interfaces that have http controllers
|
||||||
|
controllers := params.interfaces.filter(it == .openrpc || it == .openapi)
|
||||||
|
// generate openrpc code files
|
||||||
|
iface_file, iface_test_file := generate_http_interface_files(controllers)
|
||||||
|
files << iface_file
|
||||||
|
files << iface_test_file
|
||||||
|
}
|
||||||
|
.command {
|
||||||
|
files << generate_command_file(spec)!
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return error('unsupported interface ${iface}')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// folder with docs
|
||||||
|
folders << Folder {
|
||||||
|
name: 'docs'
|
||||||
|
files: docs_files
|
||||||
|
}
|
||||||
|
folders << generate_scripts_folder('example_${spec.name}', true)
|
||||||
|
|
||||||
|
// create module with code files and docs folder
|
||||||
|
name_fixed := texttools.snake_case(spec.name)
|
||||||
|
return code.new_module(
|
||||||
|
name: 'example_${name_fixed}_actor'
|
||||||
|
files: files
|
||||||
|
folders: folders
|
||||||
|
)
|
||||||
|
}
|
||||||
273
lib/baobab/generator/generate_actor_test.v
Normal file
273
lib/baobab/generator/generate_actor_test.v
Normal file
@@ -0,0 +1,273 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code
|
||||||
|
import freeflowuniverse.herolib.baobab.specification
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema
|
||||||
|
import os
|
||||||
|
import x.json2 as json {Any}
|
||||||
|
|
||||||
|
const actor_spec = specification.ActorSpecification{
|
||||||
|
name: 'Pet Store'
|
||||||
|
description: 'A sample API for a pet store'
|
||||||
|
structure: code.Struct{}
|
||||||
|
interfaces: [.openapi]
|
||||||
|
methods: [
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'listPets'
|
||||||
|
summary: 'List all pets'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
params: [
|
||||||
|
openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example limit'
|
||||||
|
description: 'Example Maximum number of pets to return'
|
||||||
|
value: 10
|
||||||
|
})
|
||||||
|
]
|
||||||
|
result: openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example response'
|
||||||
|
value: json.raw_decode('[
|
||||||
|
{"id": 1, "name": "Fluffy", "tag": "dog"},
|
||||||
|
{"id": 2, "name": "Whiskers", "tag": "cat"}
|
||||||
|
]')!
|
||||||
|
})
|
||||||
|
}
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'limit'
|
||||||
|
summary: 'Maximum number of pets to return'
|
||||||
|
description: 'Maximum number of pets to return'
|
||||||
|
required: false
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
...jsonschema.schema_u32,
|
||||||
|
example: 10
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pets'
|
||||||
|
description: 'A paged array of pets'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'array'
|
||||||
|
items: jsonschema.Items(jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
id: 'pet'
|
||||||
|
title: 'Pet'
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 400
|
||||||
|
message: 'Invalid request'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'newPet'
|
||||||
|
summary: 'Create a new pet'
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
id: 'pet'
|
||||||
|
title: 'Pet'
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
result: openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example response'
|
||||||
|
value: '[]'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'petId'
|
||||||
|
summary: 'ID of the created pet'
|
||||||
|
description: 'ID of the created pet'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
...jsonschema.schema_u32,
|
||||||
|
example: 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 400
|
||||||
|
message: 'Invalid input'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'getPet'
|
||||||
|
summary: 'Get a pet by ID'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
params: [
|
||||||
|
openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example petId'
|
||||||
|
description: 'Example ID of the pet to retrieve'
|
||||||
|
value: 1
|
||||||
|
})
|
||||||
|
]
|
||||||
|
result: openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example response'
|
||||||
|
value: json.raw_decode('{"id": 1, "name": "Fluffy", "tag": "dog"}')!
|
||||||
|
})
|
||||||
|
}
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'petId'
|
||||||
|
summary: 'ID of the pet to retrieve'
|
||||||
|
description: 'ID of the pet to retrieve'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
...jsonschema.schema_u32,
|
||||||
|
format:'uint32'
|
||||||
|
example: 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 404
|
||||||
|
message: 'Pet not found'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'deletePet'
|
||||||
|
summary: 'Delete a pet by ID'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
params: [
|
||||||
|
openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example petId'
|
||||||
|
description: 'Example ID of the pet to delete'
|
||||||
|
value: 1
|
||||||
|
})
|
||||||
|
]
|
||||||
|
}
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'petId'
|
||||||
|
summary: 'ID of the pet to delete'
|
||||||
|
description: 'ID of the pet to delete'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
...jsonschema.schema_u32,
|
||||||
|
example: 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 404
|
||||||
|
message: 'Pet not found'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
objects: [
|
||||||
|
specification.BaseObject{
|
||||||
|
schema: jsonschema.Schema{
|
||||||
|
title: 'Pet'
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.schema_u32,
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
const destination = '${os.dir(@FILE)}/testdata'
|
||||||
|
|
||||||
|
fn test_generate_plain_actor_module() {
|
||||||
|
// plain actor module without interfaces
|
||||||
|
actor_module := generate_actor_module(actor_spec)!
|
||||||
|
actor_module.write(destination,
|
||||||
|
format: true
|
||||||
|
overwrite: true
|
||||||
|
test: true
|
||||||
|
)!
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_generate_actor_module_with_openrpc_interface() {
|
||||||
|
// plain actor module without interfaces
|
||||||
|
actor_module := generate_actor_module(actor_spec, interfaces: [.openrpc])!
|
||||||
|
actor_module.write(destination,
|
||||||
|
format: true
|
||||||
|
overwrite: true
|
||||||
|
test: true
|
||||||
|
)!
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_generate_actor_module_with_openapi_interface() {
|
||||||
|
// plain actor module without interfaces
|
||||||
|
actor_module := generate_actor_module(actor_spec,
|
||||||
|
interfaces: [.openapi]
|
||||||
|
)!
|
||||||
|
actor_module.write(destination,
|
||||||
|
format: true
|
||||||
|
overwrite: true
|
||||||
|
test: true
|
||||||
|
)!
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_generate_actor_module_with_all_interfaces() {
|
||||||
|
// plain actor module without interfaces
|
||||||
|
actor_module := generate_actor_module(actor_spec,
|
||||||
|
interfaces: [.openapi, .openrpc, .http]
|
||||||
|
)!
|
||||||
|
actor_module.write(destination,
|
||||||
|
format: true
|
||||||
|
overwrite: true
|
||||||
|
test: true
|
||||||
|
)!
|
||||||
|
}
|
||||||
135
lib/baobab/generator/generate_clients.v
Normal file
135
lib/baobab/generator/generate_clients.v
Normal file
@@ -0,0 +1,135 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { Param, Folder, IFile, VFile, CodeItem, File, Function, Import, Module, Struct, CustomCode, Result }
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema.codegen as jsonschema_codegen {schemaref_to_type}
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc.codegen {content_descriptor_to_parameter}
|
||||||
|
import freeflowuniverse.herolib.baobab.specification {ActorMethod, ActorSpecification}
|
||||||
|
|
||||||
|
pub fn generate_client_file(spec ActorSpecification) !VFile {
|
||||||
|
actor_name_snake := texttools.snake_case(spec.name)
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(spec.name)
|
||||||
|
|
||||||
|
mut items := []CodeItem{}
|
||||||
|
|
||||||
|
items << CustomCode {'
|
||||||
|
pub struct Client {
|
||||||
|
stage.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_client() !Client {
|
||||||
|
mut redis := redisclient.new(\'localhost:6379\')!
|
||||||
|
mut rpc_q := redis.rpc_get(\'actor_\${name}\')
|
||||||
|
return Client{
|
||||||
|
rpc: rpc_q
|
||||||
|
}
|
||||||
|
}'}
|
||||||
|
|
||||||
|
for method in spec.methods {
|
||||||
|
items << generate_client_method(method)!
|
||||||
|
}
|
||||||
|
|
||||||
|
return VFile {
|
||||||
|
imports: [
|
||||||
|
Import{
|
||||||
|
mod: 'freeflowuniverse.herolib.baobab.stage'
|
||||||
|
},
|
||||||
|
Import{
|
||||||
|
mod: 'freeflowuniverse.herolib.core.redisclient'
|
||||||
|
},
|
||||||
|
Import{
|
||||||
|
mod: 'x.json2 as json'
|
||||||
|
types: ['Any']
|
||||||
|
}
|
||||||
|
]
|
||||||
|
name: 'client'
|
||||||
|
items: items
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_example_client_file(spec ActorSpecification) !VFile {
|
||||||
|
actor_name_snake := texttools.snake_case(spec.name)
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(spec.name)
|
||||||
|
|
||||||
|
mut items := []CodeItem{}
|
||||||
|
|
||||||
|
items << CustomCode {'
|
||||||
|
pub struct Client {
|
||||||
|
stage.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_client() !Client {
|
||||||
|
mut redis := redisclient.new(\'localhost:6379\')!
|
||||||
|
mut rpc_q := redis.rpc_get(\'actor_example_\${name}\')
|
||||||
|
return Client{
|
||||||
|
rpc: rpc_q
|
||||||
|
}
|
||||||
|
}'}
|
||||||
|
|
||||||
|
for method in spec.methods {
|
||||||
|
items << generate_client_method(method)!
|
||||||
|
}
|
||||||
|
|
||||||
|
return VFile {
|
||||||
|
imports: [
|
||||||
|
Import{
|
||||||
|
mod: 'freeflowuniverse.herolib.baobab.stage'
|
||||||
|
},
|
||||||
|
Import{
|
||||||
|
mod: 'freeflowuniverse.herolib.core.redisclient'
|
||||||
|
},
|
||||||
|
Import{
|
||||||
|
mod: 'x.json2 as json'
|
||||||
|
types: ['Any']
|
||||||
|
}
|
||||||
|
]
|
||||||
|
name: 'client'
|
||||||
|
items: items
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pub fn generate_client_method(method ActorMethod) !Function {
|
||||||
|
name_fixed := texttools.snake_case(method.name)
|
||||||
|
|
||||||
|
call_params := if method.parameters.len > 0 {
|
||||||
|
method.parameters.map(texttools.snake_case(it.name)).map('Any(${it})').join(', ')
|
||||||
|
} else {''}
|
||||||
|
|
||||||
|
params_stmt := if method.parameters.len == 0 {
|
||||||
|
''
|
||||||
|
} else if method.parameters.len == 1 {
|
||||||
|
'params := json.encode(${texttools.snake_case(method.parameters[0].name)})'
|
||||||
|
} else {
|
||||||
|
'mut params_arr := []Any{}
|
||||||
|
params_arr = [${call_params}]
|
||||||
|
params := json.encode(params_arr.str())
|
||||||
|
'
|
||||||
|
}
|
||||||
|
|
||||||
|
mut client_call_stmt := "action := client.call_to_action(
|
||||||
|
name: '${name_fixed}'"
|
||||||
|
|
||||||
|
if params_stmt != '' {
|
||||||
|
client_call_stmt += 'params: params'
|
||||||
|
}
|
||||||
|
client_call_stmt += ')!'
|
||||||
|
|
||||||
|
result_type := schemaref_to_type(method.result.schema).vgen().trim_space()
|
||||||
|
result_stmt := if result_type == '' {
|
||||||
|
''
|
||||||
|
} else {
|
||||||
|
"return json.decode[${result_type}](action.result)!"
|
||||||
|
}
|
||||||
|
result_param := content_descriptor_to_parameter(method.result)!
|
||||||
|
return Function {
|
||||||
|
receiver: code.new_param(v: 'mut client Client')!
|
||||||
|
result: Param{...result_param, typ: Result{result_param.typ}}
|
||||||
|
name: name_fixed
|
||||||
|
body: '${params_stmt}\n${client_call_stmt}\n${result_stmt}'
|
||||||
|
summary: method.summary
|
||||||
|
description: method.description
|
||||||
|
params: method.parameters.map(content_descriptor_to_parameter(it)!)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
module generator
|
module generator
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.codemodel { CodeItem, CustomCode, Import, VFile }
|
import freeflowuniverse.herolib.core.code { Folder, IFile, VFile, CodeItem, File, Function, Import, Module, Struct, CustomCode }
|
||||||
import freeflowuniverse.herolib.core.texttools
|
import freeflowuniverse.herolib.core.texttools
|
||||||
import freeflowuniverse.herolib.hero.baobab.specification { ActorMethod, ActorSpecification }
|
import freeflowuniverse.herolib.baobab.specification {ActorMethod, ActorSpecification}
|
||||||
|
|
||||||
pub fn generate_command_file(spec ActorSpecification) !VFile {
|
pub fn generate_command_file(spec ActorSpecification) !VFile {
|
||||||
mut items := []CodeItem{}
|
mut items := []CodeItem{}
|
||||||
@@ -26,14 +26,13 @@ pub fn generate_command_file(spec ActorSpecification) !VFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_cmd_function(spec ActorSpecification) string {
|
pub fn generate_cmd_function(spec ActorSpecification) string {
|
||||||
actor_name_snake := texttools.name_fix_snake(spec.name)
|
actor_name_snake := texttools.snake_case(spec.name)
|
||||||
mut cmd_function := "
|
mut cmd_function := "
|
||||||
pub fn cmd() Command {
|
pub fn cmd() Command {
|
||||||
mut cmd := Command{
|
mut cmd := Command{
|
||||||
name: '${actor_name_snake}'
|
name: '${actor_name_snake}'
|
||||||
usage: ''
|
usage: ''
|
||||||
description: '${spec.description}'
|
description: '${spec.description}'
|
||||||
execute: cmd_execute
|
|
||||||
}
|
}
|
||||||
"
|
"
|
||||||
|
|
||||||
@@ -48,7 +47,7 @@ pub fn generate_cmd_function(spec ActorSpecification) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_method_cmd(method ActorMethod) string {
|
pub fn generate_method_cmd(method ActorMethod) string {
|
||||||
method_name_snake := texttools.name_fix_snake(method.name)
|
method_name_snake := texttools.snake_case(method.name)
|
||||||
return "
|
return "
|
||||||
mut cmd_${method_name_snake} := Command{
|
mut cmd_${method_name_snake} := Command{
|
||||||
sort_flags: true
|
sort_flags: true
|
||||||
@@ -63,16 +62,16 @@ pub fn generate_method_cmd_function(actor_name string, method ActorMethod) strin
|
|||||||
mut operation_handlers := []string{}
|
mut operation_handlers := []string{}
|
||||||
mut routes := []string{}
|
mut routes := []string{}
|
||||||
|
|
||||||
actor_name_snake := texttools.name_fix_snake(actor_name)
|
actor_name_snake := texttools.snake_case(actor_name)
|
||||||
method_name_snake := texttools.name_fix_snake(method.name)
|
method_name_snake := texttools.snake_case(method.name)
|
||||||
|
|
||||||
method_call := if method.func.result.typ.symbol == '' {
|
method_call := if method.result.name == '' {
|
||||||
'${actor_name_snake}.${method_name_snake}()!'
|
'${actor_name_snake}.${method_name_snake}()!'
|
||||||
} else {
|
} else {
|
||||||
'result := ${actor_name_snake}.${method_name_snake}()!'
|
'result := ${actor_name_snake}.${method_name_snake}()!'
|
||||||
}
|
}
|
||||||
return '
|
return '
|
||||||
fn cmd_${method_name_snake}(cmd Command) ! {
|
fn cmd_${method_name_snake}_execute(cmd Command) ! {
|
||||||
${method_call}
|
${method_call}
|
||||||
}
|
}
|
||||||
'
|
'
|
||||||
48
lib/baobab/generator/generate_interface.v
Normal file
48
lib/baobab/generator/generate_interface.v
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.specification {ActorInterface}
|
||||||
|
import freeflowuniverse.herolib.core.code { Folder, IFile, VFile, CodeItem, File, Function, Import, Module, Struct, CustomCode }
|
||||||
|
|
||||||
|
fn generate_openrpc_interface_files(interfaces []ActorInterface) (VFile, VFile) {
|
||||||
|
http := ActorInterface.http in interfaces
|
||||||
|
|
||||||
|
iface_file := VFile {
|
||||||
|
name: 'interface_openrpc'
|
||||||
|
items: [CustomCode{$tmpl('./templates/interface_openrpc.v.template')}]
|
||||||
|
}
|
||||||
|
iface_test_file := VFile {
|
||||||
|
name: 'interface_openrpc_test'
|
||||||
|
items: [CustomCode{$tmpl('./templates/interface_openrpc_test.v.template')}]
|
||||||
|
}
|
||||||
|
return iface_file, iface_test_file
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_openapi_interface_files(interfaces []ActorInterface) (VFile, VFile) {
|
||||||
|
http := ActorInterface.http in interfaces
|
||||||
|
|
||||||
|
iface_file := VFile {
|
||||||
|
name: 'interface_openapi'
|
||||||
|
items: [CustomCode{$tmpl('./templates/interface_openapi.v.template')}]
|
||||||
|
}
|
||||||
|
iface_test_file := VFile {
|
||||||
|
name: 'interface_openapi_test'
|
||||||
|
items: [CustomCode{$tmpl('./templates/interface_openapi_test.v.template')}]
|
||||||
|
}
|
||||||
|
return iface_file, iface_test_file
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_http_interface_files(controllers []ActorInterface) (VFile, VFile) {
|
||||||
|
dollar := '$'
|
||||||
|
openapi := ActorInterface.openapi in controllers
|
||||||
|
openrpc := ActorInterface.openrpc in controllers
|
||||||
|
|
||||||
|
iface_file := VFile {
|
||||||
|
name: 'interface_http'
|
||||||
|
items: [CustomCode{$tmpl('./templates/interface_http.v.template')}]
|
||||||
|
}
|
||||||
|
iface_test_file := VFile {
|
||||||
|
name: 'interface_http_test'
|
||||||
|
items: [CustomCode{$tmpl('./templates/interface_http_test.v.template')}]
|
||||||
|
}
|
||||||
|
return iface_file, iface_test_file
|
||||||
|
}
|
||||||
75
lib/baobab/generator/generate_methods.v
Normal file
75
lib/baobab/generator/generate_methods.v
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { Array, Folder, IFile, VFile, CodeItem, File, Function, Param, Import, Module, Struct, CustomCode, Result }
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc.codegen {content_descriptor_to_parameter}
|
||||||
|
import freeflowuniverse.herolib.baobab.specification {ActorMethod, ActorSpecification}
|
||||||
|
|
||||||
|
const crud_prefixes = ['new', 'get', 'set', 'delete', 'list']
|
||||||
|
|
||||||
|
pub fn generate_methods_file(spec ActorSpecification) !VFile {
|
||||||
|
actor_name_snake := texttools.snake_case(spec.name)
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(spec.name)
|
||||||
|
|
||||||
|
mut items := []CodeItem{}
|
||||||
|
for method in spec.methods {
|
||||||
|
method_fn := generate_method_function(spec.name, method)!
|
||||||
|
// check if method is a Base Object CRUD Method and
|
||||||
|
// if so generate the method's body
|
||||||
|
body := match spec.method_type(method) {
|
||||||
|
.base_object_new { base_object_new_body(method)! }
|
||||||
|
.base_object_get { base_object_get_body(method)! }
|
||||||
|
.base_object_set { base_object_set_body(method)! }
|
||||||
|
.base_object_delete { base_object_delete_body(method)! }
|
||||||
|
.base_object_list { base_object_list_body(method)! }
|
||||||
|
else {"panic('implement')"}
|
||||||
|
}
|
||||||
|
items << Function{...method_fn, body: body}
|
||||||
|
}
|
||||||
|
|
||||||
|
return VFile {
|
||||||
|
name: 'methods'
|
||||||
|
items: items
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns bodyless method prototype
|
||||||
|
pub fn generate_method_function(actor_name string, method ActorMethod) !Function {
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(actor_name)
|
||||||
|
result_param := content_descriptor_to_parameter(method.result)!
|
||||||
|
return Function{
|
||||||
|
name: texttools.snake_case(method.name)
|
||||||
|
receiver: code.new_param(v: 'mut actor ${actor_name_pascal}Actor')!
|
||||||
|
result: Param {...result_param, typ: Result{result_param.typ}}
|
||||||
|
summary: method.summary
|
||||||
|
description: method.description
|
||||||
|
params: method.parameters.map(content_descriptor_to_parameter(it)!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_object_new_body(method ActorMethod) !string {
|
||||||
|
parameter := content_descriptor_to_parameter(method.parameters[0])!
|
||||||
|
return 'return actor.osis.new[${parameter.typ.vgen()}](${texttools.snake_case(parameter.name)})!'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_object_get_body(method ActorMethod) !string {
|
||||||
|
parameter := content_descriptor_to_parameter(method.parameters[0])!
|
||||||
|
result := content_descriptor_to_parameter(method.result)!
|
||||||
|
return 'return actor.osis.get[${result.typ.vgen()}](${texttools.snake_case(parameter.name)})!'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_object_set_body(method ActorMethod) !string {
|
||||||
|
parameter := content_descriptor_to_parameter(method.parameters[0])!
|
||||||
|
return 'return actor.osis.set[${parameter.typ.vgen()}](${parameter.name})!'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_object_delete_body(method ActorMethod) !string {
|
||||||
|
parameter := content_descriptor_to_parameter(method.parameters[0])!
|
||||||
|
return 'actor.osis.delete(${texttools.snake_case(parameter.name)})!'
|
||||||
|
}
|
||||||
|
|
||||||
|
fn base_object_list_body(method ActorMethod) !string {
|
||||||
|
result := content_descriptor_to_parameter(method.result)!
|
||||||
|
base_object_type := (result.typ as Array).typ
|
||||||
|
return 'return actor.osis.list[${base_object_type.symbol()}]()!'
|
||||||
|
}
|
||||||
19
lib/baobab/generator/generate_model.v
Normal file
19
lib/baobab/generator/generate_model.v
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { Folder, IFile, VFile, CodeItem, File, Function, Param, Import, Module, Struct, CustomCode }
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema.codegen {schema_to_struct}
|
||||||
|
import freeflowuniverse.herolib.baobab.specification {ActorMethod, ActorSpecification}
|
||||||
|
|
||||||
|
pub fn generate_model_file(spec ActorSpecification) !VFile {
|
||||||
|
actor_name_snake := texttools.snake_case(spec.name)
|
||||||
|
actor_name_pascal := texttools.snake_case_to_pascal(spec.name)
|
||||||
|
|
||||||
|
return VFile {
|
||||||
|
name: 'model'
|
||||||
|
items: spec.objects.map(CodeItem(
|
||||||
|
Struct {...schema_to_struct(it.schema)
|
||||||
|
is_pub: true
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
module generator
|
module generator
|
||||||
|
|
||||||
// pub fn generate_object_code(actor Struct, object BaseObject) VFile {
|
// pub fn generate_object_code(actor Struct, object BaseObject) VFile {
|
||||||
// obj_name := texttools.name_fix_snake(object.structure.name)
|
// obj_name := texttools.snake_case(object.structure.name)
|
||||||
// object_type := object.structure.name
|
// object_type := object.structure.name
|
||||||
|
|
||||||
// mut items := []CodeItem{}
|
// mut items := []CodeItem{}
|
||||||
@@ -10,7 +10,7 @@ module generator
|
|||||||
// generate_list_result_struct(actor, object), generate_list_method(actor, object)]
|
// generate_list_result_struct(actor, object), generate_list_method(actor, object)]
|
||||||
|
|
||||||
// items << generate_object_methods(actor, object)
|
// items << generate_object_methods(actor, object)
|
||||||
// mut file := codemodel.new_file(
|
// mut file := code.new_file(
|
||||||
// mod: texttools.name_fix(actor.name)
|
// mod: texttools.name_fix(actor.name)
|
||||||
// name: obj_name
|
// name: obj_name
|
||||||
// imports: [
|
// imports: [
|
||||||
@@ -38,10 +38,10 @@ module generator
|
|||||||
|
|
||||||
// pub fn (a Actor) generate_model_files() ![]VFile {
|
// pub fn (a Actor) generate_model_files() ![]VFile {
|
||||||
// structs := a.objects.map(it.structure)
|
// structs := a.objects.map(it.structure)
|
||||||
// return a.objects.map(codemodel.new_file(
|
// return a.objects.map(code.new_file(
|
||||||
// mod: texttools.name_fix(a.name)
|
// mod: texttools.name_fix(a.name)
|
||||||
// name: '${texttools.name_fix(it.structure.name)}_model'
|
// name: '${texttools.name_fix(it.structure.name)}_model'
|
||||||
// // imports: [Import{mod:'freeflowuniverse.herolib.baobab.actor'}]
|
// // imports: [Import{mod:'freeflowuniverse.herolib.baobab.stage'}]
|
||||||
// items: [it.structure]
|
// items: [it.structure]
|
||||||
// ))
|
// ))
|
||||||
// }
|
// }
|
||||||
158
lib/baobab/generator/generate_openapi.v
Normal file
158
lib/baobab/generator/generate_openapi.v
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import json
|
||||||
|
import freeflowuniverse.herolib.core.code { VFile, File, Folder, Function, Module, Struct }
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi { Components, OpenAPI, Operation }
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi.codegen
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema.codegen as jsonschema_codegen {schema_to_type}
|
||||||
|
import net.http
|
||||||
|
|
||||||
|
pub fn generate_openapi_file(specification OpenAPI) !File {
|
||||||
|
openapi_json := specification.encode_json()
|
||||||
|
return File{
|
||||||
|
name: 'openapi'
|
||||||
|
extension: 'json'
|
||||||
|
content: openapi_json
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_openapi_ts_client(specification OpenAPI) !Folder {
|
||||||
|
return codegen.ts_client_folder(specification,
|
||||||
|
body_generator: body_generator
|
||||||
|
custom_client_code: ' private restClient: HeroRestClient;
|
||||||
|
|
||||||
|
constructor(heroKeysClient: any, debug: boolean = true) {
|
||||||
|
this.restClient = new HeroRestClient(heroKeysClient, debug);
|
||||||
|
}
|
||||||
|
'
|
||||||
|
)!
|
||||||
|
}
|
||||||
|
|
||||||
|
fn body_generator(op openapi.Operation, path_ string, method http.Method) string {
|
||||||
|
path := path_.replace('{','\${')
|
||||||
|
return match method {
|
||||||
|
.post {
|
||||||
|
if schema := op.payload_schema() {
|
||||||
|
symbol := schema_to_type(schema).typescript()
|
||||||
|
"return this.restClient.post<${symbol}>('${path}', data);"
|
||||||
|
} else {''}
|
||||||
|
}
|
||||||
|
.get {
|
||||||
|
if schema := op.response_schema() {
|
||||||
|
// if op.params.len
|
||||||
|
symbol := schema_to_type(schema).typescript()
|
||||||
|
"return this.restClient.get<${symbol}>('${path}', data);"
|
||||||
|
} else {''}
|
||||||
|
} else {''}
|
||||||
|
}
|
||||||
|
// return if operation_is_base_object_method(op) {
|
||||||
|
// bo_method := operation_to_base_object_method(op)
|
||||||
|
// match method_type(op) {
|
||||||
|
// .new { ts_client_new_body(op, path) }
|
||||||
|
// .get { ts_client_get_body(op, path) }
|
||||||
|
// .set { ts_client_set_body(op, path) }
|
||||||
|
// .delete { ts_client_delete_body(op, path) }
|
||||||
|
// .list { ts_client_list_body(op, path) }
|
||||||
|
// else {''}
|
||||||
|
// }
|
||||||
|
// } else {''}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// pub fn operation_is_base_object_method(op openapi.Operation, base_objs []string) BaseObjectMethod {
|
||||||
|
// // name := texttools.pascal_case(op.operation_id)
|
||||||
|
|
||||||
|
// // if op.operation_id.starts_with('new') {
|
||||||
|
// // if op.&& operation.params.len == 1
|
||||||
|
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn operation_to_base_object_method(op openapi.Operation) BaseObjectMethod {
|
||||||
|
// if op.operation_id.starts_with('update')
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub fn openapi_ts_client_body(op openapi.Operation, path string, method http.Method) string {
|
||||||
|
// match method {
|
||||||
|
// post {
|
||||||
|
// if schema := op.payload_schema() {
|
||||||
|
// symbol := schema_to_type(schema).typescript()
|
||||||
|
// return "return this.restClient.post<${symbol}>('${path}', data);"
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// return if operation_is_base_object_method(op) {
|
||||||
|
// bo_method := operation_to_base_object_method(op)
|
||||||
|
// match bo_method. {
|
||||||
|
// .new { ts_client_new_body(op, path) }
|
||||||
|
// .get { ts_client_get_body(op, path) }
|
||||||
|
// .set { ts_client_set_body(op, path) }
|
||||||
|
// .delete { ts_client_delete_body(op, path) }
|
||||||
|
// .list { ts_client_list_body(op, path) }
|
||||||
|
// else {''}
|
||||||
|
// }
|
||||||
|
// } else {''}
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
fn get_endpoint(path string) string {
|
||||||
|
return if path == '' {
|
||||||
|
''
|
||||||
|
} else {
|
||||||
|
"/${path.trim('/')}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// // generates a Base Object's `create` method
|
||||||
|
// fn ts_client_new_body(op Operation, path string) string {
|
||||||
|
// // the parameter of a base object new method is always the base object
|
||||||
|
// bo_param := openapi_codegen.parameter_to_param(op.parameters[0])
|
||||||
|
// return "return this.restClient.post<${bo_param.typ.typescript()}>('${get_endpoint(path)}', ${bo_param.name});"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generates a Base Object's `create` method
|
||||||
|
// fn ts_client_get_body(op Operation, path string) string {
|
||||||
|
// // the parameter of a base object get method is always the id
|
||||||
|
// id_param := openapi_codegen.parameter_to_param(op.parameters[0])
|
||||||
|
// return "return this.restClient.get<${id_param.typ.typescript()}>('${get_endpoint(path)}', ${id_param.name});"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generates a Base Object's `create` method
|
||||||
|
// fn ts_client_set_body(op Operation, path string) string {
|
||||||
|
// // the parameter of a base object set method is always the base object
|
||||||
|
// bo_param := openapi_codegen.parameter_to_param(op.parameters[0])
|
||||||
|
// return "return this.restClient.put<${bo_param.typ.typescript()}>('${get_endpoint(path)}', ${bo_param.name});"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generates a Base Object's `delete` method
|
||||||
|
// fn ts_client_delete_body(op Operation, path string) string {
|
||||||
|
// // the parameter of a base object delete method is always the id
|
||||||
|
// id_param := openapi_codegen.parameter_to_param(op.parameters[0])
|
||||||
|
// return "return this.restClient.get<${id_param.typ.typescript()}>('${get_endpoint(path)}', ${id_param.name});"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generates a Base Object's `list` method
|
||||||
|
// fn ts_client_list_body(op Operation, path string) string {
|
||||||
|
// // the result parameter of a base object list method is always the array of bo
|
||||||
|
// result_param := openapi_codegen.parameter_to_param(op.parameters[0])
|
||||||
|
// return "return this.restClient.get<${result_param.typ.typescript()}>('${get_endpoint(path)}');"
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub enum BaseObjectMethodType {
|
||||||
|
// new
|
||||||
|
// get
|
||||||
|
// set
|
||||||
|
// delete
|
||||||
|
// list
|
||||||
|
// other
|
||||||
|
// }
|
||||||
|
|
||||||
|
// pub struct BaseObjectMethod {
|
||||||
|
// pub:
|
||||||
|
// typ BaseObjectMethodType
|
||||||
|
// object string // the name of the base object
|
||||||
|
// }
|
||||||
@@ -1,12 +1,9 @@
|
|||||||
module generator
|
module generator
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import freeflowuniverse.herolib.core.codemodel { File, Function, Struct, VFile }
|
import freeflowuniverse.herolib.core.code { VFile, File, Function, Module, Struct }
|
||||||
import freeflowuniverse.herolib.core.pathlib
|
import freeflowuniverse.herolib.schemas.openrpc { Components, OpenRPC }
|
||||||
import freeflowuniverse.herolib.core.texttools
|
import freeflowuniverse.herolib.schemas.openrpc.codegen { generate_client_file, generate_client_test_file }
|
||||||
import freeflowuniverse.herolib.hero.baobab.specification
|
|
||||||
import freeflowuniverse.herolib.rpc.openrpc { OpenRPC }
|
|
||||||
import freeflowuniverse.herolib.data.jsonschema
|
|
||||||
|
|
||||||
pub fn generate_openrpc_file(spec OpenRPC) !File {
|
pub fn generate_openrpc_file(spec OpenRPC) !File {
|
||||||
return File{
|
return File{
|
||||||
@@ -21,9 +18,9 @@ pub fn generate_openrpc_client_file(spec OpenRPC) !VFile {
|
|||||||
// for object in spec.objects {
|
// for object in spec.objects {
|
||||||
// objects_map[object.structure.name] = object.structure
|
// objects_map[object.structure.name] = object.structure
|
||||||
// }
|
// }
|
||||||
client_file := spec.generate_client_file(objects_map)!
|
client_file := generate_client_file(spec, objects_map)!
|
||||||
return VFile {
|
return VFile {
|
||||||
...client_file
|
...client_file,
|
||||||
name: 'client_openrpc'
|
name: 'client_openrpc'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,9 +34,9 @@ pub fn generate_openrpc_client_test_file(spec OpenRPC) !VFile {
|
|||||||
// for method in spec.methods {
|
// for method in spec.methods {
|
||||||
// methods_map[method.func.name] = method.func
|
// methods_map[method.func.name] = method.func
|
||||||
// }
|
// }
|
||||||
file := spec.generate_client_test_file(methods_map, objects_map)!
|
file := generate_client_test_file(spec, methods_map, objects_map)!
|
||||||
return VFile {
|
return VFile {
|
||||||
...file
|
...file,
|
||||||
name: 'client_openrpc_test'
|
name: 'client_openrpc_test'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
module generator
|
module generator
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.codemodel { Function, Param, Result, Struct, Type }
|
import freeflowuniverse.herolib.core.code { Function, Param, Result, Struct, Type }
|
||||||
import freeflowuniverse.herolib.rpc.openrpc
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
|
||||||
const test_actor_specification = ActorSpecification{
|
const test_actor_specification = ActorSpecification{
|
||||||
methods: [
|
methods: [
|
||||||
88
lib/baobab/generator/generate_scripts.v
Normal file
88
lib/baobab/generator/generate_scripts.v
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { Folder, File }
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
|
||||||
|
// generates the folder with runnable scripts of the actor
|
||||||
|
pub fn generate_scripts_folder(name string, example bool) Folder {
|
||||||
|
actor_name := '${texttools.snake_case(name)}_actor'
|
||||||
|
return Folder {
|
||||||
|
name: 'scripts'
|
||||||
|
files: [
|
||||||
|
generate_run_script(actor_name),
|
||||||
|
generate_docs_script(actor_name),
|
||||||
|
generate_run_actor_script(actor_name),
|
||||||
|
// generate_run_example_actor_script(actor_name),
|
||||||
|
generate_run_http_server_script(actor_name, example),
|
||||||
|
// generate_compile_script(actor_name),
|
||||||
|
// generate_generate_script(actor_name)
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to generate a script for running an actor
|
||||||
|
fn generate_run_script(actor_name string) File {
|
||||||
|
actor_title := texttools.title_case(actor_name)
|
||||||
|
dollar := '$'
|
||||||
|
return File{
|
||||||
|
name: 'run'
|
||||||
|
extension:'sh'
|
||||||
|
content: $tmpl('./templates/run.sh.template')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to generate a script for running an actor
|
||||||
|
fn generate_docs_script(actor_name string) File {
|
||||||
|
dollar := '$'
|
||||||
|
return File{
|
||||||
|
name: 'docs'
|
||||||
|
extension:'vsh'
|
||||||
|
content: $tmpl('./templates/docs.vsh.template')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to generate a script for running an actor
|
||||||
|
fn generate_run_actor_script(actor_name string) File {
|
||||||
|
return File{
|
||||||
|
name: 'run_actor'
|
||||||
|
extension:'vsh'
|
||||||
|
content: $tmpl('./templates/run_actor.vsh.template')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to generate a script for running an example actor
|
||||||
|
fn generate_run_example_actor_script(actor_name string) File {
|
||||||
|
return File{
|
||||||
|
name: 'run_example_actor'
|
||||||
|
extension:'vsh'
|
||||||
|
content: $tmpl('./templates/run_example_actor.vsh.template')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to generate a script for running an HTTP server
|
||||||
|
fn generate_run_http_server_script(actor_name string, example bool) File {
|
||||||
|
port := if example {8081} else {8080}
|
||||||
|
return File{
|
||||||
|
name: 'run_http_server'
|
||||||
|
extension:'vsh'
|
||||||
|
content: $tmpl('./templates/run_http_server.vsh.template')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// // Function to generate a script for compiling the project
|
||||||
|
// fn generate_compile_script(actor_name string) File {
|
||||||
|
// return File{
|
||||||
|
// name: 'compile'
|
||||||
|
// extension:'sh'
|
||||||
|
// content: $tmpl('./templates/run_http_server.vsh.template')
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Function to generate a script for general generation tasks
|
||||||
|
// fn generate_generate_script(actor_name string) File {
|
||||||
|
// return File{
|
||||||
|
// name: 'generate'
|
||||||
|
// extension: 'vsh'
|
||||||
|
// content: $tmpl('./templates/run_http_server.vsh.template')
|
||||||
|
// }
|
||||||
|
// }
|
||||||
@@ -1,20 +1,19 @@
|
|||||||
module example_actor
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import freeflowuniverse.herolib.hero.baobab.actor { IActor, RunParams }
|
import freeflowuniverse.herolib.baobab.stage {IActor, RunParams}
|
||||||
import freeflowuniverse.herolib.web.openapi
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
import time
|
|
||||||
|
|
||||||
const openapi_spec_path = '${os.dir(@FILE)}/specs/openapi.json'
|
const openapi_spec_path = '@{dollar}{os.dir(@@FILE)}/specs/openapi.json'
|
||||||
const openapi_spec_json = os.read_file(openapi_spec_path) or { panic(err) }
|
const openapi_spec_json = os.read_file(openapi_spec_path) or { panic(err) }
|
||||||
const openapi_specification = openapi.json_decode(openapi_spec_json)!
|
const openapi_specification = openapi.json_decode(openapi_spec_json)!
|
||||||
|
|
||||||
struct ExampleActor {
|
struct @{actor_name_pascal}Actor {
|
||||||
actor.Actor
|
stage.Actor
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new() !ExampleActor {
|
fn new() !@{actor_name_pascal}Actor {
|
||||||
return ExampleActor{actor.new('example')}
|
return @{actor_name_pascal}Actor {
|
||||||
|
stage.new_actor('@{actor_name_snake}')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run() ! {
|
pub fn run() ! {
|
||||||
37
lib/baobab/generator/templates/actor.v.template
Normal file
37
lib/baobab/generator/templates/actor.v.template
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import os
|
||||||
|
import freeflowuniverse.herolib.baobab.stage
|
||||||
|
import freeflowuniverse.herolib.core.redisclient
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
|
||||||
|
const name = '@{actor_name_snake}'
|
||||||
|
|
||||||
|
@@[heap]
|
||||||
|
struct @{actor_name_pascal}Actor {
|
||||||
|
stage.Actor
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new() !&@{actor_name_pascal}Actor {
|
||||||
|
return &@{actor_name_pascal}Actor {
|
||||||
|
Actor: stage.new_actor('@{actor_name_snake}')!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut a @{actor_name_pascal}Actor) handle(method string, data string) !string {
|
||||||
|
action := a.act(
|
||||||
|
name: method
|
||||||
|
params: data
|
||||||
|
)!
|
||||||
|
return action.result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actor listens to the Redis queue for method invocations
|
||||||
|
pub fn (mut a @{actor_name_pascal}Actor) run() ! {
|
||||||
|
mut redis := redisclient.new('localhost:6379') or { panic(err) }
|
||||||
|
mut rpc := redis.rpc_get('actor_@{dollar}{a.name}')
|
||||||
|
|
||||||
|
println('Actor started and listening for tasks...')
|
||||||
|
for {
|
||||||
|
rpc.process(a.handle)!
|
||||||
|
time.sleep(time.millisecond * 100) // Prevent CPU spinning
|
||||||
|
}
|
||||||
|
}
|
||||||
37
lib/baobab/generator/templates/actor_example.v.template
Normal file
37
lib/baobab/generator/templates/actor_example.v.template
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import os
|
||||||
|
import freeflowuniverse.herolib.baobab.stage
|
||||||
|
import freeflowuniverse.herolib.core.redisclient
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
|
||||||
|
const name = '@{actor_name_snake}'
|
||||||
|
|
||||||
|
@@[heap]
|
||||||
|
struct @{actor_name_pascal}Actor {
|
||||||
|
stage.Actor
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new() !&@{actor_name_pascal}Actor {
|
||||||
|
return &@{actor_name_pascal}Actor {
|
||||||
|
Actor: stage.new_actor('example_@{actor_name_snake}')!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut a @{actor_name_pascal}Actor) handle(method string, data string) !string {
|
||||||
|
action := a.act(
|
||||||
|
name: method
|
||||||
|
params: data
|
||||||
|
)!
|
||||||
|
return action.result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actor listens to the Redis queue for method invocations
|
||||||
|
pub fn (mut a @{actor_name_pascal}Actor) run() ! {
|
||||||
|
mut redis := redisclient.new('localhost:6379') or { panic(err) }
|
||||||
|
mut rpc := redis.rpc_get('actor_@{dollar}{a.name}')
|
||||||
|
|
||||||
|
println('Actor started and listening for tasks...')
|
||||||
|
for {
|
||||||
|
rpc.process(a.handle)!
|
||||||
|
time.sleep(time.millisecond * 100) // Prevent CPU spinning
|
||||||
|
}
|
||||||
|
}
|
||||||
10
lib/baobab/generator/templates/actor_test.v.template
Normal file
10
lib/baobab/generator/templates/actor_test.v.template
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
const test_port = 8101
|
||||||
|
|
||||||
|
pub fn test_new() ! {
|
||||||
|
new() or { return error('Failed to create actor:\n@{dollar}{err}') }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_actor_run() ! {
|
||||||
|
mut actor := new()!
|
||||||
|
spawn actor.run()
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@ module @{name}
|
|||||||
import os
|
import os
|
||||||
import cli { Command }
|
import cli { Command }
|
||||||
import vweb
|
import vweb
|
||||||
import freeflowuniverse.herolib.rpc.openrpc
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
import freeflowuniverse.herolib.core.pathlib
|
import freeflowuniverse.herolib.core.pathlib
|
||||||
|
|
||||||
|
|
||||||
74
lib/baobab/generator/templates/client_test.v
Normal file
74
lib/baobab/generator/templates/client_test.v
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
module pet_store_actor
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.stage
|
||||||
|
import freeflowuniverse.herolib.core.redisclient
|
||||||
|
import x.json2 as json
|
||||||
|
import time
|
||||||
|
|
||||||
|
fn mock_response() ! {
|
||||||
|
mut redis := redisclient.new('localhost:6379')!
|
||||||
|
mut rpc_q := redis.rpc_get('actor_pet_store')
|
||||||
|
for {
|
||||||
|
rpc_q.process(fn(method string, data string)!string{
|
||||||
|
return json.encode(method)
|
||||||
|
})!
|
||||||
|
time.sleep(time.millisecond * 100) // Prevent CPU spinning
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_list_pets() ! {
|
||||||
|
mut client := new_client()!
|
||||||
|
limit := 10
|
||||||
|
spawn mock_response()
|
||||||
|
pets := client.list_pets(limit)!
|
||||||
|
// assert pets.len <= limit
|
||||||
|
println('test_list_pets passed')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_create_pet() ! {
|
||||||
|
mut client := new_client()!
|
||||||
|
client.create_pet()!
|
||||||
|
println('test_create_pet passed')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_get_pet() ! {
|
||||||
|
mut client := new_client()!
|
||||||
|
pet_id := 1 // Replace with an actual pet ID in your system
|
||||||
|
pet := client.get_pet(pet_id)!
|
||||||
|
// assert pet.id == pet_id
|
||||||
|
println('test_get_pet passed')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_delete_pet() ! {
|
||||||
|
mut client := new_client()!
|
||||||
|
pet_id := 1 // Replace with an actual pet ID in your system
|
||||||
|
client.delete_pet(pet_id)!
|
||||||
|
println('test_delete_pet passed')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_list_orders() ! {
|
||||||
|
mut client := new_client()!
|
||||||
|
client.list_orders()!
|
||||||
|
println('test_list_orders passed')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_get_order() ! {
|
||||||
|
mut client := new_client()!
|
||||||
|
order_id := 1 // Replace with an actual order ID in your system
|
||||||
|
order := client.get_order(order_id)!
|
||||||
|
// assert order.id == order_id
|
||||||
|
println('test_get_order passed')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_delete_order() ! {
|
||||||
|
mut client := new_client()!
|
||||||
|
order_id := 1 // Replace with an actual order ID in your system
|
||||||
|
client.delete_order(order_id)!
|
||||||
|
println('test_delete_order passed')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_create_user() ! {
|
||||||
|
mut client := new_client()!
|
||||||
|
client.create_user()!
|
||||||
|
println('test_create_user passed')
|
||||||
|
}
|
||||||
51
lib/baobab/generator/templates/docs.vsh.template
Executable file
51
lib/baobab/generator/templates/docs.vsh.template
Executable file
@@ -0,0 +1,51 @@
|
|||||||
|
#!/usr/bin/env -S v -n -w -gc none -no-retry-compilation -cc tcc -d use_openssl -enable-globals run
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
abs_dir_of_script := dir(@@FILE)
|
||||||
|
|
||||||
|
// Format code
|
||||||
|
println('Formatting code...')
|
||||||
|
if os.system('v fmt -w @{dollar}{abs_dir_of_script}/examples') != 0 {
|
||||||
|
eprintln('Warning: Failed to format examples')
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.system('v fmt -w @{dollar}{abs_dir_of_script}/src') != 0 {
|
||||||
|
eprintln('Warning: Failed to format actor')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean existing docs
|
||||||
|
println('Cleaning existing documentation...')
|
||||||
|
|
||||||
|
os.rmdir_all('_docs') or {}
|
||||||
|
os.rmdir_all('docs') or {}
|
||||||
|
os.rmdir_all('vdocs') or {}
|
||||||
|
|
||||||
|
herolib_path := os.join_path(abs_dir_of_script, 'lib')
|
||||||
|
os.chdir(herolib_path) or {
|
||||||
|
panic('Failed to change directory to herolib: @{dollar}{err}')
|
||||||
|
}
|
||||||
|
|
||||||
|
os.rmdir_all('_docs') or {}
|
||||||
|
os.rmdir_all('docs') or {}
|
||||||
|
os.rmdir_all('vdocs') or {}
|
||||||
|
|
||||||
|
// Generate HTML documentation
|
||||||
|
println('Generating HTML documentation...')
|
||||||
|
if os.system('v doc -m -f html . -readme -comments -no-timestamp -o ../docs') != 0 {
|
||||||
|
panic('Failed to generate HTML documentation')
|
||||||
|
}
|
||||||
|
|
||||||
|
os.chdir(abs_dir_of_script) or {
|
||||||
|
panic('Failed to change directory to abs_dir_of_script: @{dollar}{err}')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate Markdown documentation
|
||||||
|
println('Generating Markdown documentation...')
|
||||||
|
os.rmdir_all('vdocs') or {}
|
||||||
|
|
||||||
|
if os.system('v doc -m -no-color -f md -o vdocs/herolib/') != 0 {
|
||||||
|
panic('Failed to generate Hero markdown documentation')
|
||||||
|
}
|
||||||
|
|
||||||
|
println('Documentation generation completed successfully!')
|
||||||
46
lib/baobab/generator/templates/interface_http.v.template
Normal file
46
lib/baobab/generator/templates/interface_http.v.template
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import freeflowuniverse.herolib.schemas.openapi { OpenAPI }
|
||||||
|
import freeflowuniverse.herolib.baobab.stage {Client, ClientConfig}
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc { OpenRPC }
|
||||||
|
import freeflowuniverse.herolib.baobab.stage.interfaces { HTTPServer, Context }
|
||||||
|
import veb
|
||||||
|
|
||||||
|
@@[params]
|
||||||
|
pub struct ServerParams {
|
||||||
|
pub:
|
||||||
|
base_url string
|
||||||
|
port int = 8080
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_http_server(params ServerParams) !&HTTPServer {
|
||||||
|
mut s := interfaces.new_http_server()!
|
||||||
|
@if openrpc
|
||||||
|
mut openrpc_controller := new_openrpc_http_controller(ServerParams{
|
||||||
|
...params,
|
||||||
|
base_url: '@{dollar}{params.base_url}/openrpc'
|
||||||
|
})!
|
||||||
|
s.register_controller[openrpc.HTTPController, Context]('/openrpc', mut openrpc_controller)!
|
||||||
|
@end
|
||||||
|
@if openapi
|
||||||
|
mut openapi_controller := new_openapi_http_controller(ServerParams{
|
||||||
|
...params,
|
||||||
|
base_url: '@{dollar}{params.base_url}/openapi/v1'
|
||||||
|
})!
|
||||||
|
mut openapi_example_controller := new_openapi_http_controller(ServerParams{
|
||||||
|
...params,
|
||||||
|
base_url: '@{dollar}{params.base_url}/openapi/example'
|
||||||
|
})!
|
||||||
|
mut openapi_playground_controller := openapi.new_playground_controller(
|
||||||
|
base_url: '@{dollar}{params.base_url}/playground/openapi'
|
||||||
|
specification_path: openapi_spec_path
|
||||||
|
)!
|
||||||
|
s.register_controller[openapi.HTTPController, Context]('/openapi/v1', mut openapi_controller)!
|
||||||
|
s.register_controller[openapi.HTTPController, Context]('/openapi/example', mut openapi_example_controller)!
|
||||||
|
s.register_controller[openapi.PlaygroundController, Context]('/playground/openapi', mut openapi_playground_controller)!
|
||||||
|
@end
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_http_server(params ServerParams) ! {
|
||||||
|
mut server := new_http_server(params)!
|
||||||
|
veb.run[HTTPServer, Context](mut server, params.port)
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fn test_new_http_server() ! {
|
||||||
|
new_http_server()!
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_run_http_server() ! {
|
||||||
|
spawn run_http_server()
|
||||||
|
}
|
||||||
21
lib/baobab/generator/templates/interface_openapi.v.template
Normal file
21
lib/baobab/generator/templates/interface_openapi.v.template
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import freeflowuniverse.herolib.baobab.stage.interfaces
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
|
||||||
|
pub fn new_openapi_interface() !&interfaces.OpenAPIInterface {
|
||||||
|
// create OpenAPI Handler with actor's client
|
||||||
|
client := new_client()!
|
||||||
|
return interfaces.new_openapi_interface(client.Client)
|
||||||
|
}
|
||||||
|
|
||||||
|
@if http
|
||||||
|
// creates HTTP controller with the actor's OpenAPI Handler
|
||||||
|
// and OpenAPI Specification
|
||||||
|
pub fn new_openapi_http_controller(params ServerParams) !&openapi.HTTPController {
|
||||||
|
return openapi.new_http_controller(
|
||||||
|
base_url: params.base_url
|
||||||
|
specification: openapi_specification
|
||||||
|
specification_path: openapi_spec_path
|
||||||
|
handler: new_openapi_interface()!
|
||||||
|
)
|
||||||
|
}
|
||||||
|
@end
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
fn test_new_openapi_interface() ! {
|
||||||
|
new_openapi_interface()!
|
||||||
|
}
|
||||||
|
|
||||||
|
@if http
|
||||||
|
fn test_new_openapi_http_controller() ! {
|
||||||
|
new_openapi_http_controller()!
|
||||||
|
}
|
||||||
|
@end
|
||||||
19
lib/baobab/generator/templates/interface_openrpc.v.template
Normal file
19
lib/baobab/generator/templates/interface_openrpc.v.template
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import freeflowuniverse.herolib.baobab.stage.interfaces
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
|
||||||
|
pub fn new_openrpc_interface() !&interfaces.OpenRPCInterface {
|
||||||
|
// create OpenRPC Handler with actor's client
|
||||||
|
client := new_client()!
|
||||||
|
return interfaces.new_openrpc_interface(client.Client)
|
||||||
|
}
|
||||||
|
|
||||||
|
@if http
|
||||||
|
// creates HTTP controller with the actor's OpenRPC Handler
|
||||||
|
// and OpenRPC Specification
|
||||||
|
pub fn new_openrpc_http_controller(params ServerParams) !&openrpc.HTTPController {
|
||||||
|
return openrpc.new_http_controller(
|
||||||
|
specification: openrpc_specification
|
||||||
|
handler: new_openrpc_interface()!
|
||||||
|
)
|
||||||
|
}
|
||||||
|
@end
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
fn test_new_openrpc_interface() ! {
|
||||||
|
new_openrpc_interface()!
|
||||||
|
}
|
||||||
|
|
||||||
|
@if http
|
||||||
|
fn test_new_openrpc_http_controller() ! {
|
||||||
|
new_openrpc_http_controller()!
|
||||||
|
}
|
||||||
|
@end
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/env -S v -n -cg -w -enable-globals run
|
#!/usr/bin/env -S v -n -cg -w -enable-globals run
|
||||||
|
|
||||||
import freeflowuniverse.herolib.baobab.actors.accountant
|
import freeflowuniverse.herolib.baobab.stages.accountant
|
||||||
import vweb
|
import vweb
|
||||||
import freeflowuniverse.herolib.rpc.openrpc
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
import os
|
import os
|
||||||
import freeflowuniverse.herolib.core.pathlib
|
import freeflowuniverse.herolib.core.pathlib
|
||||||
|
|
||||||
38
lib/baobab/generator/templates/run.sh.template
Executable file
38
lib/baobab/generator/templates/run.sh.template
Executable file
@@ -0,0 +1,38 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
DIR="@{dollar}(cd "@{dollar}(dirname "@{dollar}{BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
echo "@{dollar}DIR"
|
||||||
|
|
||||||
|
chmod +x @{dollar}{DIR}/run_actor.vsh
|
||||||
|
@{dollar}{DIR}/run_actor.vsh > /dev/null 2>&1 &
|
||||||
|
ACTOR_PID=@{dollar}!
|
||||||
|
|
||||||
|
chmod +x @{dollar}{DIR}/run_http_server.vsh
|
||||||
|
@{dollar}{DIR}/run_http_server.vsh > /dev/null 2>&1 &
|
||||||
|
HTTP_SERVER_PID=@{dollar}!
|
||||||
|
|
||||||
|
# Print desired output
|
||||||
|
echo "${actor_title} Actor Redis Interface running on redis://localhost:6379"
|
||||||
|
echo "* /queues/${actor_name} -> Action Interface"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "${actor_title} Actor HTTP Server running on http://localhost:8080"
|
||||||
|
echo "* /playground/openapi -> OpenAPI Playground"
|
||||||
|
echo "* /openapi -> OpenAPI Interface"
|
||||||
|
echo "* /docs -> Documentation"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Function to clean up when script is killed
|
||||||
|
cleanup() {
|
||||||
|
echo "Stopping background processes..."
|
||||||
|
kill "@{dollar}ACTOR_PID" "@{dollar}HTTP_SERVER_PID" 2>/dev/null
|
||||||
|
wait
|
||||||
|
echo "All processes stopped."
|
||||||
|
exit 0
|
||||||
|
}
|
||||||
|
|
||||||
|
# Trap SIGINT (Ctrl+C), SIGTERM, and SIGQUIT to call cleanup
|
||||||
|
trap cleanup SIGINT SIGTERM SIGQUIT
|
||||||
|
|
||||||
|
# Wait for processes to finish
|
||||||
|
wait
|
||||||
6
lib/baobab/generator/templates/run_actor.vsh.template
Executable file
6
lib/baobab/generator/templates/run_actor.vsh.template
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import @{actor_name}
|
||||||
|
|
||||||
|
mut actor := @{actor_name}.new()!
|
||||||
|
actor.run()!
|
||||||
6
lib/baobab/generator/templates/run_example_actor.vsh.template
Executable file
6
lib/baobab/generator/templates/run_example_actor.vsh.template
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
|
||||||
|
import @{actor_name}
|
||||||
|
|
||||||
|
mut actor := @{actor_name}.new_example()!
|
||||||
|
actor.run()!
|
||||||
7
lib/baobab/generator/templates/run_http_server.vsh.template
Executable file
7
lib/baobab/generator/templates/run_http_server.vsh.template
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
#!/usr/bin/env -S v -w -n -enable-globals run
|
||||||
|
import @{actor_name}
|
||||||
|
|
||||||
|
@{actor_name}.run_http_server(
|
||||||
|
base_url: 'http://localhost:@{port}'
|
||||||
|
port: @{port}
|
||||||
|
)!
|
||||||
14
lib/baobab/generator/templates/specifications.v.template
Normal file
14
lib/baobab/generator/templates/specifications.v.template
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
import os
|
||||||
|
|
||||||
|
@if support_openrpc
|
||||||
|
const openrpc_spec_path = '@{dollar}{os.dir(@@FILE)}/docs/openrpc.json'
|
||||||
|
const openrpc_spec_json = os.read_file(openrpc_spec_path) or { panic(err) }
|
||||||
|
const openrpc_specification = openrpc.decode(openrpc_spec_json)!
|
||||||
|
@end
|
||||||
|
@if support_openapi
|
||||||
|
const openapi_spec_path = '@{dollar}{os.dir(@@FILE)}/docs/openapi.json'
|
||||||
|
const openapi_spec_json = os.read_file(openapi_spec_path) or { panic(err) }
|
||||||
|
const openapi_specification = openapi.json_decode(openapi_spec_json)!
|
||||||
|
@end
|
||||||
406
lib/baobab/generator/write_object_methods.v
Normal file
406
lib/baobab/generator/write_object_methods.v
Normal file
@@ -0,0 +1,406 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.specification {BaseObject}
|
||||||
|
import freeflowuniverse.herolib.core.code { type_from_symbol, VFile, CodeItem, Function, Import, Param, Param, Struct, StructField, Type }
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
|
||||||
|
const id_param = Param{
|
||||||
|
name: 'id'
|
||||||
|
typ: type_from_symbol('u32')
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn generate_object_code(actor Struct, object BaseObject) VFile {
|
||||||
|
// obj_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// mut items := []CodeItem{}
|
||||||
|
// items = [generate_new_method(actor, object), generate_get_method(actor, object),
|
||||||
|
// generate_set_method(actor, object), generate_delete_method(actor, object),
|
||||||
|
// generate_list_result_struct(actor, object), generate_list_method(actor, object)]
|
||||||
|
|
||||||
|
// items << generate_object_methods(actor, object)
|
||||||
|
// mut file := code.new_file(
|
||||||
|
// mod: texttools.name_fix(actor.name)
|
||||||
|
// name: obj_name
|
||||||
|
// imports: [
|
||||||
|
// Import{
|
||||||
|
// mod: object.structure.mod
|
||||||
|
// types: [object_type]
|
||||||
|
// },
|
||||||
|
// Import{
|
||||||
|
// mod: 'freeflowuniverse.herolib.baobab.backend'
|
||||||
|
// types: ['FilterParams']
|
||||||
|
// },
|
||||||
|
// ]
|
||||||
|
// items: items
|
||||||
|
// )
|
||||||
|
|
||||||
|
// if object.structure.fields.any(it.attrs.any(it.name == 'index')) {
|
||||||
|
// // can't filter without indices
|
||||||
|
// filter_params := generate_filter_params(actor, object)
|
||||||
|
// file.items << filter_params.map(CodeItem(it))
|
||||||
|
// file.items << generate_filter_method(actor, object)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return file
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_get_method(actor Struct, object BaseObject) Function {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// get_method := Function{
|
||||||
|
// name: 'get_${object_name}'
|
||||||
|
// description: 'gets the ${object_name} with the given object id'
|
||||||
|
// receiver: Param{
|
||||||
|
// mutable: true
|
||||||
|
// name: 'actor'
|
||||||
|
// typ: type_from_symbol(actor.name)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// params: [generator.id_param]
|
||||||
|
// result: Param{
|
||||||
|
// typ: type_from_symbol(object.structure.name)
|
||||||
|
// is_result: true
|
||||||
|
// }
|
||||||
|
// body: 'return actor.backend.get[${object_type}](id)!'
|
||||||
|
// }
|
||||||
|
// return get_method
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_set_method(actor Struct, object BaseObject) Function {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// param_getters := generate_param_getters(
|
||||||
|
// structure: object.structure
|
||||||
|
// prefix: ''
|
||||||
|
// only_mutable: true
|
||||||
|
// )
|
||||||
|
// body := 'actor.backend.set[${object_type}](${object_name})!'
|
||||||
|
// get_method := Function{
|
||||||
|
// name: 'set_${object_name}'
|
||||||
|
// description: 'updates the ${object.structure.name} with the given object id'
|
||||||
|
// receiver: Param{
|
||||||
|
// mutable: true
|
||||||
|
// name: 'actor'
|
||||||
|
// typ: type_from_symbol(actor.name)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// params: [
|
||||||
|
// Param{
|
||||||
|
// name: object_name
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: object_type
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// ]
|
||||||
|
// result: Param{
|
||||||
|
// is_result: true
|
||||||
|
// }
|
||||||
|
// body: body
|
||||||
|
// }
|
||||||
|
// return get_method
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_delete_method(actor Struct, object BaseObject) Function {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// body := 'actor.backend.delete[${object_type}](id)!'
|
||||||
|
// get_method := Function{
|
||||||
|
// name: 'delete_${object_name}'
|
||||||
|
// description: 'deletes the ${object.structure.name} with the given object id'
|
||||||
|
// receiver: Param{
|
||||||
|
// mutable: true
|
||||||
|
// name: 'actor'
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: actor.name
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// params: [generator.id_param]
|
||||||
|
// result: Param{
|
||||||
|
// is_result: true
|
||||||
|
// }
|
||||||
|
// body: body
|
||||||
|
// }
|
||||||
|
// return get_method
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_new_method(actor Struct, object BaseObject) Function {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// param_getters := generate_param_getters(
|
||||||
|
// structure: object.structure
|
||||||
|
// prefix: ''
|
||||||
|
// only_mutable: false
|
||||||
|
// )
|
||||||
|
// body := 'return actor.backend.new[${object_type}](${object_name})!'
|
||||||
|
// new_method := Function{
|
||||||
|
// name: 'new_${object_name}'
|
||||||
|
// description: 'news the ${object.structure.name} with the given object id'
|
||||||
|
// receiver: Param{
|
||||||
|
// name: 'actor'
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: actor.name
|
||||||
|
// }
|
||||||
|
// mutable: true
|
||||||
|
// }
|
||||||
|
// params: [
|
||||||
|
// Param{
|
||||||
|
// name: object_name
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: object_type
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// ]
|
||||||
|
// result: Param{
|
||||||
|
// is_result: true
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: 'u32'
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// body: body
|
||||||
|
// }
|
||||||
|
// return new_method
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_list_result_struct(actor Struct, object BaseObject) Struct {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
// return Struct{
|
||||||
|
// name: '${object_type}List'
|
||||||
|
// is_pub: true
|
||||||
|
// fields: [
|
||||||
|
// StructField{
|
||||||
|
// name: 'items'
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: '[]${object_type}'
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_list_method(actor Struct, object BaseObject) Function {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// list_struct := Struct{
|
||||||
|
// name: '${object_type}List'
|
||||||
|
// fields: [
|
||||||
|
// StructField{
|
||||||
|
// name: 'items'
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: '[]${object_type}'
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
|
||||||
|
// param_getters := generate_param_getters(
|
||||||
|
// structure: object.structure
|
||||||
|
// prefix: ''
|
||||||
|
// only_mutable: false
|
||||||
|
// )
|
||||||
|
// body := 'return ${object_type}List{items:actor.backend.list[${object_type}]()!}'
|
||||||
|
|
||||||
|
// result_struct := generate_list_result_struct(actor, object)
|
||||||
|
// mut result := Param{}
|
||||||
|
// result.typ.symbol = result_struct.name
|
||||||
|
// result.is_result = true
|
||||||
|
// new_method := Function{
|
||||||
|
// name: 'list_${object_name}'
|
||||||
|
// description: 'lists all of the ${object_name} objects'
|
||||||
|
// receiver: Param{
|
||||||
|
// name: 'actor'
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: actor.name
|
||||||
|
// }
|
||||||
|
// mutable: true
|
||||||
|
// }
|
||||||
|
// params: []
|
||||||
|
// result: result
|
||||||
|
// body: body
|
||||||
|
// }
|
||||||
|
// return new_method
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn generate_filter_params(actor Struct, object BaseObject) []Struct {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// return [
|
||||||
|
// Struct{
|
||||||
|
// name: 'Filter${object_type}Params'
|
||||||
|
// fields: [
|
||||||
|
// StructField{
|
||||||
|
// name: 'filter'
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: '${object_type}Filter'
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// StructField{
|
||||||
|
// name: 'params'
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: 'FilterParams'
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// ]
|
||||||
|
// },
|
||||||
|
// Struct{
|
||||||
|
// name: '${object_type}Filter'
|
||||||
|
// fields: object.structure.fields.filter(it.attrs.any(it.name == 'index'))
|
||||||
|
// },
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_filter_method(actor Struct, object BaseObject) Function {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// param_getters := generate_param_getters(
|
||||||
|
// structure: object.structure
|
||||||
|
// prefix: ''
|
||||||
|
// only_mutable: false
|
||||||
|
// )
|
||||||
|
// params_type := 'Filter${object_type}Params'
|
||||||
|
// body := 'return actor.backend.filter[${object_type}, ${object_type}Filter](filter.filter, filter.params)!'
|
||||||
|
// return Function{
|
||||||
|
// name: 'filter_${object_name}'
|
||||||
|
// description: 'lists all of the ${object_name} objects'
|
||||||
|
// receiver: Param{
|
||||||
|
// name: 'actor'
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: actor.name
|
||||||
|
// }
|
||||||
|
// mutable: true
|
||||||
|
// }
|
||||||
|
// params: [
|
||||||
|
// Param{
|
||||||
|
// name: 'filter'
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: params_type
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// ]
|
||||||
|
// result: Param{
|
||||||
|
// typ: Type{
|
||||||
|
// symbol: '[]${object_type}'
|
||||||
|
// }
|
||||||
|
// is_result: true
|
||||||
|
// }
|
||||||
|
// body: body
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// // fn generate_object_methods(actor Struct, object BaseObject) []Function {
|
||||||
|
// // object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// // object_type := object.structure.name
|
||||||
|
|
||||||
|
// // mut funcs := []Function{}
|
||||||
|
// // for method in object.methods {
|
||||||
|
// // mut params := [Param{
|
||||||
|
// // name: 'id'
|
||||||
|
// // typ: Type{
|
||||||
|
// // symbol: 'u32'
|
||||||
|
// // }
|
||||||
|
// // }]
|
||||||
|
// // params << method.params
|
||||||
|
// // funcs << Function{
|
||||||
|
// // name: method.name
|
||||||
|
// // description: method.description
|
||||||
|
// // receiver: Param{
|
||||||
|
// // name: 'actor'
|
||||||
|
// // typ: Type{
|
||||||
|
// // symbol: actor.name
|
||||||
|
// // }
|
||||||
|
// // mutable: true
|
||||||
|
// // }
|
||||||
|
// // params: params
|
||||||
|
// // result: method.result
|
||||||
|
// // body: 'obj := actor.backend.get[${method.receiver.typ.symbol}](id)!
|
||||||
|
// // obj.${method.name}(${method.params.map(it.name).join(',')})
|
||||||
|
// // actor.backend.set[${method.receiver.typ.symbol}](obj)!
|
||||||
|
// // '
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// // return funcs
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// @[params]
|
||||||
|
// struct GenerateParamGetters {
|
||||||
|
// structure Struct
|
||||||
|
// prefix string
|
||||||
|
// only_mutable bool // if true generates param.get methods for only mutable struct fields. Used for updating.
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn generate_param_getters(params GenerateParamGetters) []string {
|
||||||
|
// mut param_getters := []string{}
|
||||||
|
// fields := if params.only_mutable {
|
||||||
|
// params.structure.fields.filter(it.is_mut && it.is_pub)
|
||||||
|
// } else {
|
||||||
|
// params.structure.fields.filter(it.is_pub)
|
||||||
|
// }
|
||||||
|
// for field in fields {
|
||||||
|
// if field.typ.symbol.starts_with_capital() {
|
||||||
|
// subgetters := generate_param_getters(GenerateParamGetters{
|
||||||
|
// ...params
|
||||||
|
// structure: field.structure
|
||||||
|
// prefix: '${field.name}_'
|
||||||
|
// })
|
||||||
|
// // name of the tested object, used for param declaration
|
||||||
|
// // ex: fruits []Fruit becomes fruit_name
|
||||||
|
// nested_name := field.structure.name.to_lower()
|
||||||
|
// if field.typ.is_map {
|
||||||
|
// param_getters.insert(0, '${nested_name}_key := params.get(\'${nested_name}_key\')!')
|
||||||
|
// param_getters << '${field.name}: {${nested_name}_key: ${field.structure.name}}{'
|
||||||
|
// } else if field.typ.is_array {
|
||||||
|
// param_getters << '${field.name}: [${field.structure.name}{'
|
||||||
|
// } else {
|
||||||
|
// param_getters << '${field.name}: ${field.structure.name}{'
|
||||||
|
// }
|
||||||
|
// param_getters << subgetters
|
||||||
|
// param_getters << if field.typ.is_array { '}]' } else { '}' }
|
||||||
|
// continue
|
||||||
|
// }
|
||||||
|
|
||||||
|
// mut get_method := '${field.name}: params.get'
|
||||||
|
// if field.typ.symbol != 'string' {
|
||||||
|
// // TODO: check if params method actually exists
|
||||||
|
// 'get_${field.typ.symbol}'
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if field.default != '' {
|
||||||
|
// get_method += '_default'
|
||||||
|
// }
|
||||||
|
|
||||||
|
// get_method = get_method + "('${params.prefix}${field.name}')!"
|
||||||
|
// param_getters << get_method
|
||||||
|
// }
|
||||||
|
// return param_getters
|
||||||
|
// }
|
||||||
|
|
||||||
|
// @[params]
|
||||||
|
// struct GetChildField {
|
||||||
|
// parent Struct @[required]
|
||||||
|
// child Struct @[required]
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn get_child_field(params GetChildField) StructField {
|
||||||
|
// fields := params.parent.fields.filter(it.typ.symbol == 'map[string]&${params.child.name}')
|
||||||
|
// if fields.len != 1 {
|
||||||
|
// panic('this should never happen')
|
||||||
|
// }
|
||||||
|
// return fields[0]
|
||||||
|
// }
|
||||||
@@ -1,12 +1,10 @@
|
|||||||
module generator
|
module generator
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.codemodel
|
import freeflowuniverse.herolib.core.code
|
||||||
import freeflowuniverse.herolib.core.codeparser
|
|
||||||
import os
|
import os
|
||||||
import freeflowuniverse.herolib.ui.console
|
|
||||||
|
|
||||||
// // generate_object_methods generates CRUD actor methods for a provided structure
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
// pub fn (generator ActorGenerator) generate_object_methods(structure codemodel.Struct) []codemodel.Function {
|
// pub fn (generator ActorGenerator) generate_object_methods(structure code.Struct) []code.Function {
|
||||||
// return [
|
// return [
|
||||||
// generator.generate_get_method(structure),
|
// generator.generate_get_method(structure),
|
||||||
// // generator.generate_set_method(structure),
|
// // generator.generate_set_method(structure),
|
||||||
@@ -18,19 +16,19 @@ import freeflowuniverse.herolib.ui.console
|
|||||||
// generate_object_methods generates CRUD actor methods for a provided structure
|
// generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
pub fn test_generate_get_method() {
|
pub fn test_generate_get_method() {
|
||||||
generator := ActorGenerator{'test'}
|
generator := ActorGenerator{'test'}
|
||||||
actor_struct := codemodel.Struct{
|
actor_struct := code.Struct{
|
||||||
name: 'TestActor'
|
name: 'TestActor'
|
||||||
fields: [
|
fields: [
|
||||||
codemodel.StructField{
|
code.StructField{
|
||||||
name: 'test_struct_map'
|
name: 'test_struct_map'
|
||||||
typ: codemodel.Type{
|
typ: code.Type{
|
||||||
symbol: 'map[string]&TestStruct'
|
symbol: 'map[string]&TestStruct'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
test_struct := codemodel.Struct{
|
test_struct := code.Struct{
|
||||||
name: 'TestStruct'
|
name: 'TestStruct'
|
||||||
}
|
}
|
||||||
field := get_child_field(
|
field := get_child_field(
|
||||||
@@ -43,31 +41,30 @@ pub fn test_generate_get_method() {
|
|||||||
actor_field: field
|
actor_field: field
|
||||||
root_struct: test_struct
|
root_struct: test_struct
|
||||||
)
|
)
|
||||||
// console.print_debug(method.vgen())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// // generate_object_methods generates CRUD actor methods for a provided structure
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
// pub fn (generator ActorGenerator) generate_set_method(structure codemodel.Struct) codemodel.Function {
|
// pub fn (generator ActorGenerator) generate_set_method(structure code.Struct) code.Function {
|
||||||
// params_getter := "id := params.get('id')!"
|
// params_getter := "id := params.get('id')!"
|
||||||
// field := generator.get_object_field(structure)
|
// field := generator.get_object_field(structure)
|
||||||
// object_getter := 'object := actor.${field.name}[id]'
|
// object_getter := 'object := actor.${field.name}[id]'
|
||||||
// body := '${params_getter}\n${object_getter}\nreturn object'
|
// body := '${params_getter}\n${object_getter}\nreturn object'
|
||||||
// get_method := codemodel.Function{
|
// get_method := code.Function{
|
||||||
// name: 'get_${generator.model_name}'
|
// name: 'get_${generator.model_name}'
|
||||||
// description: 'gets the ${structure.name} with the given object id'
|
// description: 'gets the ${structure.name} with the given object id'
|
||||||
// receiver: codemodel.Param{
|
// receiver: code.Param{
|
||||||
// name: 'actor'
|
// name: 'actor'
|
||||||
// struct_: generator.actor_struct
|
// struct_: generator.actor_struct
|
||||||
// }
|
// }
|
||||||
// params: [
|
// params: [
|
||||||
// codemodel.Param{
|
// code.Param{
|
||||||
// name: 'id'
|
// name: 'id'
|
||||||
// typ: codemodel.Type{
|
// typ: code.Type{
|
||||||
// symbol: 'string'
|
// symbol: 'string'
|
||||||
// }
|
// }
|
||||||
// },
|
// },
|
||||||
// ]
|
// ]
|
||||||
// result: codemodel.Result{
|
// result: code.Result{
|
||||||
// structure: structure
|
// structure: structure
|
||||||
// }
|
// }
|
||||||
// body: body
|
// body: body
|
||||||
@@ -76,27 +73,27 @@ pub fn test_generate_get_method() {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// // generate_object_methods generates CRUD actor methods for a provided structure
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
// pub fn (generator ActorGenerator) generate_get_method(structure codemodel.Struct) codemodel.Function {
|
// pub fn (generator ActorGenerator) generate_get_method(structure code.Struct) code.Function {
|
||||||
// params_getter := "id := params.get('id')!"
|
// params_getter := "id := params.get('id')!"
|
||||||
// field := generator.get_object_field(structure)
|
// field := generator.get_object_field(structure)
|
||||||
// object_getter := 'object := actor.${field.name}[id]'
|
// object_getter := 'object := actor.${field.name}[id]'
|
||||||
// body := '${params_getter}\n${object_getter}\nreturn object'
|
// body := '${params_getter}\n${object_getter}\nreturn object'
|
||||||
// get_method := codemodel.Function{
|
// get_method := code.Function{
|
||||||
// name: 'get_${generator.model_name}'
|
// name: 'get_${generator.model_name}'
|
||||||
// description: 'gets the ${structure.name} with the given object id'
|
// description: 'gets the ${structure.name} with the given object id'
|
||||||
// receiver: codemodel.Param{
|
// receiver: code.Param{
|
||||||
// name: 'actor'
|
// name: 'actor'
|
||||||
// struct_: generator.actor_struct
|
// struct_: generator.actor_struct
|
||||||
// }
|
// }
|
||||||
// params: [
|
// params: [
|
||||||
// codemodel.Param{
|
// code.Param{
|
||||||
// name: 'id'
|
// name: 'id'
|
||||||
// typ: codemodel.Type{
|
// typ: code.Type{
|
||||||
// symbol: 'string'
|
// symbol: 'string'
|
||||||
// }
|
// }
|
||||||
// },
|
// },
|
||||||
// ]
|
// ]
|
||||||
// result: codemodel.Result{
|
// result: code.Result{
|
||||||
// structure: structure
|
// structure: structure
|
||||||
// }
|
// }
|
||||||
// body: body
|
// body: body
|
||||||
@@ -105,27 +102,27 @@ pub fn test_generate_get_method() {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// // generate_object_methods generates CRUD actor methods for a provided structure
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
// pub fn (generator ActorGenerator) generate_delete_method(structure codemodel.Struct) codemodel.Function {
|
// pub fn (generator ActorGenerator) generate_delete_method(structure code.Struct) code.Function {
|
||||||
// params_getter := "id := params.get('id')!"
|
// params_getter := "id := params.get('id')!"
|
||||||
// field := generator.get_object_field(structure)
|
// field := generator.get_object_field(structure)
|
||||||
// object_getter := 'object := actor.${field.name}[id]'
|
// object_getter := 'object := actor.${field.name}[id]'
|
||||||
// body := '${params_getter}\n${object_getter}\nreturn object'
|
// body := '${params_getter}\n${object_getter}\nreturn object'
|
||||||
// get_method := codemodel.Function{
|
// get_method := code.Function{
|
||||||
// name: 'get_${generator.model_name}'
|
// name: 'get_${generator.model_name}'
|
||||||
// description: 'gets the ${structure.name} with the given object id'
|
// description: 'gets the ${structure.name} with the given object id'
|
||||||
// receiver: codemodel.Param{
|
// receiver: code.Param{
|
||||||
// name: 'actor'
|
// name: 'actor'
|
||||||
// struct_: generator.actor_struct
|
// struct_: generator.actor_struct
|
||||||
// }
|
// }
|
||||||
// params: [
|
// params: [
|
||||||
// codemodel.Param{
|
// code.Param{
|
||||||
// name: 'id'
|
// name: 'id'
|
||||||
// typ: codemodel.Type{
|
// typ: code.Type{
|
||||||
// symbol: 'string'
|
// symbol: 'string'
|
||||||
// }
|
// }
|
||||||
// },
|
// },
|
||||||
// ]
|
// ]
|
||||||
// result: codemodel.Result{
|
// result: code.Result{
|
||||||
// structure: structure
|
// structure: structure
|
||||||
// }
|
// }
|
||||||
// body: body
|
// body: body
|
||||||
@@ -133,7 +130,7 @@ pub fn test_generate_get_method() {
|
|||||||
// return get_method
|
// return get_method
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// pub fn (generator ActorGenerator) get_object_field(structure codemodel.Struct) codemodel.StructField {
|
// pub fn (generator ActorGenerator) get_object_field(structure code.Struct) code.StructField {
|
||||||
// fields := generator.actor_struct.fields.filter(it.typ.symbol == 'map[string]&${structure.name}')
|
// fields := generator.actor_struct.fields.filter(it.typ.symbol == 'map[string]&${structure.name}')
|
||||||
// if fields.len != 1 {
|
// if fields.len != 1 {
|
||||||
// panic('this should never happen')
|
// panic('this should never happen')
|
||||||
168
lib/baobab/generator/write_object_tests.v
Normal file
168
lib/baobab/generator/write_object_tests.v
Normal file
@@ -0,0 +1,168 @@
|
|||||||
|
module generator
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { VFile, CustomCode, Function, Import, Struct }
|
||||||
|
import freeflowuniverse.herolib.baobab.specification {BaseObject}
|
||||||
|
import rand
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// pub fn generate_object_test_code(actor Struct, object BaseObject) !VFile {
|
||||||
|
// consts := CustomCode{"const db_dir = '\${os.home_dir()}/hero/db'
|
||||||
|
// const actor_name = '${actor.name}_test_actor'"}
|
||||||
|
|
||||||
|
// clean_code := 'mut actor := get(name: actor_name)!\nactor.backend.reset()!'
|
||||||
|
|
||||||
|
// testsuite_begin := Function{
|
||||||
|
// name: 'testsuite_begin'
|
||||||
|
// body: clean_code
|
||||||
|
// }
|
||||||
|
|
||||||
|
// testsuite_end := Function{
|
||||||
|
// name: 'testsuite_end'
|
||||||
|
// body: clean_code
|
||||||
|
// }
|
||||||
|
|
||||||
|
// actor_name := texttools.name_fix(actor.name)
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.schema.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
// // TODO: support modules outside of crystal
|
||||||
|
|
||||||
|
// mut file := VFile{
|
||||||
|
// name: '${object_name}_test'
|
||||||
|
// mod: texttools.name_fix(actor_name)
|
||||||
|
// imports: [
|
||||||
|
// Import{
|
||||||
|
// mod: 'os'
|
||||||
|
// },
|
||||||
|
// Import{
|
||||||
|
// mod: '${object.structure.mod}'
|
||||||
|
// types: [object_type]
|
||||||
|
// },
|
||||||
|
// ]
|
||||||
|
// items: [
|
||||||
|
// consts,
|
||||||
|
// testsuite_begin,
|
||||||
|
// testsuite_end,
|
||||||
|
// generate_new_method_test(actor, object)!,
|
||||||
|
// generate_get_method_test(actor, object)!,
|
||||||
|
// ]
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if object.structure.fields.any(it.attrs.any(it.name == 'index')) {
|
||||||
|
// // can't filter without indices
|
||||||
|
// file.items << generate_filter_test(actor, object)!
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return file
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_new_method_test(actor Struct, object BaseObject) !Function {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// required_fields := object.structure.fields.filter(it.attrs.any(it.name == 'required'))
|
||||||
|
// mut fields := []string{}
|
||||||
|
// for field in required_fields {
|
||||||
|
// mut field_decl := '${field.name}: ${get_mock_value(field.typ.symbol())!}'
|
||||||
|
// fields << field_decl
|
||||||
|
// }
|
||||||
|
|
||||||
|
// body := 'mut actor := get(name: actor_name)!
|
||||||
|
// mut ${object_name}_id := actor.new_${object_name}(${object_type}{${fields.join(',')}})!
|
||||||
|
// assert ${object_name}_id == 1
|
||||||
|
|
||||||
|
// ${object_name}_id = actor.new_${object_name}(${object_type}{${fields.join(',')}})!
|
||||||
|
// assert ${object_name}_id == 2'
|
||||||
|
// return Function{
|
||||||
|
// name: 'test_new_${object_name}'
|
||||||
|
// description: 'news the ${object_type} with the given object id'
|
||||||
|
// result: code.Param{
|
||||||
|
// is_result: true
|
||||||
|
// }
|
||||||
|
// body: body
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_get_method_test(actor Struct, object BaseObject) !Function {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// required_fields := object.structure.fields.filter(it.attrs.any(it.name == 'required'))
|
||||||
|
// mut fields := []string{}
|
||||||
|
// for field in required_fields {
|
||||||
|
// mut field_decl := '${field.name}: ${get_mock_value(field.typ.symbol())!}'
|
||||||
|
// fields << field_decl
|
||||||
|
// }
|
||||||
|
|
||||||
|
// body := 'mut actor := get(name: actor_name)!
|
||||||
|
// mut ${object_name} := ${object_type}{${fields.join(',')}}
|
||||||
|
// ${object_name}.id = actor.new_${object_name}(${object_name})!
|
||||||
|
// assert ${object_name} == actor.get_${object_name}(${object_name}.id)!'
|
||||||
|
// return Function{
|
||||||
|
// name: 'test_get_${object_name}'
|
||||||
|
// description: 'news the ${object_type} with the given object id'
|
||||||
|
// result: code.Param{
|
||||||
|
// is_result: true
|
||||||
|
// }
|
||||||
|
// body: body
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // generate_object_methods generates CRUD actor methods for a provided structure
|
||||||
|
// fn generate_filter_test(actor Struct, object BaseObject) !Function {
|
||||||
|
// object_name := texttools.name_fix_pascal_to_snake(object.structure.name)
|
||||||
|
// object_type := object.structure.name
|
||||||
|
|
||||||
|
// index_fields := object.structure.fields.filter(it.attrs.any(it.name == 'index'))
|
||||||
|
// if index_fields.len == 0 {
|
||||||
|
// return error('Cannot generate filter method test for object without any index fields')
|
||||||
|
// }
|
||||||
|
|
||||||
|
// mut index_tests := []string{}
|
||||||
|
// for i, field in index_fields {
|
||||||
|
// val := get_mock_value(field.typ.symbol())!
|
||||||
|
// index_field := '${field.name}: ${val}' // index field assignment line
|
||||||
|
// mut fields := [index_field]
|
||||||
|
// fields << get_required_fields(object.structure)!
|
||||||
|
// index_tests << '${object_name}_id${i} := actor.new_${object_name}(${object_type}{${fields.join(',')}})!
|
||||||
|
// ${object_name}_list${i} := actor.filter_${object_name}(
|
||||||
|
// filter: ${object_type}Filter{${index_field}}
|
||||||
|
// )!
|
||||||
|
// assert ${object_name}_list${i}.len == 1
|
||||||
|
// assert ${object_name}_list${i}[0].${field.name} == ${val}
|
||||||
|
// '
|
||||||
|
// }
|
||||||
|
|
||||||
|
// body := 'mut actor := get(name: actor_name)!
|
||||||
|
// \n${index_tests.join('\n\n')}'
|
||||||
|
|
||||||
|
// return Function{
|
||||||
|
// name: 'test_filter_${object_name}'
|
||||||
|
// description: 'news the ${object_type} with the given object id'
|
||||||
|
// result: code.Param{
|
||||||
|
// is_result: true
|
||||||
|
// }
|
||||||
|
// body: body
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn get_required_fields(s Struct) ![]string {
|
||||||
|
// required_fields := s.fields.filter(it.attrs.any(it.name == 'required'))
|
||||||
|
// mut fields := []string{}
|
||||||
|
// for field in required_fields {
|
||||||
|
// fields << '${field.name}: ${get_mock_value(field.typ.symbol())!}'
|
||||||
|
// }
|
||||||
|
// return fields
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn get_mock_value(typ string) !string {
|
||||||
|
// if typ == 'string' {
|
||||||
|
// return "'mock_string_${rand.string(3)}'"
|
||||||
|
// } else if typ == 'int' || typ == 'u32' {
|
||||||
|
// return '42'
|
||||||
|
// } else {
|
||||||
|
// return error('mock values for types other than strings and numbers are not yet supported')
|
||||||
|
// }
|
||||||
|
// }
|
||||||
8
lib/baobab/osis/factory.v
Normal file
8
lib/baobab/osis/factory.v
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
module osis
|
||||||
|
|
||||||
|
pub fn new(config OSISConfig) !OSIS {
|
||||||
|
return OSIS{
|
||||||
|
indexer: new_indexer()!
|
||||||
|
storer: new_storer()!
|
||||||
|
}
|
||||||
|
}
|
||||||
116
lib/baobab/osis/indexer.v
Normal file
116
lib/baobab/osis/indexer.v
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
module osis
|
||||||
|
|
||||||
|
import json
|
||||||
|
import db.sqlite
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
import freeflowuniverse.herolib.core.pathlib
|
||||||
|
|
||||||
|
pub struct Indexer {
|
||||||
|
db sqlite.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
@[params]
|
||||||
|
pub struct IndexerConfig {
|
||||||
|
db_path string
|
||||||
|
reset bool
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_indexer(config IndexerConfig) !Indexer {
|
||||||
|
return Indexer{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// deletes an indexer table belonging to a base object
|
||||||
|
pub fn reset(path string) ! {
|
||||||
|
mut db_file := pathlib.get_file(path: path)!
|
||||||
|
db_file.delete()!
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut i Indexer) new_generic[T](id u32, object T) !u32 {
|
||||||
|
return i.new(get_table[T](), id, get_indices[T](object))!
|
||||||
|
}
|
||||||
|
|
||||||
|
// new creates a new root object entry in the root_objects table,
|
||||||
|
// and the table belonging to the type of root object with columns for index fields
|
||||||
|
pub fn (mut i Indexer) new(table string, id u32, indices map[string]string) !u32 {
|
||||||
|
insert_query := 'INSERT into ${table} (${indices.keys().join(',')}) values (${indices.values().join(',')})'
|
||||||
|
i.db.exec(insert_query) or {
|
||||||
|
return error('Error inserting object ${id} into table ${table}\n${err}')
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the session to redis & mem
|
||||||
|
pub fn (mut backend Indexer) set(obj RootObject) ! {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the session to redis & mem
|
||||||
|
pub fn (mut backend Indexer) delete(id string, obj RootObject) ! {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut backend Indexer) get(id string, obj RootObject) !RootObject {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut backend Indexer) get_json(id string, obj RootObject) !string {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut backend Indexer) list(obj RootObject) ![]u32 {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
// from and to for int f64 time etc.
|
||||||
|
@[params]
|
||||||
|
pub struct FilterParams {
|
||||||
|
// indices map[string]string // map of index values that are being filtered by, in order of priority.
|
||||||
|
limit int // limit to the number of values to be returned, in order of priority
|
||||||
|
fuzzy bool // if fuzzy matching is enabled in matching indices
|
||||||
|
matches_all bool // if results should match all indices or any
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter lists root objects of type T that match provided index parameters and params.
|
||||||
|
pub fn (mut backend Indexer) filter(filter RootObject, params FilterParams) ![]string {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
// create_root_struct_table creates a table for a root_struct with columns for each index field
|
||||||
|
fn (mut backend Indexer) create_root_object_table(object RootObject) ! {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
// deletes an indexer table belonging to a root object
|
||||||
|
fn (mut backend Indexer) delete_table(object RootObject)! {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut backend Indexer) get_table_indices(table_name string) ![]string {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (mut backend Indexer) table_exists(table_name string) !bool {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
// get_table_name returns the name of the table belonging to a root struct
|
||||||
|
fn get_table_name(object RootObject) string {
|
||||||
|
panic('implement')
|
||||||
|
}
|
||||||
|
|
||||||
|
// get_table_name returns the name of the table belonging to a root struct
|
||||||
|
fn get_table[T]() string {
|
||||||
|
return typeof[T]()
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns the lists of the indices of a root objects db table, and corresponding values
|
||||||
|
pub fn get_indices[T](object T) map[string]string {
|
||||||
|
mut indices := map[string]string
|
||||||
|
$for field in T.fields {
|
||||||
|
if field.attrs.contains('index') {
|
||||||
|
value := object.$(field.name)
|
||||||
|
indices[field.name] = '${value}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return indices
|
||||||
|
}
|
||||||
16
lib/baobab/osis/model.v
Normal file
16
lib/baobab/osis/model.v
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
module osis
|
||||||
|
|
||||||
|
pub struct OSIS {
|
||||||
|
pub mut:
|
||||||
|
indexer Indexer // storing indeces
|
||||||
|
storer Storer
|
||||||
|
}
|
||||||
|
|
||||||
|
@[params]
|
||||||
|
pub struct OSISConfig {
|
||||||
|
pub:
|
||||||
|
directory string
|
||||||
|
name string
|
||||||
|
secret string
|
||||||
|
reset bool
|
||||||
|
}
|
||||||
@@ -1,10 +1,6 @@
|
|||||||
module osis
|
module osis
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import db.sqlite
|
|
||||||
import db.pg
|
|
||||||
import freeflowuniverse.herolib.data.dbfs
|
|
||||||
import freeflowuniverse.herolib.data.encoderhero
|
|
||||||
|
|
||||||
pub fn (mut o OSIS) generic_new[T](obj T) !u32 {
|
pub fn (mut o OSIS) generic_new[T](obj T) !u32 {
|
||||||
id := o.indexer.generic_new[T](obj)!
|
id := o.indexer.generic_new[T](obj)!
|
||||||
@@ -12,10 +8,20 @@ pub fn (mut o OSIS) generic_new[T](obj T) !u32 {
|
|||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (mut o OSIS) new[T](obj T) !u32 {
|
||||||
|
id := o.storer.new_generic[T](obj)!
|
||||||
|
o.indexer.new_generic[T](id, obj)!
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (mut o OSIS) generic_get[T](id u32) !T {
|
pub fn (mut o OSIS) generic_get[T](id u32) !T {
|
||||||
return o.storer.generic_get[T](id)!
|
return o.storer.generic_get[T](id)!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (mut o OSIS) get[T](id u32) !T {
|
||||||
|
return o.storer.generic_get[T](u32(id))!
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (mut o OSIS) generic_set[T](obj T) ! {
|
pub fn (mut o OSIS) generic_set[T](obj T) ! {
|
||||||
o.indexer.generic_set[T](obj) or { return error('Failed to set new indices:\n${err}') }
|
o.indexer.generic_set[T](obj) or { return error('Failed to set new indices:\n${err}') }
|
||||||
o.storer.generic_set[T](obj)!
|
o.storer.generic_set[T](obj)!
|
||||||
@@ -26,6 +32,16 @@ pub fn (mut o OSIS) generic_delete[T](id u32) ! {
|
|||||||
o.storer.generic_delete[T](id)!
|
o.storer.generic_delete[T](id)!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn (mut o OSIS) delete(id u32) ! {
|
||||||
|
o.storer.delete(u32(id))!
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut o OSIS) list[T]() ![]T {
|
||||||
|
panic('implement')
|
||||||
|
// ids := o.indexer.generic_list[T]()!
|
||||||
|
// return o.storer.generic_list[T](ids)!
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (mut o OSIS) generic_list[T]() ![]T {
|
pub fn (mut o OSIS) generic_list[T]() ![]T {
|
||||||
ids := o.indexer.generic_list[T]()!
|
ids := o.indexer.generic_list[T]()!
|
||||||
return o.storer.generic_list[T](ids)!
|
return o.storer.generic_list[T](ids)!
|
||||||
15
lib/baobab/osis/storer.v
Normal file
15
lib/baobab/osis/storer.v
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
module osis
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.data.ourdb {OurDB}
|
||||||
|
import os
|
||||||
|
|
||||||
|
pub struct Storer {
|
||||||
|
pub mut:
|
||||||
|
db OurDB
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_storer() !Storer {
|
||||||
|
return Storer {
|
||||||
|
db: ourdb.new()!
|
||||||
|
}
|
||||||
|
}
|
||||||
23
lib/baobab/osis/storer_generic.v
Normal file
23
lib/baobab/osis/storer_generic.v
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
module osis
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
// new creates a new root object entry in the root_objects table,
|
||||||
|
// and the table belonging to the type of root object with columns for index fields
|
||||||
|
pub fn (mut storer Storer) new_generic[T](obj T) !u32 {
|
||||||
|
data := json.encode(obj).bytes()
|
||||||
|
return storer.db.set(data: data)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut storer Storer) generic_get[T](id u32) !T {
|
||||||
|
return json.decode(T, storer.db.get(id)!.bytestr())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut storer Storer) generic_set[T](obj T) ! {
|
||||||
|
data := json.encode(obj).bytes()
|
||||||
|
return storer.db.set(data: data)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut storer Storer) delete(id u32) ! {
|
||||||
|
storer.db.delete(id)!
|
||||||
|
}
|
||||||
150
lib/baobab/specification/from_openapi.v
Normal file
150
lib/baobab/specification/from_openapi.v
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
module specification
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { Struct, Function }
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema { Schema, SchemaRef }
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi { Operation, Parameter, OpenAPI, Components, Info, PathItem, ServerSpec }
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc { ExamplePairing, Example, ExampleRef, ContentDescriptor, ErrorSpec }
|
||||||
|
|
||||||
|
// Helper function: Convert OpenAPI parameter to ContentDescriptor
|
||||||
|
fn openapi_param_to_content_descriptor(param Parameter) ContentDescriptor {
|
||||||
|
return ContentDescriptor{
|
||||||
|
name: param.name,
|
||||||
|
summary: param.description,
|
||||||
|
description: param.description,
|
||||||
|
required: param.required,
|
||||||
|
schema: param.schema
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function: Convert OpenAPI parameter to ContentDescriptor
|
||||||
|
fn openapi_param_to_example(param Parameter) ?Example {
|
||||||
|
if param.schema is Schema {
|
||||||
|
if param.schema.example.str() != '' {
|
||||||
|
return Example{
|
||||||
|
name: 'Example ${param.name}',
|
||||||
|
description: 'Example ${param.description}',
|
||||||
|
value: param.schema.example
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return none
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function: Convert OpenAPI operation to ActorMethod
|
||||||
|
fn openapi_operation_to_actor_method(info openapi.OperationInfo) ActorMethod {
|
||||||
|
mut parameters := []ContentDescriptor{}
|
||||||
|
mut example_parameters:= []Example{}
|
||||||
|
|
||||||
|
for param in info.operation.parameters {
|
||||||
|
parameters << openapi_param_to_content_descriptor(param)
|
||||||
|
example_parameters << openapi_param_to_example(param) or {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response_200 := info.operation.responses['200'].content['application/json']
|
||||||
|
|
||||||
|
mut result := ContentDescriptor{
|
||||||
|
name: "result",
|
||||||
|
description: "The response of the operation.",
|
||||||
|
required: true,
|
||||||
|
schema: response_200.schema
|
||||||
|
}
|
||||||
|
|
||||||
|
example_result := if response_200.example.str() != '' {
|
||||||
|
Example{
|
||||||
|
name: 'Example response',
|
||||||
|
value: response_200.example
|
||||||
|
}
|
||||||
|
} else {Example{}}
|
||||||
|
|
||||||
|
pairing := if example_result != Example{} || example_parameters.len > 0 {
|
||||||
|
ExamplePairing{
|
||||||
|
params: example_parameters.map(ExampleRef(it))
|
||||||
|
result: ExampleRef(example_result)
|
||||||
|
}
|
||||||
|
} else {ExamplePairing{}}
|
||||||
|
|
||||||
|
mut errors := []ErrorSpec{}
|
||||||
|
for status, response in info.operation.responses {
|
||||||
|
if status.int() >= 400 {
|
||||||
|
error_schema := if response.content.len > 0 {
|
||||||
|
response.content.values()[0].schema
|
||||||
|
} else {Schema{}}
|
||||||
|
errors << ErrorSpec{
|
||||||
|
code: status.int(),
|
||||||
|
message: response.description,
|
||||||
|
data: error_schema, // Extend if error schema is defined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ActorMethod{
|
||||||
|
name: info.operation.operation_id,
|
||||||
|
description: info.operation.description,
|
||||||
|
summary: info.operation.summary,
|
||||||
|
parameters: parameters,
|
||||||
|
example: pairing
|
||||||
|
result: result,
|
||||||
|
errors: errors,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function: Convert OpenAPI schema to Struct
|
||||||
|
fn openapi_schema_to_struct(name string, schema SchemaRef) Struct {
|
||||||
|
// Assuming schema properties can be mapped to Struct fields
|
||||||
|
return Struct{
|
||||||
|
name: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converts OpenAPI to ActorSpecification
|
||||||
|
pub fn from_openapi(spec_raw OpenAPI) !ActorSpecification {
|
||||||
|
spec := openapi.process(spec_raw)!
|
||||||
|
mut objects := []BaseObject{}
|
||||||
|
|
||||||
|
// get all operations for path as list of tuple [](path_string, http.Method, openapi.Operation)
|
||||||
|
|
||||||
|
// Extract methods from OpenAPI paths
|
||||||
|
// for path, item in spec.paths {
|
||||||
|
// if item.get.operation_id != '' {
|
||||||
|
// methods << openapi_operation_to_actor_method(item.get, item.get.operation_id, path)
|
||||||
|
// }
|
||||||
|
// if item.post.operation_id != '' {
|
||||||
|
// methods << openapi_operation_to_actor_method(item.post, item.post.operation_id, path)
|
||||||
|
// }
|
||||||
|
// if item.put.operation_id != '' {
|
||||||
|
// methods << openapi_operation_to_actor_method(item.put, item.put.operation_id, path)
|
||||||
|
// }
|
||||||
|
// if item.delete.operation_id != '' {
|
||||||
|
// methods << openapi_operation_to_actor_method(item.delete, item.delete.operation_id, path)
|
||||||
|
// }
|
||||||
|
// if item.patch.operation_id != '' {
|
||||||
|
// methods << openapi_operation_to_actor_method(item.patch, item.patch.operation_id, path)
|
||||||
|
// }
|
||||||
|
// if item.head.operation_id != '' {
|
||||||
|
// methods << openapi_operation_to_actor_method(item.head, item.head.operation_id, path)
|
||||||
|
// }
|
||||||
|
// if item.options.operation_id != '' {
|
||||||
|
// methods << openapi_operation_to_actor_method(item.options, item.options.operation_id, path)
|
||||||
|
// }
|
||||||
|
// if item.trace.operation_id != '' {
|
||||||
|
// methods << openapi_operation_to_actor_method(item.trace, item.trace.operation_id, path)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Extract objects from OpenAPI components.schemas
|
||||||
|
for name, schema in spec.components.schemas {
|
||||||
|
objects << BaseObject{schema: schema as Schema}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ActorSpecification{
|
||||||
|
openapi: spec_raw
|
||||||
|
name: spec.info.title,
|
||||||
|
description: spec.info.description,
|
||||||
|
structure: Struct{}, // Assuming no top-level structure for this use case
|
||||||
|
interfaces: [.openapi], // Default to OpenAPI for input
|
||||||
|
methods: spec.get_operations().map(openapi_operation_to_actor_method(it)),
|
||||||
|
objects: objects,
|
||||||
|
}
|
||||||
|
}
|
||||||
400
lib/baobab/specification/from_openapi_test.v
Normal file
400
lib/baobab/specification/from_openapi_test.v
Normal file
@@ -0,0 +1,400 @@
|
|||||||
|
module specification
|
||||||
|
|
||||||
|
import x.json2 as json {Any}
|
||||||
|
import freeflowuniverse.herolib.core.code { Struct, Function }
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc { ContentDescriptor, ErrorSpec }
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi { OpenAPI, Info, ServerSpec, Components, Operation, PathItem, PathRef }
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema {Schema, Reference, SchemaRef}
|
||||||
|
|
||||||
|
const openapi_spec = openapi.OpenAPI{
|
||||||
|
openapi: '3.0.3'
|
||||||
|
info: openapi.Info{
|
||||||
|
title: 'Pet Store API'
|
||||||
|
description: 'A sample API for a pet store'
|
||||||
|
version: '1.0.0'
|
||||||
|
}
|
||||||
|
servers: [
|
||||||
|
openapi.ServerSpec{
|
||||||
|
url: 'https://api.petstore.example.com/v1'
|
||||||
|
description: 'Production server'
|
||||||
|
},
|
||||||
|
openapi.ServerSpec{
|
||||||
|
url: 'https://staging.petstore.example.com/v1'
|
||||||
|
description: 'Staging server'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
paths: {
|
||||||
|
'/pets': openapi.PathItem{
|
||||||
|
get: openapi.Operation{
|
||||||
|
summary: 'List all pets'
|
||||||
|
operation_id: 'listPets'
|
||||||
|
parameters: [
|
||||||
|
openapi.Parameter{
|
||||||
|
name: 'limit'
|
||||||
|
in_: 'query'
|
||||||
|
description: 'Maximum number of pets to return'
|
||||||
|
required: false
|
||||||
|
schema: Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
format: 'int32'
|
||||||
|
example: 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
responses: {
|
||||||
|
'200': openapi.ResponseSpec{
|
||||||
|
description: 'A paginated list of pets'
|
||||||
|
content: {
|
||||||
|
'application/json': openapi.MediaType{
|
||||||
|
schema: Reference{
|
||||||
|
ref: '#/components/schemas/Pets'
|
||||||
|
}
|
||||||
|
example: json.raw_decode('[
|
||||||
|
{ "id": 1, "name": "Fluffy", "tag": "dog" },
|
||||||
|
{ "id": 2, "name": "Whiskers", "tag": "cat" }
|
||||||
|
]')!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'400': openapi.ResponseSpec{
|
||||||
|
description: 'Invalid request'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
post: openapi.Operation{
|
||||||
|
summary: 'Create a new pet'
|
||||||
|
operation_id: 'createPet'
|
||||||
|
request_body: openapi.RequestBody{
|
||||||
|
required: true
|
||||||
|
content: {
|
||||||
|
'application/json': openapi.MediaType{
|
||||||
|
schema: Reference{
|
||||||
|
ref: '#/components/schemas/NewPet'
|
||||||
|
}
|
||||||
|
example: json.raw_decode('{ "name": "Bella", "tag": "dog" }')!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
responses: {
|
||||||
|
'201': openapi.ResponseSpec{
|
||||||
|
description: 'Pet created'
|
||||||
|
content: {
|
||||||
|
'application/json': openapi.MediaType{
|
||||||
|
schema: Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
}
|
||||||
|
example: json.raw_decode('{ "id": 3, "name": "Bella", "tag": "dog" }')!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'400': openapi.ResponseSpec{
|
||||||
|
description: 'Invalid input'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'/pets/{petId}': openapi.PathItem{
|
||||||
|
get: openapi.Operation{
|
||||||
|
summary: 'Get a pet by ID'
|
||||||
|
operation_id: 'getPet'
|
||||||
|
parameters: [
|
||||||
|
openapi.Parameter{
|
||||||
|
name: 'petId'
|
||||||
|
in_: 'path'
|
||||||
|
description: 'ID of the pet to retrieve'
|
||||||
|
required: true
|
||||||
|
schema: Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
format: 'int64'
|
||||||
|
example: 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
responses: {
|
||||||
|
'200': openapi.ResponseSpec{
|
||||||
|
description: 'A pet'
|
||||||
|
content: {
|
||||||
|
'application/json': openapi.MediaType{
|
||||||
|
schema: Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
}
|
||||||
|
example: json.raw_decode('{ "id": 1, "name": "Fluffy", "tag": "dog" }')!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'404': openapi.ResponseSpec{
|
||||||
|
description: 'Pet not found'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete: openapi.Operation{
|
||||||
|
summary: 'Delete a pet by ID'
|
||||||
|
operation_id: 'deletePet'
|
||||||
|
parameters: [
|
||||||
|
openapi.Parameter{
|
||||||
|
name: 'petId'
|
||||||
|
in_: 'path'
|
||||||
|
description: 'ID of the pet to delete'
|
||||||
|
required: true
|
||||||
|
schema: Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
format: 'int64'
|
||||||
|
example: 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
responses: {
|
||||||
|
'204': openapi.ResponseSpec{
|
||||||
|
description: 'Pet deleted'
|
||||||
|
}
|
||||||
|
'404': openapi.ResponseSpec{
|
||||||
|
description: 'Pet not found'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
components: openapi.Components{
|
||||||
|
schemas: {
|
||||||
|
'Pet': SchemaRef(Schema{
|
||||||
|
typ: 'object'
|
||||||
|
required: ['id', 'name']
|
||||||
|
properties: {
|
||||||
|
'id': SchemaRef(Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
format: 'int64'
|
||||||
|
})
|
||||||
|
'name': SchemaRef(Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
'tag': SchemaRef(Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
'NewPet': SchemaRef(Schema{
|
||||||
|
typ: 'object'
|
||||||
|
required: ['name']
|
||||||
|
properties: {
|
||||||
|
'name': SchemaRef(Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
'tag': SchemaRef(Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
'Pets': SchemaRef(Schema{
|
||||||
|
typ: 'array'
|
||||||
|
items: SchemaRef(Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const actor_spec = specification.ActorSpecification{
|
||||||
|
name: 'Pet Store API'
|
||||||
|
description: 'A sample API for a pet store'
|
||||||
|
structure: code.Struct{}
|
||||||
|
interfaces: [.openapi]
|
||||||
|
methods: [
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'listPets'
|
||||||
|
summary: 'List all pets'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
params: [
|
||||||
|
openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example limit'
|
||||||
|
description: 'Example Maximum number of pets to return'
|
||||||
|
value: 10
|
||||||
|
})
|
||||||
|
]
|
||||||
|
result: openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example response'
|
||||||
|
value: json.raw_decode('[
|
||||||
|
{"id": 1, "name": "Fluffy", "tag": "dog"},
|
||||||
|
{"id": 2, "name": "Whiskers", "tag": "cat"}
|
||||||
|
]')!
|
||||||
|
})
|
||||||
|
}
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'limit'
|
||||||
|
summary: 'Maximum number of pets to return'
|
||||||
|
description: 'Maximum number of pets to return'
|
||||||
|
required: false
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
format: 'int32'
|
||||||
|
example: 10
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pets'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 400
|
||||||
|
message: 'Invalid request'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'createPet'
|
||||||
|
summary: 'Create a new pet'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
result: openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example response'
|
||||||
|
value: '[]'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 400
|
||||||
|
message: 'Invalid input'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'getPet'
|
||||||
|
summary: 'Get a pet by ID'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
params: [
|
||||||
|
openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example petId'
|
||||||
|
description: 'Example ID of the pet to retrieve'
|
||||||
|
value: 1
|
||||||
|
})
|
||||||
|
]
|
||||||
|
result: openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example response'
|
||||||
|
value: json.raw_decode('{"id": 1, "name": "Fluffy", "tag": "dog"}')!
|
||||||
|
})
|
||||||
|
}
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'petId'
|
||||||
|
summary: 'ID of the pet to retrieve'
|
||||||
|
description: 'ID of the pet to retrieve'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
format: 'int64'
|
||||||
|
example: 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 404
|
||||||
|
message: 'Pet not found'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
specification.ActorMethod{
|
||||||
|
name: 'deletePet'
|
||||||
|
summary: 'Delete a pet by ID'
|
||||||
|
example: openrpc.ExamplePairing{
|
||||||
|
params: [
|
||||||
|
openrpc.ExampleRef(openrpc.Example{
|
||||||
|
name: 'Example petId'
|
||||||
|
description: 'Example ID of the pet to delete'
|
||||||
|
value: 1
|
||||||
|
})
|
||||||
|
]
|
||||||
|
}
|
||||||
|
parameters: [
|
||||||
|
openrpc.ContentDescriptor{
|
||||||
|
name: 'petId'
|
||||||
|
summary: 'ID of the pet to delete'
|
||||||
|
description: 'ID of the pet to delete'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
format: 'int64'
|
||||||
|
example: 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'result'
|
||||||
|
description: 'The response of the operation.'
|
||||||
|
required: true
|
||||||
|
}
|
||||||
|
errors: [
|
||||||
|
openrpc.ErrorSpec{
|
||||||
|
code: 404
|
||||||
|
message: 'Pet not found'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
objects: [
|
||||||
|
specification.BaseObject{
|
||||||
|
schema: jsonschema.Schema{
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
format: 'int64'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
}
|
||||||
|
},
|
||||||
|
specification.BaseObject{
|
||||||
|
schema: jsonschema.Schema{
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['name']
|
||||||
|
}
|
||||||
|
},
|
||||||
|
specification.BaseObject{
|
||||||
|
schema: jsonschema.Schema{
|
||||||
|
typ: 'array'
|
||||||
|
items: jsonschema.Items(jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_from_openapi() ! {
|
||||||
|
// panic(from_openapi(openapi_spec)!)
|
||||||
|
assert from_openapi(openapi_spec)! == actor_spec
|
||||||
|
}
|
||||||
105
lib/baobab/specification/from_openrpc.v
Normal file
105
lib/baobab/specification/from_openrpc.v
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
module specification
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc { OpenRPC, Method, ContentDescriptor, ErrorSpec }
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema { Reference, Schema, SchemaRef }
|
||||||
|
import freeflowuniverse.herolib.core.texttools
|
||||||
|
|
||||||
|
// Helper function: Convert OpenRPC Method to ActorMethod
|
||||||
|
fn openrpc_method_to_actor_method(method Method) ActorMethod {
|
||||||
|
mut parameters := []ContentDescriptor{}
|
||||||
|
mut errors := []ErrorSpec{}
|
||||||
|
|
||||||
|
// Process parameters
|
||||||
|
for param in method.params {
|
||||||
|
if param is ContentDescriptor {
|
||||||
|
parameters << param
|
||||||
|
} else {
|
||||||
|
panic('Method param should be inflated')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process errors
|
||||||
|
for err in method.errors {
|
||||||
|
if err is ErrorSpec {
|
||||||
|
errors << err
|
||||||
|
} else {
|
||||||
|
panic('Method error should be inflated')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if method.result is Reference {
|
||||||
|
panic('Method result should be inflated')
|
||||||
|
}
|
||||||
|
|
||||||
|
return ActorMethod{
|
||||||
|
name: method.name
|
||||||
|
description: method.description
|
||||||
|
summary: method.summary
|
||||||
|
parameters: parameters
|
||||||
|
result: method.result as ContentDescriptor
|
||||||
|
errors: errors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// // Helper function: Extract Structs from OpenRPC Components
|
||||||
|
// fn extract_structs_from_openrpc(openrpc OpenRPC) []Struct {
|
||||||
|
// mut structs := []Struct{}
|
||||||
|
|
||||||
|
// for schema_name, schema in openrpc.components.schemas {
|
||||||
|
// if schema is Schema {
|
||||||
|
// mut fields := []Struct.Field{}
|
||||||
|
// for field_name, field_schema in schema.properties {
|
||||||
|
// if field_schema is Schema {
|
||||||
|
// fields << Struct.Field{
|
||||||
|
// name: field_name
|
||||||
|
// typ: field_schema.to_code() or { panic(err) }
|
||||||
|
// description: field_schema.description
|
||||||
|
// required: field_name in schema.required
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// structs << Struct{
|
||||||
|
// name: schema_name
|
||||||
|
// description: schema.description
|
||||||
|
// fields: fields
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return structs
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Converts OpenRPC to ActorSpecification
|
||||||
|
pub fn from_openrpc(spec OpenRPC) !ActorSpecification {
|
||||||
|
mut methods := []ActorMethod{}
|
||||||
|
mut objects := []BaseObject{}
|
||||||
|
|
||||||
|
// Process methods
|
||||||
|
for method in spec.methods {
|
||||||
|
methods << openrpc_method_to_actor_method(spec.inflate_method(method))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process objects (schemas)
|
||||||
|
// structs := extract_structs_from_openrpc(spec)
|
||||||
|
for key, schema in spec.components.schemas {
|
||||||
|
if schema is Schema {
|
||||||
|
if schema.typ == 'object' {
|
||||||
|
objects << BaseObject{
|
||||||
|
schema: Schema {...schema,
|
||||||
|
title: texttools.name_fix_pascal(key)
|
||||||
|
id: texttools.snake_case(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ActorSpecification{
|
||||||
|
name: spec.info.title
|
||||||
|
description: spec.info.description
|
||||||
|
interfaces: [.openrpc]
|
||||||
|
methods: methods
|
||||||
|
objects: objects
|
||||||
|
}
|
||||||
|
}
|
||||||
393
lib/baobab/specification/from_openrpc_test.v
Normal file
393
lib/baobab/specification/from_openrpc_test.v
Normal file
@@ -0,0 +1,393 @@
|
|||||||
|
module specification
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { Struct, Function }
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc { ContentDescriptor, ErrorSpec }
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi { OpenAPI, Info, ServerSpec, Components, Operation, PathItem, PathRef }
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema {Schema, Reference, SchemaRef}
|
||||||
|
|
||||||
|
const openrpc_spec = openrpc.OpenRPC{
|
||||||
|
openrpc: '1.0.0-rc1'
|
||||||
|
info: openrpc.Info{
|
||||||
|
title: 'Petstore'
|
||||||
|
license: openrpc.License{
|
||||||
|
name: 'MIT'
|
||||||
|
}
|
||||||
|
version: '1.0.0'
|
||||||
|
}
|
||||||
|
servers: [openrpc.Server{
|
||||||
|
name: 'localhost'
|
||||||
|
url: openrpc.RuntimeExpression('http://localhost:8080')
|
||||||
|
}]
|
||||||
|
methods: [
|
||||||
|
openrpc.Method{
|
||||||
|
name: 'list_pets'
|
||||||
|
summary: 'List all pets'
|
||||||
|
params: [openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'limit'
|
||||||
|
description: 'How many items to return at one time (max 100)'
|
||||||
|
required: false
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
minimum: 1
|
||||||
|
})
|
||||||
|
})]
|
||||||
|
result: openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'pets'
|
||||||
|
description: 'A paged array of pets'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'array'
|
||||||
|
items: jsonschema.Items(jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
examples: [openrpc.ExamplePairing{
|
||||||
|
name: 'listPetExample'
|
||||||
|
description: 'List pet example'
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
openrpc.Method{
|
||||||
|
name: 'create_pet'
|
||||||
|
summary: 'Create a pet'
|
||||||
|
params: [
|
||||||
|
openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'newPetName'
|
||||||
|
description: 'Name of pet to create'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'newPetTag'
|
||||||
|
description: 'Pet tag to create'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptorRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/contentDescriptors/PetId'
|
||||||
|
})
|
||||||
|
examples: [openrpc.ExamplePairing{
|
||||||
|
name: 'createPetExample'
|
||||||
|
description: 'Create pet example'
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
openrpc.Method{
|
||||||
|
name: 'get_pet'
|
||||||
|
summary: 'Info for a specific pet'
|
||||||
|
params: [openrpc.ContentDescriptorRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/contentDescriptors/PetId'
|
||||||
|
})]
|
||||||
|
result: openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'Expected response to a valid request'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
examples: [openrpc.ExamplePairing{
|
||||||
|
name: 'getPetExample'
|
||||||
|
description: 'Get pet example'
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
openrpc.Method{
|
||||||
|
name: 'update_pet'
|
||||||
|
summary: 'Update a pet'
|
||||||
|
params: [
|
||||||
|
openrpc.ContentDescriptorRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/contentDescriptors/PetId'
|
||||||
|
}),
|
||||||
|
openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'updatedPetName'
|
||||||
|
description: 'New name for the pet'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'updatedPetTag'
|
||||||
|
description: 'New tag for the pet'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
]
|
||||||
|
result: openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'Updated pet object'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/Pet'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
examples: [openrpc.ExamplePairing{
|
||||||
|
name: 'updatePetExample'
|
||||||
|
description: 'Update pet example'
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
openrpc.Method{
|
||||||
|
name: 'delete_pet'
|
||||||
|
summary: 'Delete a pet'
|
||||||
|
params: [openrpc.ContentDescriptorRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/contentDescriptors/PetId'
|
||||||
|
})]
|
||||||
|
result: openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'success'
|
||||||
|
description: 'Boolean indicating success'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'boolean'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
examples: [openrpc.ExamplePairing{
|
||||||
|
name: 'deletePetExample'
|
||||||
|
description: 'Delete pet example'
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
components: openrpc.Components{
|
||||||
|
content_descriptors: {
|
||||||
|
'PetId': openrpc.ContentDescriptorRef(openrpc.ContentDescriptor{
|
||||||
|
name: 'petId'
|
||||||
|
description: 'The ID of the pet'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
schemas: {
|
||||||
|
'PetId': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
minimum: 0
|
||||||
|
}),
|
||||||
|
'Pet': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const actor_spec = specification.ActorSpecification{
|
||||||
|
name: 'Petstore'
|
||||||
|
structure: code.Struct{
|
||||||
|
is_pub: false
|
||||||
|
}
|
||||||
|
interfaces: [.openrpc]
|
||||||
|
methods: [specification.ActorMethod{
|
||||||
|
name: 'list_pets'
|
||||||
|
summary: 'List all pets'
|
||||||
|
parameters: [openrpc.ContentDescriptor{
|
||||||
|
name: 'limit'
|
||||||
|
description: 'How many items to return at one time (max 100)'
|
||||||
|
required: false
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
minimum: 1
|
||||||
|
})
|
||||||
|
}]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pets'
|
||||||
|
description: 'A paged array of pets'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'array'
|
||||||
|
items: jsonschema.Items(jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, specification.ActorMethod{
|
||||||
|
name: 'create_pet'
|
||||||
|
summary: 'Create a pet'
|
||||||
|
parameters: [openrpc.ContentDescriptor{
|
||||||
|
name: 'newPetName'
|
||||||
|
description: 'Name of pet to create'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}, openrpc.ContentDescriptor{
|
||||||
|
name: 'newPetTag'
|
||||||
|
description: 'Pet tag to create'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}]
|
||||||
|
}, specification.ActorMethod{
|
||||||
|
name: 'get_pet'
|
||||||
|
summary: 'Info for a specific pet'
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'Expected response to a valid request'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, specification.ActorMethod{
|
||||||
|
name: 'update_pet'
|
||||||
|
summary: 'Update a pet'
|
||||||
|
parameters: [openrpc.ContentDescriptor{
|
||||||
|
name: 'updatedPetName'
|
||||||
|
description: 'New name for the pet'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}, openrpc.ContentDescriptor{
|
||||||
|
name: 'updatedPetTag'
|
||||||
|
description: 'New tag for the pet'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'Updated pet object'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, specification.ActorMethod{
|
||||||
|
name: 'delete_pet'
|
||||||
|
summary: 'Delete a pet'
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'success'
|
||||||
|
description: 'Boolean indicating success'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'boolean'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
objects: [specification.BaseObject{
|
||||||
|
schema: jsonschema.Schema{
|
||||||
|
id: 'pet'
|
||||||
|
title: 'Pet'
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn test_from_openrpc() ! {
|
||||||
|
actor_spec_ := from_openrpc(openrpc_spec)!
|
||||||
|
assert actor_spec_.methods.len == actor_spec.methods.len
|
||||||
|
assert_methods_match(actor_spec_.methods[0], actor_spec.methods[0])
|
||||||
|
|
||||||
|
|
||||||
|
// assert from_openrpc(openrpc_spec)! == actor_spec
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_methods_match(a ActorMethod, b ActorMethod) {
|
||||||
|
// Compare method names
|
||||||
|
assert a.name == b.name, 'Method names do not match: ${a.name} != ${b.name}'
|
||||||
|
|
||||||
|
// Compare summaries
|
||||||
|
assert a.summary == b.summary, 'Method summaries do not match for method ${a.name}.'
|
||||||
|
|
||||||
|
// Compare descriptions
|
||||||
|
assert a.description == b.description, 'Method descriptions do not match for method ${a.name}.'
|
||||||
|
|
||||||
|
// Compare parameters count
|
||||||
|
assert a.parameters.len == b.parameters.len, 'Parameter counts do not match for method ${a.name}.'
|
||||||
|
|
||||||
|
// Compare each parameter
|
||||||
|
for i, param_a in a.parameters {
|
||||||
|
assert_params_match(param_a, b.parameters[i], a.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare result
|
||||||
|
assert_params_match(a.result, b.result, a.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_params_match(a openrpc.ContentDescriptor, b openrpc.ContentDescriptor, method_name string) {
|
||||||
|
// Compare parameter names
|
||||||
|
assert a.name == b.name, 'Parameter names do not match in method ${method_name}: ${a.name} != ${b.name}'
|
||||||
|
|
||||||
|
// Compare summaries
|
||||||
|
assert a.summary == b.summary, 'Parameter summaries do not match in method ${method_name}: ${a.name}'
|
||||||
|
|
||||||
|
// Compare descriptions
|
||||||
|
assert a.description == b.description, 'Parameter descriptions do not match in method ${method_name}: ${a.name}'
|
||||||
|
|
||||||
|
// Compare required flags
|
||||||
|
assert a.required == b.required, 'Required flags do not match in method ${method_name}: ${a.name}'
|
||||||
|
|
||||||
|
// Compare schemas
|
||||||
|
// assert_schemas_match(a.schema, b.schema, method_name, a.name)
|
||||||
|
}
|
||||||
|
|
||||||
|
// fn assert_schemas_match(a jsonschema.SchemaRef, b jsonschema.SchemaRef, method_name string, param_name string) {
|
||||||
|
// if a is Schema &&
|
||||||
|
// // Compare schema types
|
||||||
|
// assert a.typ == b.typ, 'Schema types do not match for parameter ${param_name} in method ${method_name}: ${a.typ} != ${b.typ}'
|
||||||
|
|
||||||
|
// // Compare schema titles
|
||||||
|
// assert a.title == b.title, 'Schema titles do not match for parameter ${param_name} in method ${method_name}.'
|
||||||
|
|
||||||
|
// // Compare schema descriptions
|
||||||
|
// assert a.description == b.description, 'Schema descriptions do not match for parameter ${param_name} in method ${method_name}.'
|
||||||
|
|
||||||
|
// // Compare other schema fields as needed (e.g., properties, additional properties, items, etc.)
|
||||||
|
// // Add more checks here if needed for deeper schema comparisons
|
||||||
|
// }
|
||||||
200
lib/baobab/specification/model.v
Normal file
200
lib/baobab/specification/model.v
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
module specification
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code { Struct, Function }
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc {ExamplePairing, ContentDescriptor, ErrorSpec}
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema {Schema, Reference}
|
||||||
|
|
||||||
|
pub struct ActorSpecification {
|
||||||
|
pub mut:
|
||||||
|
openapi ?openapi.OpenAPI
|
||||||
|
openrpc ?openrpc.OpenRPC
|
||||||
|
name string @[omitempty]
|
||||||
|
description string @[omitempty]
|
||||||
|
structure Struct @[omitempty]
|
||||||
|
interfaces []ActorInterface @[omitempty]
|
||||||
|
methods []ActorMethod @[omitempty]
|
||||||
|
objects []BaseObject @[omitempty]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum ActorInterface {
|
||||||
|
openrpc
|
||||||
|
openapi
|
||||||
|
webui
|
||||||
|
command
|
||||||
|
http
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ActorMethod {
|
||||||
|
pub:
|
||||||
|
name string @[omitempty]
|
||||||
|
description string @[omitempty]
|
||||||
|
summary string
|
||||||
|
example ExamplePairing
|
||||||
|
parameters []ContentDescriptor
|
||||||
|
result ContentDescriptor
|
||||||
|
errors []ErrorSpec
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct BaseObject {
|
||||||
|
pub mut:
|
||||||
|
schema Schema
|
||||||
|
new_method ?ActorMethod
|
||||||
|
get_method ?ActorMethod
|
||||||
|
set_method ?ActorMethod
|
||||||
|
delete_method ?ActorMethod
|
||||||
|
list_method ?ActorMethod
|
||||||
|
filter_method ?ActorMethod
|
||||||
|
other_methods []ActorMethod
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum MethodCategory {
|
||||||
|
base_object_new
|
||||||
|
base_object_get
|
||||||
|
base_object_set
|
||||||
|
base_object_delete
|
||||||
|
base_object_list
|
||||||
|
other
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns whether method belongs to a given base object
|
||||||
|
// TODO: link to more info about base object methods
|
||||||
|
fn (m ActorMethod) belongs_to_object(obj BaseObject) bool {
|
||||||
|
base_obj_is_param := m.parameters
|
||||||
|
.filter(it.schema is Schema)
|
||||||
|
.map(it.schema as Schema)
|
||||||
|
.any(it.id == obj.schema.id)
|
||||||
|
|
||||||
|
base_obj_is_result := if m.result.schema is Schema {
|
||||||
|
m.result.schema.id == obj.schema.id
|
||||||
|
} else {
|
||||||
|
ref := m.result.schema as Reference
|
||||||
|
ref.ref.all_after_last('/') == obj.name()
|
||||||
|
}
|
||||||
|
|
||||||
|
return base_obj_is_param || base_obj_is_result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (s ActorSpecification) validate() ActorSpecification {
|
||||||
|
mut validated_objects := []BaseObject{}
|
||||||
|
for obj_ in s.objects {
|
||||||
|
mut obj := obj_
|
||||||
|
if obj.schema.id == '' {
|
||||||
|
obj.schema.id = obj.schema.title
|
||||||
|
}
|
||||||
|
methods := s.methods.filter(it.belongs_to_object(obj))
|
||||||
|
|
||||||
|
if m := methods.filter(it.is_new_method())[0] {
|
||||||
|
obj.new_method = m
|
||||||
|
}
|
||||||
|
if m := methods.filter(it.is_set_method())[0] {
|
||||||
|
obj.set_method = m
|
||||||
|
}
|
||||||
|
if m := methods.filter(it.is_get_method())[0] {
|
||||||
|
obj.get_method = m
|
||||||
|
}
|
||||||
|
if m := methods.filter(it.is_delete_method())[0] {
|
||||||
|
obj.delete_method = m
|
||||||
|
}
|
||||||
|
if m := methods.filter(it.is_list_method())[0] {
|
||||||
|
obj.list_method = m
|
||||||
|
}
|
||||||
|
validated_objects << BaseObject {
|
||||||
|
...obj
|
||||||
|
other_methods: methods.filter(!it.is_crudlf_method())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ActorSpecification {
|
||||||
|
...s,
|
||||||
|
objects: validated_objects
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// method category returns what category a method falls under
|
||||||
|
pub fn (s ActorSpecification) method_type(method ActorMethod) MethodCategory {
|
||||||
|
return if s.is_base_object_new_method(method) {
|
||||||
|
.base_object_new
|
||||||
|
} else if s.is_base_object_get_method(method) {
|
||||||
|
.base_object_get
|
||||||
|
} else if s.is_base_object_set_method(method) {
|
||||||
|
.base_object_set
|
||||||
|
} else if s.is_base_object_delete_method(method) {
|
||||||
|
.base_object_delete
|
||||||
|
} else if s.is_base_object_list_method(method) {
|
||||||
|
.base_object_list
|
||||||
|
} else {
|
||||||
|
.other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// a base object method is a method that is a
|
||||||
|
// CRUD+list+filter method of a base object
|
||||||
|
fn (s ActorSpecification) is_base_object_method(method ActorMethod) bool {
|
||||||
|
base_obj_is_param := method.parameters
|
||||||
|
.filter(it.schema is Schema)
|
||||||
|
.map(it.schema as Schema)
|
||||||
|
.any(it.id in s.objects.map(it.schema.id))
|
||||||
|
|
||||||
|
base_obj_is_result := if method.result.schema is Schema {
|
||||||
|
method.result.schema.id in s.objects.map(it.name())
|
||||||
|
} else {
|
||||||
|
ref := method.result.schema as Reference
|
||||||
|
ref.ref.all_after_last('/') in s.objects.map(it.name())
|
||||||
|
}
|
||||||
|
|
||||||
|
return base_obj_is_param || base_obj_is_result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (m ActorMethod) is_new_method() bool {
|
||||||
|
return m.name.starts_with('new')
|
||||||
|
}
|
||||||
|
fn (m ActorMethod) is_get_method() bool {
|
||||||
|
return m.name.starts_with('get')
|
||||||
|
}
|
||||||
|
fn (m ActorMethod) is_set_method() bool {
|
||||||
|
return m.name.starts_with('set')
|
||||||
|
}
|
||||||
|
fn (m ActorMethod) is_delete_method() bool {
|
||||||
|
return m.name.starts_with('delete')
|
||||||
|
}
|
||||||
|
fn (m ActorMethod) is_list_method() bool {
|
||||||
|
return m.name.starts_with('list')
|
||||||
|
}
|
||||||
|
fn (m ActorMethod) is_filter_method() bool {
|
||||||
|
return m.name.starts_with('filter')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (m ActorMethod) is_crudlf_method() bool {
|
||||||
|
return m.is_new_method() ||
|
||||||
|
m.is_get_method() ||
|
||||||
|
m.is_set_method() ||
|
||||||
|
m.is_delete_method() ||
|
||||||
|
m.is_list_method() ||
|
||||||
|
m.is_filter_method()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (o BaseObject) name() string {
|
||||||
|
return if o.schema.id.trim_space() != '' {
|
||||||
|
o.schema.id.trim_space()
|
||||||
|
} else {o.schema.title.trim_space()}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (s ActorSpecification) is_base_object_new_method(method ActorMethod) bool {
|
||||||
|
return s.is_base_object_method(method) && method.name.starts_with('new')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (s ActorSpecification) is_base_object_get_method(method ActorMethod) bool {
|
||||||
|
return s.is_base_object_method(method) && method.name.starts_with('get')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (s ActorSpecification) is_base_object_set_method(method ActorMethod) bool {
|
||||||
|
return s.is_base_object_method(method) && method.name.starts_with('set')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (s ActorSpecification) is_base_object_delete_method(method ActorMethod) bool {
|
||||||
|
return s.is_base_object_method(method) && method.name.starts_with('delete')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (s ActorSpecification) is_base_object_list_method(method ActorMethod) bool {
|
||||||
|
return s.is_base_object_method(method) && method.name.starts_with('list')
|
||||||
|
}
|
||||||
91
lib/baobab/specification/to_openapi.v
Normal file
91
lib/baobab/specification/to_openapi.v
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
module specification
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema { Schema, SchemaRef }
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi { MediaType, ResponseSpec, Operation, Parameter, OpenAPI, Components, Info, PathItem, ServerSpec }
|
||||||
|
import net.http
|
||||||
|
|
||||||
|
// Converts ActorSpecification to OpenAPI
|
||||||
|
pub fn (s ActorSpecification) to_openapi() OpenAPI {
|
||||||
|
if openapi_spec := s.openapi {
|
||||||
|
return openapi_spec
|
||||||
|
}
|
||||||
|
mut paths := map[string]PathItem{}
|
||||||
|
|
||||||
|
// Map ActorMethods to paths
|
||||||
|
for method in s.methods {
|
||||||
|
op := method.to_openapi_operation()
|
||||||
|
paths['${method.http_path()}'] = match method.http_method() {
|
||||||
|
.get { PathItem {get: op} }
|
||||||
|
else { panic('unsupported http method') }
|
||||||
|
}
|
||||||
|
// Assign operation to corresponding HTTP method
|
||||||
|
// TODO: what about other verbs
|
||||||
|
}
|
||||||
|
|
||||||
|
mut schemas := map[string]SchemaRef{}
|
||||||
|
for object in s.objects {
|
||||||
|
schemas[object.schema.id] = object.to_schema()
|
||||||
|
}
|
||||||
|
|
||||||
|
return OpenAPI{
|
||||||
|
openapi: '3.0.0',
|
||||||
|
info: Info{
|
||||||
|
title: s.name,
|
||||||
|
summary: s.description,
|
||||||
|
description: s.description,
|
||||||
|
version: '1.0.0',
|
||||||
|
},
|
||||||
|
servers: [
|
||||||
|
ServerSpec{
|
||||||
|
url: 'http://localhost:8080',
|
||||||
|
description: 'Default server',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
paths: paths,
|
||||||
|
components: Components{
|
||||||
|
schemas: schemas
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (bo BaseObject) to_schema() Schema {
|
||||||
|
return Schema{}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (m ActorMethod) http_path() string {
|
||||||
|
return m.name
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (m ActorMethod) http_method() http.Method {
|
||||||
|
return .get
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (method ActorMethod) to_openapi_operation() Operation {
|
||||||
|
mut op := Operation{
|
||||||
|
summary: method.summary,
|
||||||
|
description: method.description,
|
||||||
|
operation_id: method.name,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert parameters to OpenAPI format
|
||||||
|
for param in method.parameters {
|
||||||
|
op.parameters << Parameter{
|
||||||
|
name: param.name,
|
||||||
|
in_: 'query', // Default to query parameters; adjust based on function context
|
||||||
|
description: param.description,
|
||||||
|
required: param.required,
|
||||||
|
schema: param.schema,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if method.is_void()
|
||||||
|
op.responses['200'] = ResponseSpec {
|
||||||
|
description: method.description
|
||||||
|
content: {
|
||||||
|
'application/json': MediaType {
|
||||||
|
schema: method.result.schema
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return op
|
||||||
|
}
|
||||||
159
lib/baobab/specification/to_openapi_test.v
Normal file
159
lib/baobab/specification/to_openapi_test.v
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
module specification
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.code
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema { Schema, SchemaRef }
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi { Operation, Parameter, OpenAPI, Components, Info, PathItem, ServerSpec }
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc
|
||||||
|
|
||||||
|
|
||||||
|
const actor_spec = specification.ActorSpecification{
|
||||||
|
name: 'Petstore'
|
||||||
|
structure: code.Struct{
|
||||||
|
is_pub: false
|
||||||
|
}
|
||||||
|
interfaces: [.openrpc]
|
||||||
|
methods: [specification.ActorMethod{
|
||||||
|
name: 'list_pets'
|
||||||
|
summary: 'List all pets'
|
||||||
|
parameters: [openrpc.ContentDescriptor{
|
||||||
|
name: 'limit'
|
||||||
|
description: 'How many items to return at one time (max 100)'
|
||||||
|
required: false
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'integer'
|
||||||
|
minimum: 1
|
||||||
|
})
|
||||||
|
}]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pets'
|
||||||
|
description: 'A paged array of pets'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'array'
|
||||||
|
items: jsonschema.Items(jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, specification.ActorMethod{
|
||||||
|
name: 'create_pet'
|
||||||
|
summary: 'Create a pet'
|
||||||
|
parameters: [openrpc.ContentDescriptor{
|
||||||
|
name: 'newPetName'
|
||||||
|
description: 'Name of pet to create'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}, openrpc.ContentDescriptor{
|
||||||
|
name: 'newPetTag'
|
||||||
|
description: 'Pet tag to create'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}]
|
||||||
|
}, specification.ActorMethod{
|
||||||
|
name: 'get_pet'
|
||||||
|
summary: 'Info for a specific pet'
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'Expected response to a valid request'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, specification.ActorMethod{
|
||||||
|
name: 'update_pet'
|
||||||
|
summary: 'Update a pet'
|
||||||
|
parameters: [openrpc.ContentDescriptor{
|
||||||
|
name: 'updatedPetName'
|
||||||
|
description: 'New name for the pet'
|
||||||
|
required: true
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}, openrpc.ContentDescriptor{
|
||||||
|
name: 'updatedPetTag'
|
||||||
|
description: 'New tag for the pet'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}]
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'pet'
|
||||||
|
description: 'Updated pet object'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, specification.ActorMethod{
|
||||||
|
name: 'delete_pet'
|
||||||
|
summary: 'Delete a pet'
|
||||||
|
result: openrpc.ContentDescriptor{
|
||||||
|
name: 'success'
|
||||||
|
description: 'Boolean indicating success'
|
||||||
|
schema: jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'boolean'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
objects: [specification.BaseObject{
|
||||||
|
schema: jsonschema.Schema{
|
||||||
|
id: 'pet'
|
||||||
|
title: 'Pet'
|
||||||
|
typ: 'object'
|
||||||
|
properties: {
|
||||||
|
'id': jsonschema.SchemaRef(jsonschema.Reference{
|
||||||
|
ref: '#/components/schemas/PetId'
|
||||||
|
}),
|
||||||
|
'name': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
}),
|
||||||
|
'tag': jsonschema.SchemaRef(jsonschema.Schema{
|
||||||
|
typ: 'string'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
required: ['id', 'name']
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Converts ActorSpecification to OpenAPI
|
||||||
|
pub fn test_specification_to_openapi() {
|
||||||
|
panic(actor_spec.to_openapi())
|
||||||
|
}
|
||||||
72
lib/baobab/specification/to_openrpc.v
Normal file
72
lib/baobab/specification/to_openrpc.v
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
module specification
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc {OpenRPC, Components}
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema {SchemaRef}
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonschema.codegen { struct_to_schema }
|
||||||
|
|
||||||
|
// pub fn from_openrpc(spec openrpc.OpenRPC) !ActorSpecification {
|
||||||
|
// // Extract Actor metadata from OpenRPC info
|
||||||
|
// // actor_name := openrpc_doc.info.title
|
||||||
|
// // actor_description := openrpc_doc.info.description
|
||||||
|
|
||||||
|
// // // Generate methods
|
||||||
|
// // mut methods := []ActorMethod{}
|
||||||
|
// // for method in openrpc_doc.methods {
|
||||||
|
// // method_code := method.to_code()! // Using provided to_code function
|
||||||
|
// // methods << ActorMethod{
|
||||||
|
// // name: method.name
|
||||||
|
// // func: method_code
|
||||||
|
// // }
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// // // Generate BaseObject structs from schemas
|
||||||
|
// // mut objects := []BaseObject{}
|
||||||
|
// // for key, schema_ref in openrpc_doc.components.schemas {
|
||||||
|
// // struct_obj := schema_ref.to_code()! // Assuming schema_ref.to_code() converts schema to Struct
|
||||||
|
// // // objects << BaseObject{
|
||||||
|
// // // structure: code.Struct{
|
||||||
|
// // // name: struct_obj.name
|
||||||
|
// // // }
|
||||||
|
// // // }
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// // Build the Actor struct
|
||||||
|
// return ActorSpecification{
|
||||||
|
// // name: actor_name
|
||||||
|
// // description: actor_description
|
||||||
|
// // methods: methods
|
||||||
|
// // objects: objects
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
pub fn (specification ActorSpecification) to_openrpc() OpenRPC {
|
||||||
|
mut schemas := map[string]SchemaRef{}
|
||||||
|
for obj in specification.objects {
|
||||||
|
schemas[obj.schema.id] = obj.schema
|
||||||
|
// for child in obj.children {
|
||||||
|
// schemas[child.name] = struct_to_schema(child)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
return OpenRPC{
|
||||||
|
info: openrpc.Info{
|
||||||
|
title: specification.name.title()
|
||||||
|
version: '1.0.0'
|
||||||
|
}
|
||||||
|
methods: specification.methods.map(method_to_openrpc_method(it))
|
||||||
|
components: Components{
|
||||||
|
schemas: schemas
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn method_to_openrpc_method(method ActorMethod) openrpc.Method {
|
||||||
|
return openrpc.Method {
|
||||||
|
name: method.name
|
||||||
|
summary: method.summary
|
||||||
|
description: method.description
|
||||||
|
params: method.parameters.map(openrpc.ContentDescriptorRef(it))
|
||||||
|
result: openrpc.ContentDescriptorRef(method.result)
|
||||||
|
errors: method.errors.map(openrpc.ErrorRef(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
140
lib/baobab/stage/README.md
Normal file
140
lib/baobab/stage/README.md
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
|
||||||
|
# Stage Module
|
||||||
|
|
||||||
|
The **Stage** module is a core component of the **Baobab** (Base Object and Actor Backend) library. It provides the infrastructure for handling RPC-based communication and managing the lifecycle of **Actors** and **Actions**. This module facilitates processing incoming requests, converting them to actions, and ensuring their correct execution.
|
||||||
|
|
||||||
|
## Architecture Overview
|
||||||
|
|
||||||
|
The **Stage** module operates based on the following architecture:
|
||||||
|
|
||||||
|
1. **RPC Request Handling**:
|
||||||
|
- An **Interface Handler** receives an RPC request. Supported interfaces include:
|
||||||
|
- **OpenRPC**
|
||||||
|
- **JSON-RPC**
|
||||||
|
- **OpenAPI**
|
||||||
|
|
||||||
|
2. **Action Creation**:
|
||||||
|
- The **Interface Handler** converts the incoming request into an **Action**, which represents the task to be executed.
|
||||||
|
|
||||||
|
3. **Action Execution**:
|
||||||
|
- The **Interface Handler** passes the **Action** to the **Director** for coordinated execution.
|
||||||
|
- (Note: Currently, the **Director** is not fully implemented. Actions are passed directly to the **Actor** for execution.)
|
||||||
|
|
||||||
|
4. **Actor Processing**:
|
||||||
|
- The **Actor** uses its `act` method to execute the **Action**.
|
||||||
|
- The result of the **Action** is stored in its `result` field, and the **Action** is returned.
|
||||||
|
|
||||||
|
5. **RPC Response Generation**:
|
||||||
|
- The **Interface Handler** converts the resulting **Action** back into the appropriate RPC response format and returns it.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Key Components
|
||||||
|
|
||||||
|
### **Interface Handlers**
|
||||||
|
- **Responsibilities**:
|
||||||
|
- Receive and parse incoming RPC requests.
|
||||||
|
- Convert requests into **Actions**.
|
||||||
|
- Convert resulting **Actions** into appropriate RPC responses.
|
||||||
|
- Files:
|
||||||
|
- `interfaces/jsonrpc_interface.v`
|
||||||
|
- `interfaces/openapi_interface.v`
|
||||||
|
|
||||||
|
### **Director**
|
||||||
|
- **Responsibilities**:
|
||||||
|
- (Planned) Coordinate the execution of **Actions**.
|
||||||
|
- Handle retries, timeouts, and error recovery.
|
||||||
|
- File:
|
||||||
|
- `director.v`
|
||||||
|
|
||||||
|
### **Actors**
|
||||||
|
- **Responsibilities**:
|
||||||
|
- Execute **Actions** using their `act` method.
|
||||||
|
- Populate the `result` field of **Actions** with the execution result.
|
||||||
|
- File:
|
||||||
|
- `actor.v`
|
||||||
|
|
||||||
|
### **Actions**
|
||||||
|
- **Responsibilities**:
|
||||||
|
- Represent tasks to be executed by **Actors**.
|
||||||
|
- Carry results back after execution.
|
||||||
|
- File:
|
||||||
|
- `action.v`
|
||||||
|
|
||||||
|
### **Executor**
|
||||||
|
- **Responsibilities**:
|
||||||
|
- Manage the assignment of **Actions** to **Actors**.
|
||||||
|
- File:
|
||||||
|
- `executor.v`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Directory Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
stage/
|
||||||
|
interfaces/
|
||||||
|
jsonrpc_interface.v # Converts JSON-RPC requests to Actions
|
||||||
|
openapi_interface.v # Converts OpenAPI requests to Actions
|
||||||
|
actor.v # Defines the Actor and its behavior
|
||||||
|
action.v # Defines the Action structure and utilities
|
||||||
|
executor.v # Executes Actions on Actors
|
||||||
|
director.v # (Planned) Coordinates actors, actions, and retries
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Workflow Example
|
||||||
|
|
||||||
|
### 1. Receiving an RPC Request
|
||||||
|
An RPC request is received by an interface handler:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "doSomething",
|
||||||
|
"params": { "key": "value" },
|
||||||
|
"id": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Converting the Request to an Action
|
||||||
|
The interface handler converts the request into an **Action**:
|
||||||
|
|
||||||
|
```v
|
||||||
|
action := jsonrpc_interface.jsonrpc_to_action(request)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Executing the Action
|
||||||
|
The action is passed directly to an **Actor** for execution:
|
||||||
|
|
||||||
|
```v
|
||||||
|
actor := MyActor{id: "actor-1"}
|
||||||
|
resulting_action := actor.act(action)
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Returning the RPC Response
|
||||||
|
The interface handler converts the resulting **Action** back into a JSON-RPC response:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"result": { "status": "success", "data": "..." },
|
||||||
|
"id": 1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Future Improvements
|
||||||
|
|
||||||
|
- **Director Implementation**:
|
||||||
|
- Add retries and timeout handling for actions.
|
||||||
|
- Provide better coordination for complex workflows.
|
||||||
|
|
||||||
|
- **Enhanced Interfaces**:
|
||||||
|
- Add support for more RPC protocols.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
This module is a crucial building block of the **Baobab** library, designed to streamline RPC-based communication and task execution with flexibility and scalability.
|
||||||
15
lib/baobab/stage/action.v
Normal file
15
lib/baobab/stage/action.v
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
module stage
|
||||||
|
|
||||||
|
// import freeflowuniverse.herolib.core.smartid
|
||||||
|
|
||||||
|
pub struct Action {
|
||||||
|
pub mut:
|
||||||
|
id string
|
||||||
|
name string
|
||||||
|
priority int = 10 // 0 is highest, do 10 as default
|
||||||
|
params string // json encoded params
|
||||||
|
result string // can be used to remember outputs
|
||||||
|
// run bool = true // certain actions can be defined but meant to be executed directly
|
||||||
|
comments string
|
||||||
|
done bool // if done then no longer need to process
|
||||||
|
}
|
||||||
47
lib/baobab/stage/action_client.v
Normal file
47
lib/baobab/stage/action_client.v
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
module stage
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.core.redisclient
|
||||||
|
|
||||||
|
// Processor struct for managing procedure calls
|
||||||
|
pub struct Client {
|
||||||
|
pub mut:
|
||||||
|
rpc redisclient.RedisRpc // Redis RPC mechanism
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parameters for processing a procedure call
|
||||||
|
@[params]
|
||||||
|
pub struct Params {
|
||||||
|
pub:
|
||||||
|
timeout int // Timeout in seconds
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ClientConfig {
|
||||||
|
pub:
|
||||||
|
redis_url string // url to redis server running
|
||||||
|
redis_queue string // name of redis queue
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_client(config ClientConfig) !Client {
|
||||||
|
mut redis := redisclient.new(config.redis_url)!
|
||||||
|
mut rpc_q := redis.rpc_get(config.redis_queue)
|
||||||
|
|
||||||
|
return Client{
|
||||||
|
rpc: rpc_q
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the procedure call
|
||||||
|
pub fn (mut p Client) call_to_action(action Action, params Params) !Action {
|
||||||
|
// Use RedisRpc's `call` to send the call and wait for the response
|
||||||
|
response_data := p.rpc.call(redisclient.RPCArgs{
|
||||||
|
cmd: action.name
|
||||||
|
data: action.params
|
||||||
|
timeout: u64(params.timeout * 1000) // Convert seconds to milliseconds
|
||||||
|
wait: true
|
||||||
|
})!
|
||||||
|
|
||||||
|
return Action {
|
||||||
|
...action
|
||||||
|
result: response_data
|
||||||
|
}
|
||||||
|
}
|
||||||
45
lib/baobab/stage/actor.v
Normal file
45
lib/baobab/stage/actor.v
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
module stage
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.osis {OSIS}
|
||||||
|
|
||||||
|
@[heap]
|
||||||
|
pub interface IActor {
|
||||||
|
name string
|
||||||
|
mut:
|
||||||
|
act(Action) !Action
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Actor {
|
||||||
|
pub:
|
||||||
|
name string
|
||||||
|
redis_url string = 'localhost:6379'
|
||||||
|
mut:
|
||||||
|
osis OSIS
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_actor(name string) !Actor {
|
||||||
|
return Actor{
|
||||||
|
osis: osis.new()!
|
||||||
|
name: name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut a IActor) handle(method string, data string) !string {
|
||||||
|
action := a.act(
|
||||||
|
name: method
|
||||||
|
params: data
|
||||||
|
)!
|
||||||
|
return action.result
|
||||||
|
}
|
||||||
|
|
||||||
|
// // Actor listens to the Redis queue for method invocations
|
||||||
|
// pub fn (mut a IActor) run() ! {
|
||||||
|
// mut redis := redisclient.new('localhost:6379') or { panic(err) }
|
||||||
|
// mut rpc := redis.rpc_get(a.name)
|
||||||
|
|
||||||
|
// println('Actor started and listening for tasks...')
|
||||||
|
// for {
|
||||||
|
// rpc.process(a.handle)!
|
||||||
|
// time.sleep(time.millisecond * 100) // Prevent CPU spinning
|
||||||
|
// }
|
||||||
|
// }
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
module action
|
module stage
|
||||||
|
|
||||||
// Error struct for error handling
|
// Error struct for error handling
|
||||||
pub struct ActionError {
|
pub struct ActionError {
|
||||||
16
lib/baobab/stage/interfaces/jsonrpc.v
Normal file
16
lib/baobab/stage/interfaces/jsonrpc.v
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
module interfaces
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonrpc
|
||||||
|
import freeflowuniverse.herolib.baobab.stage {Action}
|
||||||
|
|
||||||
|
pub fn action_from_jsonrpc_request(request jsonrpc.Request) Action {
|
||||||
|
return Action{
|
||||||
|
id: request.id
|
||||||
|
name: request.method
|
||||||
|
params: request.params
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn action_to_jsonrpc_response(action Action) jsonrpc.Response {
|
||||||
|
return jsonrpc.new_response(action.id, action.result)
|
||||||
|
}
|
||||||
53
lib/baobab/stage/interfaces/openapi.v
Normal file
53
lib/baobab/stage/interfaces/openapi.v
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
module interfaces
|
||||||
|
|
||||||
|
import rand
|
||||||
|
import x.json2 as json {Any}
|
||||||
|
import freeflowuniverse.herolib.baobab.stage {Action, Client}
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonrpc
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi
|
||||||
|
|
||||||
|
pub struct OpenAPIInterface {
|
||||||
|
pub mut:
|
||||||
|
client Client
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_openapi_interface(client Client) &OpenAPIInterface {
|
||||||
|
return &OpenAPIInterface{client}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut i OpenAPIInterface) handle(request openapi.Request) !openapi.Response {
|
||||||
|
// Convert incoming OpenAPI request to a procedure call
|
||||||
|
action := action_from_openapi_request(request)
|
||||||
|
println('debugzo3 ${action}')
|
||||||
|
response := i.client.call_to_action(action) or {
|
||||||
|
println('debugzo3.5 ${err.msg()}')
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
println('debugzo4 ${response}')
|
||||||
|
return action_to_openapi_response(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn action_from_openapi_request(request openapi.Request) Action {
|
||||||
|
mut params := []Any{}
|
||||||
|
if request.arguments.len > 0 {
|
||||||
|
params << request.arguments.values()
|
||||||
|
}
|
||||||
|
if request.body != '' {
|
||||||
|
params << request.body
|
||||||
|
}
|
||||||
|
if request.parameters.len > 0 {
|
||||||
|
params << json.encode(request.parameters)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Action {
|
||||||
|
id: rand.uuid_v4()
|
||||||
|
name: request.operation.operation_id
|
||||||
|
params: json.encode(params.str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn action_to_openapi_response(action Action) openapi.Response {
|
||||||
|
return openapi.Response {
|
||||||
|
body: action.result
|
||||||
|
}
|
||||||
|
}
|
||||||
29
lib/baobab/stage/interfaces/openrpc.v
Normal file
29
lib/baobab/stage/interfaces/openrpc.v
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
module interfaces
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.baobab.stage {Client}
|
||||||
|
import freeflowuniverse.herolib.schemas.jsonrpc
|
||||||
|
|
||||||
|
// handler for test echoes JSONRPC Request as JSONRPC Response
|
||||||
|
fn handler(request jsonrpc.Request) !jsonrpc.Response {
|
||||||
|
return jsonrpc.Response {
|
||||||
|
jsonrpc: request.jsonrpc
|
||||||
|
id: request.id
|
||||||
|
result: request.params
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct OpenRPCInterface {
|
||||||
|
pub mut:
|
||||||
|
client Client
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_openrpc_interface(client Client) &OpenRPCInterface {
|
||||||
|
return &OpenRPCInterface{client}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut i OpenRPCInterface) handle(request jsonrpc.Request) !jsonrpc.Response {
|
||||||
|
// Convert incoming OpenAPI request to a procedure call
|
||||||
|
action := action_from_jsonrpc_request(request)
|
||||||
|
response := i.client.call_to_action(action)!
|
||||||
|
return action_to_jsonrpc_response(response)
|
||||||
|
}
|
||||||
91
lib/baobab/stage/interfaces/reflection_openapi.v
Normal file
91
lib/baobab/stage/interfaces/reflection_openapi.v
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
module interfaces
|
||||||
|
|
||||||
|
// import os
|
||||||
|
// import time
|
||||||
|
// import veb
|
||||||
|
// import x.json2 {Any}
|
||||||
|
// import net.http
|
||||||
|
import freeflowuniverse.herolib.baobab.stage {Action}
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi {Request}
|
||||||
|
|
||||||
|
pub fn openapi_request_to_action(request Request) Action {
|
||||||
|
// // Convert incoming OpenAPI request to a procedure call
|
||||||
|
// mut params := []Any{}
|
||||||
|
|
||||||
|
// if request.arguments.len > 0 {
|
||||||
|
// params << request.arguments.values().map(it.str()).clone()
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if request.body != '' {
|
||||||
|
// params << request.body
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if request.parameters != '' {
|
||||||
|
// params << request.body
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if request.parameters.len != 0 {
|
||||||
|
// mut param_map := map[string]Any{} // Store parameters with correct types
|
||||||
|
|
||||||
|
// for param_name, param_value in request.parameters {
|
||||||
|
// operation_param := request.operation.parameters.filter(it.name == param_name)
|
||||||
|
// if operation_param.len > 0 {
|
||||||
|
// param_schema := operation_param[0].schema as Schema
|
||||||
|
// param_type := param_schema.typ
|
||||||
|
// param_format := param_schema.format
|
||||||
|
|
||||||
|
// // Convert parameter value to corresponding type
|
||||||
|
// match param_type {
|
||||||
|
// 'integer' {
|
||||||
|
// match param_format {
|
||||||
|
// 'int32' {
|
||||||
|
// param_map[param_name] = param_value.int() // Convert to int
|
||||||
|
// }
|
||||||
|
// 'int64' {
|
||||||
|
// param_map[param_name] = param_value.i64() // Convert to i64
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// param_map[param_name] = param_value.int() // Default to int
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// 'string' {
|
||||||
|
// param_map[param_name] = param_value // Already a string
|
||||||
|
// }
|
||||||
|
// 'boolean' {
|
||||||
|
// param_map[param_name] = param_value.bool() // Convert to bool
|
||||||
|
// }
|
||||||
|
// 'number' {
|
||||||
|
// match param_format {
|
||||||
|
// 'float' {
|
||||||
|
// param_map[param_name] = param_value.f32() // Convert to float
|
||||||
|
// }
|
||||||
|
// 'double' {
|
||||||
|
// param_map[param_name] = param_value.f64() // Convert to double
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// param_map[param_name] = param_value.f64() // Default to double
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// else {
|
||||||
|
// param_map[param_name] = param_value // Leave as string for unknown types
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// // If the parameter is not defined in the OpenAPI operation, skip or log it
|
||||||
|
// println('Unknown parameter: $param_name')
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Encode the parameter map to JSON if needed
|
||||||
|
// params << json.encode(param_map.str())
|
||||||
|
// }
|
||||||
|
|
||||||
|
// call := Action{
|
||||||
|
// name: request.operation.operation_id
|
||||||
|
// params_json: json2.encode(params.str()) // Keep as a string since ProcedureCall expects a string
|
||||||
|
// }
|
||||||
|
// return call
|
||||||
|
return Action{}
|
||||||
|
}
|
||||||
43
lib/baobab/stage/interfaces/server_http.v
Normal file
43
lib/baobab/stage/interfaces/server_http.v
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
module interfaces
|
||||||
|
|
||||||
|
import freeflowuniverse.herolib.schemas.openapi { OpenAPI }
|
||||||
|
import freeflowuniverse.herolib.baobab.stage {Client, ClientConfig}
|
||||||
|
import freeflowuniverse.herolib.schemas.openrpc { OpenRPC }
|
||||||
|
import veb
|
||||||
|
|
||||||
|
pub struct HTTPServer {
|
||||||
|
veb.Controller
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Context {
|
||||||
|
veb.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct HTTPServerConfig {
|
||||||
|
ClientConfig
|
||||||
|
pub:
|
||||||
|
openapi_specification OpenAPI
|
||||||
|
openrpc_specification OpenRPC
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_http_server() !&HTTPServer {
|
||||||
|
mut s := &HTTPServer{}
|
||||||
|
|
||||||
|
// client := actor.new_client(cfg.ClientConfig)!
|
||||||
|
|
||||||
|
// openapi_proxy := new_openapi_proxy(
|
||||||
|
// client: new_client(cfg.ClientConfig)!
|
||||||
|
// specification: cfg.openapi_spec
|
||||||
|
// )
|
||||||
|
|
||||||
|
// mut openrpc_controller := openrpc.new_http_controller(
|
||||||
|
// specification: cfg.openrpc_specification
|
||||||
|
// handler: new_openrpc_interface(client)
|
||||||
|
// )
|
||||||
|
// s.register_controller[openrpc.HTTPController, Context]('/openrpc', mut openrpc_controller)!
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (mut server HTTPServer) run() {
|
||||||
|
veb.run[HTTPServer, Context](mut server, 8082)
|
||||||
|
}
|
||||||
@@ -1,148 +0,0 @@
|
|||||||
# Code Model
|
|
||||||
|
|
||||||
A set of models that represent code structures, such as structs, functions, imports, and constants. The motivation behind this module is to provide a more generic and lighter alternative to v.ast code models, that can be used for code parsing and code generation across multiple languages.
|
|
||||||
|
|
||||||
## Features
|
|
||||||
|
|
||||||
- **Struct Modeling**: Complete struct representation including:
|
|
||||||
- Fields with types, visibility, and mutability
|
|
||||||
- Embedded structs
|
|
||||||
- Generic type support
|
|
||||||
- Attributes
|
|
||||||
- Documentation comments
|
|
||||||
|
|
||||||
- **Function Modeling**: Comprehensive function support with:
|
|
||||||
- Parameters and return types
|
|
||||||
- Receiver methods
|
|
||||||
- Optional and result types
|
|
||||||
- Function body content
|
|
||||||
- Visibility modifiers
|
|
||||||
|
|
||||||
- **Type System**: Rich type representation including:
|
|
||||||
- Basic types
|
|
||||||
- Reference types
|
|
||||||
- Arrays and maps
|
|
||||||
- Optional and result types
|
|
||||||
- Mutable and shared types
|
|
||||||
|
|
||||||
- **Code Organization**:
|
|
||||||
- Import statements with module and type specifications
|
|
||||||
- Constants (both single and grouped)
|
|
||||||
- Custom code blocks for specialized content
|
|
||||||
- Documentation through single and multi-line comments
|
|
||||||
|
|
||||||
## Using Codemodel
|
|
||||||
|
|
||||||
The codemodel module provides a set of types and utilities for working with code structures. Here are some examples of how to use the module:
|
|
||||||
|
|
||||||
### Working with Functions
|
|
||||||
|
|
||||||
```v
|
|
||||||
// Parse a function definition
|
|
||||||
fn_def := 'pub fn (mut app App) process() !string'
|
|
||||||
function := codemodel.parse_function(fn_def)!
|
|
||||||
println(function.name) // prints: process
|
|
||||||
println(function.receiver.name) // prints: app
|
|
||||||
println(function.result.typ.symbol) // prints: string
|
|
||||||
|
|
||||||
// Create a function model
|
|
||||||
my_fn := Function{
|
|
||||||
name: 'add'
|
|
||||||
is_pub: true
|
|
||||||
params: [
|
|
||||||
Param{
|
|
||||||
name: 'x'
|
|
||||||
typ: Type{symbol: 'int'}
|
|
||||||
},
|
|
||||||
Param{
|
|
||||||
name: 'y'
|
|
||||||
typ: Type{symbol: 'int'}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
result: Result{
|
|
||||||
typ: Type{symbol: 'int'}
|
|
||||||
}
|
|
||||||
body: 'return x + y'
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Working with Imports
|
|
||||||
|
|
||||||
```v
|
|
||||||
// Parse an import statement
|
|
||||||
import_def := 'import os { exists }'
|
|
||||||
imp := codemodel.parse_import(import_def)
|
|
||||||
println(imp.mod) // prints: os
|
|
||||||
println(imp.types) // prints: ['exists']
|
|
||||||
|
|
||||||
// Create an import model
|
|
||||||
my_import := Import{
|
|
||||||
mod: 'json'
|
|
||||||
types: ['encode', 'decode']
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Working with Constants
|
|
||||||
|
|
||||||
```v
|
|
||||||
// Parse constant definitions
|
|
||||||
const_def := 'const max_size = 1000'
|
|
||||||
constant := codemodel.parse_const(const_def)!
|
|
||||||
println(constant.name) // prints: max_size
|
|
||||||
println(constant.value) // prints: 1000
|
|
||||||
|
|
||||||
// Parse grouped constants
|
|
||||||
const_block := 'const (
|
|
||||||
pi = 3.14
|
|
||||||
e = 2.718
|
|
||||||
)'
|
|
||||||
constants := codemodel.parse_consts(const_block)!
|
|
||||||
```
|
|
||||||
|
|
||||||
### Working with Types
|
|
||||||
|
|
||||||
The module provides rich type modeling capabilities:
|
|
||||||
|
|
||||||
```v
|
|
||||||
// Basic type
|
|
||||||
basic := Type{
|
|
||||||
symbol: 'string'
|
|
||||||
}
|
|
||||||
|
|
||||||
// Array type
|
|
||||||
array := Type{
|
|
||||||
symbol: 'string'
|
|
||||||
is_array: true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Optional type
|
|
||||||
optional := Type{
|
|
||||||
symbol: 'int'
|
|
||||||
is_optional: true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Result type
|
|
||||||
result := Type{
|
|
||||||
symbol: 'string'
|
|
||||||
is_result: true
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Code Generation
|
|
||||||
|
|
||||||
The codemodel types can be used as intermediate structures for code generation. For example, generating documentation:
|
|
||||||
|
|
||||||
```v
|
|
||||||
mut doc := ''
|
|
||||||
|
|
||||||
// Document a struct
|
|
||||||
for field in my_struct.fields {
|
|
||||||
doc += '- ${field.name}: ${field.typ.symbol}'
|
|
||||||
if field.description != '' {
|
|
||||||
doc += ' // ${field.description}'
|
|
||||||
}
|
|
||||||
doc += '\n'
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The codemodel module provides a foundation for building tools that need to work with code structures, whether for analysis, transformation, or generation purposes.
|
|
||||||
@@ -1,205 +0,0 @@
|
|||||||
module codemodel
|
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.pathlib
|
|
||||||
// Code is a list of statements
|
|
||||||
// pub type Code = []CodeItem
|
|
||||||
|
|
||||||
pub type CodeItem = Alias | Comment | CustomCode | Function | Import | Struct | Sumtype
|
|
||||||
|
|
||||||
// item for adding custom code in
|
|
||||||
pub struct CustomCode {
|
|
||||||
pub:
|
|
||||||
text string
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Comment {
|
|
||||||
pub:
|
|
||||||
text string
|
|
||||||
is_multi bool
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Struct {
|
|
||||||
pub mut:
|
|
||||||
name string
|
|
||||||
description string
|
|
||||||
mod string
|
|
||||||
is_pub bool
|
|
||||||
embeds []Struct @[str: skip]
|
|
||||||
generics map[string]string @[str: skip]
|
|
||||||
attrs []Attribute
|
|
||||||
fields []StructField
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Sumtype {
|
|
||||||
pub:
|
|
||||||
name string
|
|
||||||
description string
|
|
||||||
types []Type
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct StructField {
|
|
||||||
pub mut:
|
|
||||||
comments []Comment
|
|
||||||
attrs []Attribute
|
|
||||||
name string
|
|
||||||
description string
|
|
||||||
default string
|
|
||||||
is_pub bool
|
|
||||||
is_mut bool
|
|
||||||
is_ref bool
|
|
||||||
anon_struct Struct @[str: skip] // sometimes fields may hold anonymous structs
|
|
||||||
typ Type
|
|
||||||
structure Struct @[str: skip]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Attribute {
|
|
||||||
pub:
|
|
||||||
name string // [name]
|
|
||||||
has_arg bool
|
|
||||||
arg string // [name: arg]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Function {
|
|
||||||
pub:
|
|
||||||
name string
|
|
||||||
receiver Param
|
|
||||||
is_pub bool
|
|
||||||
mod string
|
|
||||||
pub mut:
|
|
||||||
description string
|
|
||||||
params []Param
|
|
||||||
body string
|
|
||||||
result Result
|
|
||||||
has_return bool
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_function(code_ string) !Function {
|
|
||||||
mut code := code_.trim_space()
|
|
||||||
is_pub := code.starts_with('pub ')
|
|
||||||
if is_pub {
|
|
||||||
code = code.trim_string_left('pub ').trim_space()
|
|
||||||
}
|
|
||||||
|
|
||||||
is_fn := code.starts_with('fn ')
|
|
||||||
if !is_fn {
|
|
||||||
return error('invalid function format')
|
|
||||||
}
|
|
||||||
code = code.trim_string_left('fn ').trim_space()
|
|
||||||
|
|
||||||
receiver := if code.starts_with('(') {
|
|
||||||
param_str := code.all_after('(').all_before(')').trim_space()
|
|
||||||
code = code.all_after(')').trim_space()
|
|
||||||
parse_param(param_str)!
|
|
||||||
} else {
|
|
||||||
Param{}
|
|
||||||
}
|
|
||||||
|
|
||||||
name := code.all_before('(').trim_space()
|
|
||||||
code = code.trim_string_left(name).trim_space()
|
|
||||||
|
|
||||||
params_str := code.all_after('(').all_before(')')
|
|
||||||
params := if params_str.trim_space() != '' {
|
|
||||||
params_str_lst := params_str.split(',')
|
|
||||||
params_str_lst.map(parse_param(it)!)
|
|
||||||
} else {
|
|
||||||
[]Param{}
|
|
||||||
}
|
|
||||||
result := parse_result(code.all_after(')').all_before('{').replace(' ', ''))!
|
|
||||||
|
|
||||||
body := if code.contains('{') { code.all_after('{').all_before_last('}') } else { '' }
|
|
||||||
return Function{
|
|
||||||
name: name
|
|
||||||
receiver: receiver
|
|
||||||
params: params
|
|
||||||
result: result
|
|
||||||
body: body
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_param(code_ string) !Param {
|
|
||||||
mut code := code_.trim_space()
|
|
||||||
is_mut := code.starts_with('mut ')
|
|
||||||
if is_mut {
|
|
||||||
code = code.trim_string_left('mut ').trim_space()
|
|
||||||
}
|
|
||||||
split := code.split(' ').filter(it != '')
|
|
||||||
if split.len != 2 {
|
|
||||||
return error('invalid param format: ${code_}')
|
|
||||||
}
|
|
||||||
return Param{
|
|
||||||
name: split[0]
|
|
||||||
typ: Type{
|
|
||||||
symbol: split[1]
|
|
||||||
}
|
|
||||||
mutable: is_mut
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_result(code_ string) !Result {
|
|
||||||
code := code_.replace(' ', '').trim_space()
|
|
||||||
|
|
||||||
return Result{
|
|
||||||
result: code_.starts_with('!')
|
|
||||||
optional: code_.starts_with('?')
|
|
||||||
typ: Type{
|
|
||||||
symbol: code.trim('!?')
|
|
||||||
is_optional: code.starts_with('?')
|
|
||||||
is_result: code.starts_with('!')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Param {
|
|
||||||
pub:
|
|
||||||
required bool
|
|
||||||
mutable bool
|
|
||||||
is_shared bool
|
|
||||||
is_optional bool
|
|
||||||
description string
|
|
||||||
name string
|
|
||||||
typ Type
|
|
||||||
struct_ Struct
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Result {
|
|
||||||
pub mut:
|
|
||||||
typ Type
|
|
||||||
description string
|
|
||||||
name string
|
|
||||||
result bool // whether is result type
|
|
||||||
optional bool // whether is result type
|
|
||||||
structure Struct
|
|
||||||
}
|
|
||||||
|
|
||||||
// todo: maybe make 'is_' fields methods?
|
|
||||||
pub struct Type {
|
|
||||||
pub mut:
|
|
||||||
is_reference bool @[str: skip]
|
|
||||||
is_map bool @[str: skip]
|
|
||||||
is_array bool
|
|
||||||
is_mutable bool @[str: skip]
|
|
||||||
is_shared bool @[str: skip]
|
|
||||||
is_optional bool @[str: skip]
|
|
||||||
is_result bool @[str: skip]
|
|
||||||
symbol string
|
|
||||||
mod string @[str: skip]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct File {
|
|
||||||
pub mut:
|
|
||||||
name string
|
|
||||||
extension string
|
|
||||||
content string
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (f File) write(path string) ! {
|
|
||||||
mut fd_file := pathlib.get_file(path: '${path}/${f.name}.${f.extension}')!
|
|
||||||
fd_file.write(f.content)!
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Alias {
|
|
||||||
pub:
|
|
||||||
name string
|
|
||||||
description string
|
|
||||||
typ Type
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
module codemodel
|
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.pathlib
|
|
||||||
import os
|
|
||||||
|
|
||||||
pub struct Module {
|
|
||||||
pub mut:
|
|
||||||
name string
|
|
||||||
files []CodeFile
|
|
||||||
misc_files []File
|
|
||||||
// model CodeFile
|
|
||||||
// methods CodeFile
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (mod Module) write_v(path string, options WriteOptions) ! {
|
|
||||||
mut module_dir := pathlib.get_dir(
|
|
||||||
path: '${path}/${mod.name}'
|
|
||||||
empty: options.overwrite
|
|
||||||
)!
|
|
||||||
|
|
||||||
if !options.overwrite && module_dir.exists() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
for file in mod.files {
|
|
||||||
file.write_v(module_dir.path, options)!
|
|
||||||
}
|
|
||||||
for file in mod.misc_files {
|
|
||||||
file.write(module_dir.path)!
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.format {
|
|
||||||
os.execute('v fmt -w ${module_dir.path}')
|
|
||||||
}
|
|
||||||
if options.document {
|
|
||||||
os.execute('v doc -f html -o ${module_dir.path}/docs ${module_dir.path}')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,274 +0,0 @@
|
|||||||
module codemodel
|
|
||||||
|
|
||||||
import os
|
|
||||||
import freeflowuniverse.herolib.core.texttools
|
|
||||||
import freeflowuniverse.herolib.core.pathlib
|
|
||||||
import freeflowuniverse.herolib.ui.console
|
|
||||||
|
|
||||||
pub struct WriteCode {
|
|
||||||
destination string
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ICodeItem {
|
|
||||||
vgen() string
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn vgen(code []CodeItem) string {
|
|
||||||
mut str := ''
|
|
||||||
for item in code {
|
|
||||||
if item is Function {
|
|
||||||
str += '\n${item.vgen()}'
|
|
||||||
}
|
|
||||||
if item is Struct {
|
|
||||||
str += '\n${item.vgen()}'
|
|
||||||
}
|
|
||||||
if item is CustomCode {
|
|
||||||
str += '\n${item.vgen()}'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
// pub fn (code Code) vgen() string {
|
|
||||||
// return code.items.map(it.vgen()).join_lines()
|
|
||||||
// }
|
|
||||||
|
|
||||||
// vgen_import generates an import statement for a given type
|
|
||||||
pub fn (import_ Import) vgen() string {
|
|
||||||
types_str := if import_.types.len > 0 {
|
|
||||||
'{${import_.types.join(', ')}}'
|
|
||||||
} else {
|
|
||||||
''
|
|
||||||
} // comma separated string list of types
|
|
||||||
return 'import ${import_.mod} ${types_str}'
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: enfore that cant be both mutable and shared
|
|
||||||
pub fn (type_ Type) vgen() string {
|
|
||||||
mut type_str := ''
|
|
||||||
if type_.is_mutable {
|
|
||||||
type_str += 'mut '
|
|
||||||
} else if type_.is_shared {
|
|
||||||
type_str += 'shared '
|
|
||||||
}
|
|
||||||
|
|
||||||
if type_.is_optional {
|
|
||||||
type_str += '?'
|
|
||||||
} else if type_.is_result {
|
|
||||||
type_str += '!'
|
|
||||||
}
|
|
||||||
|
|
||||||
return '${type_str} ${type_.symbol}'
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (field StructField) vgen() string {
|
|
||||||
symbol := field.get_type_symbol()
|
|
||||||
mut vstr := '${field.name} ${symbol}'
|
|
||||||
if field.description != '' {
|
|
||||||
vstr += '// ${field.description}'
|
|
||||||
}
|
|
||||||
return vstr
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (field StructField) get_type_symbol() string {
|
|
||||||
mut field_str := if field.structure.name != '' {
|
|
||||||
field.structure.get_type_symbol()
|
|
||||||
} else {
|
|
||||||
field.typ.symbol
|
|
||||||
}
|
|
||||||
|
|
||||||
if field.is_ref {
|
|
||||||
field_str = '&${field_str}'
|
|
||||||
}
|
|
||||||
|
|
||||||
return field_str
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (structure Struct) get_type_symbol() string {
|
|
||||||
mut symbol := if structure.mod != '' {
|
|
||||||
'${structure.mod.all_after_last('.')}.${structure.name}'
|
|
||||||
} else {
|
|
||||||
structure.name
|
|
||||||
}
|
|
||||||
if structure.generics.len > 0 {
|
|
||||||
symbol = '${symbol}${vgen_generics(structure.generics)}'
|
|
||||||
}
|
|
||||||
|
|
||||||
return symbol
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn vgen_generics(generics map[string]string) string {
|
|
||||||
if generics.keys().len == 0 {
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
mut vstr := '['
|
|
||||||
for key, val in generics {
|
|
||||||
vstr += if val != '' { val } else { key }
|
|
||||||
}
|
|
||||||
return '${vstr}]'
|
|
||||||
}
|
|
||||||
|
|
||||||
// vgen_function generates a function statement for a function
|
|
||||||
pub fn (function Function) vgen(options WriteOptions) string {
|
|
||||||
mut params_ := function.params.map(Param{
|
|
||||||
...it
|
|
||||||
typ: Type{
|
|
||||||
symbol: if it.struct_.name != '' {
|
|
||||||
it.struct_.name
|
|
||||||
} else {
|
|
||||||
it.typ.symbol
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
optionals := params_.filter(it.is_optional)
|
|
||||||
options_struct := Struct{
|
|
||||||
name: '${texttools.name_fix_snake_to_pascal(function.name)}Options'
|
|
||||||
attrs: [Attribute{
|
|
||||||
name: 'params'
|
|
||||||
}]
|
|
||||||
fields: optionals.map(StructField{
|
|
||||||
name: it.name
|
|
||||||
description: it.description
|
|
||||||
typ: Type{
|
|
||||||
symbol: it.typ.symbol
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if optionals.len > 0 {
|
|
||||||
params_ << Param{
|
|
||||||
name: 'options'
|
|
||||||
typ: Type{
|
|
||||||
symbol: options_struct.name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
params := params_.filter(!it.is_optional).map('${it.name} ${it.typ.symbol}').join(', ')
|
|
||||||
|
|
||||||
receiver := function.receiver.vgen()
|
|
||||||
|
|
||||||
mut function_str := $tmpl('templates/function/function.v.template')
|
|
||||||
|
|
||||||
// if options.format {
|
|
||||||
// result := os.execute_opt('echo "${function_str.replace('$', '\\$')}" | v fmt') or {
|
|
||||||
// panic('${function_str}\n${err}')
|
|
||||||
// }
|
|
||||||
// function_str = result.output
|
|
||||||
// }
|
|
||||||
function_str = function_str.split_into_lines().filter(!it.starts_with('import ')).join('\n')
|
|
||||||
|
|
||||||
return if options_struct.fields.len != 0 {
|
|
||||||
'${options_struct.vgen()}\n${function_str}'
|
|
||||||
} else {
|
|
||||||
function_str
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (param Param) vgen() string {
|
|
||||||
if param.name == '' {
|
|
||||||
return ''
|
|
||||||
}
|
|
||||||
sym := if param.struct_.name != '' {
|
|
||||||
param.struct_.get_type_symbol()
|
|
||||||
} else {
|
|
||||||
param.typ.symbol
|
|
||||||
}
|
|
||||||
|
|
||||||
mut vstr := '${param.name} ${sym}'
|
|
||||||
if param.typ.is_reference {
|
|
||||||
vstr = '&${vstr}'
|
|
||||||
}
|
|
||||||
if param.mutable {
|
|
||||||
vstr = 'mut ${vstr}'
|
|
||||||
}
|
|
||||||
return '(${vstr})'
|
|
||||||
}
|
|
||||||
|
|
||||||
// vgen_function generates a function statement for a function
|
|
||||||
pub fn (struct_ Struct) vgen() string {
|
|
||||||
gen := VGenerator{false}
|
|
||||||
return gen.generate_struct(struct_) or { panic(err) }
|
|
||||||
// mut struct_str := $tmpl('templates/struct/struct.v.template')
|
|
||||||
// return struct_str
|
|
||||||
// result := os.execute_opt('echo "${struct_str.replace('$', '\$')}" | v fmt') or {panic(err)}
|
|
||||||
// return result.output
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct VGenerator {
|
|
||||||
format bool
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (gen VGenerator) generate_struct(struct_ Struct) !string {
|
|
||||||
name := if struct_.generics.len > 0 {
|
|
||||||
'${struct_.name}${vgen_generics(struct_.generics)}'
|
|
||||||
} else {
|
|
||||||
struct_.name
|
|
||||||
}
|
|
||||||
|
|
||||||
prefix := if struct_.is_pub {
|
|
||||||
'pub'
|
|
||||||
} else {
|
|
||||||
''
|
|
||||||
}
|
|
||||||
|
|
||||||
priv_fields := struct_.fields.filter(!it.is_mut && !it.is_pub).map(gen.generate_struct_field(it))
|
|
||||||
pub_fields := struct_.fields.filter(!it.is_mut && it.is_pub).map(gen.generate_struct_field(it))
|
|
||||||
mut_fields := struct_.fields.filter(it.is_mut && !it.is_pub).map(gen.generate_struct_field(it))
|
|
||||||
pub_mut_fields := struct_.fields.filter(it.is_mut && it.is_pub).map(gen.generate_struct_field(it))
|
|
||||||
|
|
||||||
mut struct_str := $tmpl('templates/struct/struct.v.template')
|
|
||||||
if gen.format {
|
|
||||||
result := os.execute_opt('echo "${struct_str.replace('$', '\$')}" | v fmt') or {
|
|
||||||
console.print_debug(struct_str)
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return result.output
|
|
||||||
}
|
|
||||||
return struct_str
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (gen VGenerator) generate_struct_field(field StructField) string {
|
|
||||||
symbol := field.get_type_symbol()
|
|
||||||
mut vstr := '${field.name} ${symbol}'
|
|
||||||
if field.description != '' {
|
|
||||||
vstr += '// ${field.description}'
|
|
||||||
}
|
|
||||||
return vstr
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (custom CustomCode) vgen() string {
|
|
||||||
return custom.text
|
|
||||||
}
|
|
||||||
|
|
||||||
// vgen_function generates a function statement for a function
|
|
||||||
pub fn (result Result) vgen() string {
|
|
||||||
result_type := if result.structure.name != '' {
|
|
||||||
result.structure.get_type_symbol()
|
|
||||||
} else if result.typ.symbol == 'void' {
|
|
||||||
''
|
|
||||||
} else {
|
|
||||||
if result.typ.is_array {
|
|
||||||
'[]${result.typ.symbol}'
|
|
||||||
} else {
|
|
||||||
result.typ.symbol
|
|
||||||
}
|
|
||||||
}
|
|
||||||
str := if result.result {
|
|
||||||
'!'
|
|
||||||
} else if result.typ.is_result {
|
|
||||||
'!'
|
|
||||||
} else {
|
|
||||||
''
|
|
||||||
}
|
|
||||||
return '${str}${result_type}'
|
|
||||||
}
|
|
||||||
|
|
||||||
@[params]
|
|
||||||
pub struct WriteOptions {
|
|
||||||
pub:
|
|
||||||
format bool
|
|
||||||
overwrite bool
|
|
||||||
document bool
|
|
||||||
prefix string
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
module imagemagick
|
module imagemagick
|
||||||
|
|
||||||
import freeflowuniverse.herolib.core.pathlib
|
import freeflowuniverse.herolib.core.pathlib
|
||||||
import freeflowuniverse.herolib.data.paramsparser
|
|
||||||
import freeflowuniverse.herolib.osal
|
import freeflowuniverse.herolib.osal
|
||||||
import freeflowuniverse.herolib.ui.console
|
import freeflowuniverse.herolib.ui.console
|
||||||
|
|
||||||
@@ -64,7 +63,10 @@ pub fn installed() bool {
|
|||||||
return installed1
|
return installed1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter_imagemagic(mut path pathlib.Path, mut params_ paramsparser.Params) !bool {
|
pub struct FilterImageMagic {}
|
||||||
|
|
||||||
|
fn (f FilterImageMagic) filter (path_ pathlib.Path) !bool {
|
||||||
|
mut path := path_
|
||||||
// console.print_debug(" - check $path.path")
|
// console.print_debug(" - check $path.path")
|
||||||
// console.print_debug(" ===== "+path.name_no_ext())
|
// console.print_debug(" ===== "+path.name_no_ext())
|
||||||
if path.name().starts_with('.') {
|
if path.name().starts_with('.') {
|
||||||
@@ -100,28 +102,22 @@ fn filter_imagemagic(mut path pathlib.Path, mut params_ paramsparser.Params) !bo
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn executor_imagemagic(mut path pathlib.Path, mut params_ paramsparser.Params) !paramsparser.Params {
|
pub struct ExecutorImageMagic {
|
||||||
|
pub mut:
|
||||||
|
backupdir string
|
||||||
|
redo bool // if you want to check the file again, even if processed
|
||||||
|
convertpng bool // if yes will go from png to jpg
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (e ExecutorImageMagic) execute(path_ pathlib.Path) ! {
|
||||||
|
mut path := path_
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
return params_
|
return params_
|
||||||
}
|
}
|
||||||
// console.print_debug(' image check ${path.path}')
|
|
||||||
mut backupdir := ''
|
|
||||||
if params_.exists('backupdir') {
|
|
||||||
backupdir = params_.get('backupdir') or { panic(error) }
|
|
||||||
}
|
|
||||||
mut image := image_new(mut path)
|
mut image := image_new(mut path)
|
||||||
mut redo := false
|
if e.backupdir.len > 0 {
|
||||||
if params_.exists('redo') {
|
image.downsize(backup: true, backup_dest: e.backupdir, e.redo: redo, convertpng: e.convertpng)!
|
||||||
redo = true
|
|
||||||
}
|
|
||||||
mut convertpng := false
|
|
||||||
if params_.exists('convertpng') {
|
|
||||||
convertpng = true
|
|
||||||
}
|
|
||||||
if backupdir.len > 0 {
|
|
||||||
image.downsize(backup: true, backup_dest: backupdir, redo: redo, convertpng: convertpng)!
|
|
||||||
} else {
|
} else {
|
||||||
image.downsize(redo: redo, convertpng: convertpng)!
|
image.downsize(redo: redo, convertpng: convertpng)!
|
||||||
}
|
}
|
||||||
return params_
|
|
||||||
}
|
}
|
||||||
38
lib/core/code/README.md
Normal file
38
lib/core/code/README.md
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
# Code Model
|
||||||
|
|
||||||
|
A set of models that represent code, such as structs and functions. The motivation behind this module is to provide a more generic, and lighter alternative to v.ast code models, that can be used for code parsing and code generation across multiple languages.
|
||||||
|
|
||||||
|
## Using Codemodel
|
||||||
|
|
||||||
|
While the models in this module can be used in any domain, the models here are used extensively in the modules [codeparser](../codeparser/) and codegen (under development). Below are examples on how codemodel can be used for parsing and generating code.
|
||||||
|
## Code parsing with codemodel
|
||||||
|
|
||||||
|
As shown in the example below, the codemodels returned by the parser can be used to infer information about the code written
|
||||||
|
|
||||||
|
```js
|
||||||
|
code := codeparser.parse("somedir") // code is a list of code models
|
||||||
|
|
||||||
|
num_functions := code.filter(it is Function).len
|
||||||
|
structs := code.filter(it is Struct)
|
||||||
|
println("This directory has ${num_functions} functions")
|
||||||
|
println('The directory has the structs: ${structs.map(it.name)}')
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
or can be used as intermediate structures to serialize code into some other format:
|
||||||
|
|
||||||
|
```js
|
||||||
|
code_md := ''
|
||||||
|
|
||||||
|
// describes the struct in markdown format
|
||||||
|
for struct in structs {
|
||||||
|
code_md += '# ${struct.name}'
|
||||||
|
code_md += 'Type: ${struct.typ.symbol()}'
|
||||||
|
code_md += '## Fields:'
|
||||||
|
for field in struct.fields {
|
||||||
|
code_md += '- ${field.name}'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The [openrpc/docgen](../openrpc/docgen/) module demonstrates a good use case, where codemodels are serialized into JSON schema's, to generate an OpenRPC description document from a client in v.
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
module codemodel
|
module code
|
||||||
|
|
||||||
pub struct Example {
|
pub struct Example {
|
||||||
function Function
|
function Function
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user