fix crystallib imports

This commit is contained in:
2024-12-29 18:00:58 +02:00
parent 705fbfdebc
commit df63231db5
11 changed files with 504 additions and 491 deletions

View File

@@ -5,7 +5,7 @@ works very well in combination with heroscript
## How to get the paramsparser
```v
import freeflowuniverse.crystallib.data.paramsparser
import freeflowuniverse.herolib.data.paramsparser
// Create new params from text
params := paramsparser.new("color:red size:'large' priority:1 enable:true")!
@@ -25,6 +25,7 @@ The parser supports several formats:
4. Comments: `// this is a comment`
Example:
```v
text := "name:'John Doe' age:30 active:true // user details"
params := paramsparser.new(text)!
@@ -59,6 +60,7 @@ progress := params.get_percentage("progress")!
The module supports various type conversions:
### Basic Types
- `get_int()`: Convert to int32
- `get_u32()`: Convert to unsigned 32-bit integer
- `get_u64()`: Convert to unsigned 64-bit integer
@@ -67,10 +69,12 @@ The module supports various type conversions:
- `get_percentage()`: Convert percentage string to float (e.g., "80%" → 0.8)
### Boolean Values
- `get_default_true()`: Returns true if value is empty, "1", "true", "y", or "yes"
- `get_default_false()`: Returns false if value is empty, "0", "false", "n", or "no"
### Lists
The module provides robust support for parsing and converting lists:
```v
@@ -89,6 +93,7 @@ clean_names := params.get_list_namefix("categories")!
```
Supported list types:
- `get_list()`: String list
- `get_list_u8()`, `get_list_u16()`, `get_list_u32()`, `get_list_u64()`: Unsigned integers
- `get_list_i8()`, `get_list_i16()`, `get_list_int()`, `get_list_i64()`: Signed integers
@@ -97,6 +102,7 @@ Supported list types:
Each list method has a corresponding `_default` version that accepts a default value.
Valid list formats:
```v
users: "john, jane,bob"
ids: "1,2,3,4,5"

View File

@@ -1,10 +1,9 @@
# module osal
import as
```vlang
import freeflowuniverse.crystallib.osal
import freeflowuniverse.herolib.osal
osal.ping...
@@ -46,7 +45,6 @@ pub enum CPUType {
## process
### execute jobs
```v
@@ -91,10 +89,10 @@ info returns like:
## other commands
fn bin_path() !string
fn cmd_add(args_ CmdAddArgs) !
fn bin*path() !string
fn cmd_add(args* CmdAddArgs) !
copy a binary to the right location on the local computer . e.g. is /usr/local/bin on linux . e.g. is ~/hero/bin on osx . will also add the bin location to the path of .zprofile and .zshrc (different per platform)
fn cmd_exists(cmd string) bool
fn cmd*exists(cmd string) bool
fn cmd_exists_profile(cmd string) bool
fn cmd_path(cmd string) !string
is same as executing which in OS returns path or error
@@ -117,7 +115,7 @@ fn done_get_str(key string) string
fn done_print() !
fn done_reset() !
fn done_set(key string, val string) !
fn download(args_ DownloadArgs) !pathlib.Path
fn download(args* DownloadArgs) !pathlib.Path
if name is not specified, then will be the filename part if the last ends in an extension like .md .txt .log .text ... the file will be downloaded
fn env_get(key string) !string
Returns the requested environment variable if it exists or throws an error if it does not
@@ -167,6 +165,7 @@ fn exec(cmd Command) !Job
process os.Process
```
return Job .
fn exec_string(cmd Command) !string
cmd is the cmd to execute can use ' ' and spaces if \n in cmd it will write it to ext and then execute with bash if die==false then will just return returncode,out but not return error if stdout will show stderr and stdout
@@ -175,7 +174,8 @@ fn exec_string(cmd Command) !string
Command argument: cmd string timeout int = 600 stdout bool = true die bool = true debug bool
return what needs to be executed can give it to bash -c ...
fn execute_debug(cmd string) !string
fn execute*debug(cmd string) !string
fn execute_interactive(cmd string) !
shortcut to execute a job interactive means in shell
fn execute_ok(cmd string) bool
@@ -205,7 +205,7 @@ fn load_env_file(file_path string) !
fn memdb_exists(key string) bool
fn memdb_get(key string) string
fn memdb_set(key string, val string)
fn package_install(name_ string) !
fn package_install(name* string) !
install a package will use right commands per platform
fn package_refresh() !
update the package list
@@ -221,8 +221,7 @@ fn processinfo_children(pid int) !ProcessMap
get all children of 1 process
fn processinfo_get(pid int) !ProcessInfo
get process info from 1 specific process returns
```
pub struct ProcessInfo {
` pub struct ProcessInfo {
pub mut:
cpu_perc f32
mem_perc f32
@@ -232,7 +231,7 @@ fn processinfo_get(pid int) !ProcessInfo
//resident memory
rss int
}
```
`
fn processinfo_get_byname(name string) ![]ProcessInfo
fn processinfo_with_children(pid int) !ProcessMap
return the process and its children
@@ -250,11 +249,10 @@ fn sleep(duration int)
sleep in seconds
fn tcp_port_test(args TcpPortTestArgs) bool
test if a tcp port answers
```
address string //192.168.8.8
` address string //192.168.8.8
port int = 22
timeout u16 = 2000 // total time in milliseconds to keep on trying
```
`
fn user_add(args UserArgs) !int
add's a user if the user does not exist yet
fn user_exists(username string) bool
@@ -437,4 +435,5 @@ struct UserArgs {
pub mut:
name string @[required]
}
*
-

View File

@@ -1,14 +1,14 @@
# module ui.console.chalk
Chalk offers functions:- `console.color_fg(text string, color string)` - To change the foreground color.
- `console.color_bg(text string, color string)` - To change the background color.
- `console.style(text string, style string)` - To change the text style.
Example:
```vlang
import freeflowuniverse.crystallib.ui.console
import freeflowuniverse.herolib.ui.console
# basic usage
println('I am really ' + console.color_fg('happy', 'green'))
@@ -18,6 +18,7 @@ println('I am really ' + console.color_fg(console.style('ANGRY', 'bold'), 'red')
```
Available colors:- black
- red
- green
- yellow
@@ -36,6 +37,7 @@ Available colors:- black
- white
Available styles:- bold
- dim
- underline
- blink

View File

@@ -5,14 +5,13 @@ has mechanisms to print better to console, see the methods below
import as
```vlang
import freeflowuniverse.crystallib.ui.console
import freeflowuniverse.herolib.ui.console
```
## Methods
```v
````v
fn clear()
//reset the console screen
@@ -86,13 +85,12 @@ fn style(c Style) string
fn trim(c_ string) string
```
````
## Console Object
Is used to ask feedback to users
```v
struct UIConsole {
@@ -148,11 +146,8 @@ fn (mut c UIConsole) status() string
```
## enums
```v
enum BackgroundColor {
default_color = 49 // 'default' is a reserved keyword in V

View File

@@ -1,4 +1,3 @@
# how to run the vshell example scripts
this is how we want example scripts to be, see the first line
@@ -6,7 +5,7 @@ this is how we want example scripts to be, see the first line
```vlang
#!/usr/bin/env -S v -gc none -no-retry-compilation -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.crystallib.installers.sysadmintools.daguserver
import freeflowuniverse.herolib.installers.sysadmintools.daguserver
mut ds := daguserver.get()!
@@ -18,4 +17,3 @@ the files are in ~/code/github/freeflowuniverse/crystallib/examples for crystall
## important instructions
- never use fn main() in a .vsh script

View File

@@ -1,7 +1,6 @@
#!/usr/bin/env -S v -gc none -no-retry-compilation -cc tcc -d use_openssl -enable-globals run
import freeflowuniverse.crystallib.hero.bootstrap
import freeflowuniverse.herolib.hero.bootstrap
mut al := bootstrap.new_alpine_loader()

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env -S v -w -n -enable-globals run
import freeflowuniverse.crystallib.hero.generation
import freeflowuniverse.herolib.hero.generation
generation.generate_actor(
name: 'Example'

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env -S v -w -n -enable-globals run
import freeflowuniverse.crystallib.hero.generation
import freeflowuniverse.herolib.hero.generation
generation.generate_actor(
name: 'Example'

View File

@@ -1,8 +1,8 @@
module example_actor
import os
import freeflowuniverse.crystallib.hero.baobab.actor {IActor, RunParams}
import freeflowuniverse.crystallib.web.openapi
import freeflowuniverse.herolib.hero.baobab.actor { IActor, RunParams }
import freeflowuniverse.herolib.web.openapi
import time
const openapi_spec_path = '${os.dir(@FILE)}/specs/openapi.json'
@@ -14,9 +14,7 @@ struct ExampleActor {
}
fn new() !ExampleActor {
return ExampleActor{
actor.new('example')
}
return ExampleActor{actor.new('example')}
}
pub fn run() ! {
@@ -30,7 +28,7 @@ pub fn run_server(params RunParams) ! {
mut server := actor.new_server(
redis_url: 'localhost:6379'
redis_queue: a.name
openapi_spec: openapi_specification
openapi_spec: example_actor.openapi_specification
)!
server.run(params)
}

View File

@@ -6,9 +6,9 @@ import veb
import json
import x.json2
import net.http
import freeflowuniverse.crystallib.web.openapi {Server, Context, Request, Response}
import freeflowuniverse.crystallib.hero.processor {Processor, ProcedureCall, ProcedureResponse, ProcessParams}
import freeflowuniverse.crystallib.clients.redisclient
import freeflowuniverse.herolib.web.openapi
import freeflowuniverse.herolib.hero.processor
import freeflowuniverse.herolib.clients.redisclient
@[heap]
struct Actor {
@@ -108,35 +108,47 @@ fn (mut actor Actor) handle_method(cmd string, data string) !string {
return json.encode(response)
}
'deletePet' {
params := json.decode(map[string]int, data) or { 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'})
params := json.decode(map[string]int, data) or {
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'
})
}
'listOrders' {
orders := actor.data_store.list_orders()
return json.encode(orders)
}
'getOrder' {
params := json.decode(map[string]int, data) or { return error('Invalid params: $err') }
params := json.decode(map[string]int, data) or {
return error('Invalid params: ${err}')
}
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)
}
'deleteOrder' {
params := json.decode(map[string]int, data) or { return error('Invalid params: $err') }
actor.data_store.delete_order(params['orderId']) or {
return error('Order not found: $err')
params := json.decode(map[string]int, data) or {
return error('Invalid params: ${err}')
}
return json.encode({'message': 'Order deleted'})
actor.data_store.delete_order(params['orderId']) or {
return error('Order not found: ${err}')
}
return json.encode({
'message': 'Order deleted'
})
}
'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)
return json.encode(created_user)
}
else {
return error('Unknown method: $cmd')
return error('Unknown method: ${cmd}')
}
}
}
@@ -159,15 +171,17 @@ fn (mut store DataStore) list_pets(params ListPetParams) []Pet {
fn (mut store DataStore) create_pet(new_pet NewPet) Pet {
id := store.pets.keys().len + 1
pet := Pet{id: id, name: new_pet.name, tag: new_pet.tag}
pet := Pet{
id: id
name: new_pet.name
tag: new_pet.tag
}
store.pets[id] = pet
return pet
}
fn (mut store DataStore) get_pet(id int) !Pet {
return store.pets[id] or {
return error('Pet with id ${id} not found.')
}
return store.pets[id] or { return error('Pet with id ${id} not found.') }
}
fn (mut store DataStore) delete_pet(id int) ! {
@@ -196,7 +210,12 @@ fn (mut store DataStore) delete_order(id int) ! {
fn (mut store DataStore) create_user(new_user NewUser) User {
id := store.users.keys().len + 1
user := User{id: id, username: new_user.username, email: new_user.email, phone: new_user.phone}
user := User{
id: id
username: new_user.username
email: new_user.email
phone: new_user.phone
}
store.users[id] = user
return user
}

View File

@@ -6,10 +6,10 @@ import veb
import json
import x.json2 { Any }
import net.http
import freeflowuniverse.crystallib.data.jsonschema {Schema}
import freeflowuniverse.crystallib.web.openapi {Server, Context, Request, Response}
import freeflowuniverse.crystallib.hero.processor {Processor, ProcedureCall, ProcedureResponse, ProcessParams}
import freeflowuniverse.crystallib.clients.redisclient
import freeflowuniverse.herolib.data.jsonschema { Schema }
import freeflowuniverse.herolib.web.openapi { Context, Request, Response, Server }
import freeflowuniverse.herolib.hero.processor { ProcedureCall, ProcessParams, Processor }
import freeflowuniverse.herolib.clients.redisclient
const spec_path = '${os.dir(@FILE)}/data/openapi.json'
const spec_json = os.read_file(spec_path) or { panic(err) }
@@ -101,7 +101,7 @@ fn (mut handler Handler) handle(request Request) !Response {
}
} else {
// If the parameter is not defined in the OpenAPI operation, skip or log it
println('Unknown parameter: $param_name')
println('Unknown parameter: ${param_name}')
}
}
@@ -111,16 +111,13 @@ fn (mut handler Handler) handle(request Request) !Response {
call := ProcedureCall{
method: request.operation.operation_id
params: "[${params.join(',')}]" // Keep as a string since ProcedureCall expects a string
params: '[${params.join(',')}]' // Keep as a string since ProcedureCall expects a string
}
// Process the procedure call
procedure_response := handler.processor.process(
call,
ProcessParams{
procedure_response := handler.processor.process(call, ProcessParams{
timeout: 30 // Set timeout in seconds
}
) or {
}) or {
// Handle ProcedureError
if err is processor.ProcedureError {
return Response{
@@ -130,7 +127,7 @@ fn (mut handler Handler) handle(request Request) !Response {
})
}
}
return error('Unexpected error: $err')
return error('Unexpected error: ${err}')
}
// Convert returned procedure response to OpenAPI response