generic code generation improvements

This commit is contained in:
timurgordon
2025-02-13 02:31:59 +03:00
parent f0c23eb4ae
commit 776942cd8b
6 changed files with 26 additions and 6 deletions

View File

@@ -53,6 +53,7 @@ pub fn (function Function) vgen(options WriteOptions) string {
'(${receiver_.vgen()})' '(${receiver_.vgen()})'
} else {''} } else {''}
name := texttools.name_fix(function.name)
result := function.result.typ.vgen() result := function.result.typ.vgen()
mut function_str := $tmpl('templates/function/function.v.template') mut function_str := $tmpl('templates/function/function.v.template')

View File

@@ -18,18 +18,23 @@ pub mut:
// vgen_function generates a function statement for a function // vgen_function generates a function statement for a function
pub fn (struct_ Struct) vgen() string { pub fn (struct_ Struct) vgen() string {
name := if struct_.generics.len > 0 { name_ := if struct_.generics.len > 0 {
'${struct_.name}${vgen_generics(struct_.generics)}' '${struct_.name}${vgen_generics(struct_.generics)}'
} else { } else {
struct_.name struct_.name
} }
name := texttools.pascal_case(name_)
prefix := if struct_.is_pub { prefix := if struct_.is_pub {
'pub' 'pub '
} else { } else {
'' ''
} }
comments := if struct_.description.trim_space() != '' {
'// ${struct_.description.trim_space()}'
} else {''}
priv_fields := struct_.fields.filter(!it.is_mut && !it.is_pub).map(it.vgen()) priv_fields := struct_.fields.filter(!it.is_mut && !it.is_pub).map(it.vgen())
pub_fields := struct_.fields.filter(!it.is_mut && it.is_pub).map(it.vgen()) pub_fields := struct_.fields.filter(!it.is_mut && it.is_pub).map(it.vgen())
mut_fields := struct_.fields.filter(it.is_mut && !it.is_pub).map(it.vgen()) mut_fields := struct_.fields.filter(it.is_mut && !it.is_pub).map(it.vgen())

View File

@@ -1,6 +1,6 @@
@if function.description != '' @if function.description != ''
// @{function.description} // @{function.description}
@endif @endif
pub fn @receiver @{function.name}(@{params}) @{result} { pub fn @receiver @{name}(@{params}) @{result} {
@{function.body.trim_space().replace('\t', '')} @{function.body.trim_space().replace('\t', '')}
} }

View File

@@ -1,4 +1,4 @@
// @{struct_.description} @{comments}
@if struct_.attrs.len > 0 @if struct_.attrs.len > 0
[ [
@for attr in struct_.attrs @for attr in struct_.attrs
@@ -6,7 +6,7 @@
@end @end
] ]
@end @end
@{prefix} struct @{name} { @{prefix}struct @{name} {
@for embed in struct_.embeds @for embed in struct_.embeds
@{embed.get_type_symbol()} @{embed.get_type_symbol()}
@end @end

View File

@@ -162,6 +162,7 @@ pub fn schema_to_struct(schema Schema) Struct {
name: schema.title name: schema.title
description: schema.description description: schema.description
fields: fields fields: fields
is_pub: true
} }
} }

View File

@@ -1,7 +1,8 @@
module codegen module codegen
import freeflowuniverse.herolib.core.code { VFile, CodeItem, CustomCode, Function, Struct, parse_function } import freeflowuniverse.herolib.core.code { VFile, CodeItem, CustomCode, Function, Struct, parse_function }
import freeflowuniverse.herolib.schemas.jsonschema.codegen as jsonschema_codegen {schemaref_to_type} import freeflowuniverse.herolib.schemas.jsonschema.codegen as jsonschema_codegen {schemaref_to_type, schema_to_struct}
import freeflowuniverse.herolib.schemas.jsonschema {Schema}
import freeflowuniverse.herolib.schemas.openrpc {Method, ContentDescriptor} import freeflowuniverse.herolib.schemas.openrpc {Method, ContentDescriptor}
import freeflowuniverse.herolib.core.texttools import freeflowuniverse.herolib.core.texttools
@@ -26,6 +27,18 @@ pub fn method_to_function(method Method) !Function {
} }
} }
pub fn content_descriptor_to_struct(cd ContentDescriptor) Struct {
if cd.schema is Schema {
mut struct_ := schema_to_struct(cd.schema)
if struct_.name == '' || struct_.name == 'Unknown' {
struct_.name = cd.name
}
return struct_
} else {
panic('Struct code can be generated only from content descriptor with non-reference schema')
}
}
pub fn content_descriptor_to_parameter(cd ContentDescriptor) !code.Param { pub fn content_descriptor_to_parameter(cd ContentDescriptor) !code.Param {
return code.Param{ return code.Param{
name: cd.name name: cd.name