diff --git a/lib/core/code/function.v b/lib/core/code/function.v index 32a501cb..9c3f99f9 100644 --- a/lib/core/code/function.v +++ b/lib/core/code/function.v @@ -53,6 +53,7 @@ pub fn (function Function) vgen(options WriteOptions) string { '(${receiver_.vgen()})' } else {''} + name := texttools.name_fix(function.name) result := function.result.typ.vgen() mut function_str := $tmpl('templates/function/function.v.template') diff --git a/lib/core/code/struct.v b/lib/core/code/struct.v index 66c7e7ca..0aca3665 100644 --- a/lib/core/code/struct.v +++ b/lib/core/code/struct.v @@ -18,18 +18,23 @@ pub mut: // vgen_function generates a function statement for a function pub fn (struct_ Struct) vgen() string { - name := if struct_.generics.len > 0 { + name_ := if struct_.generics.len > 0 { '${struct_.name}${vgen_generics(struct_.generics)}' } else { struct_.name } + name := texttools.pascal_case(name_) prefix := if struct_.is_pub { - 'pub' + 'pub ' } 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()) 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()) diff --git a/lib/core/code/templates/function/function.v.template b/lib/core/code/templates/function/function.v.template index 50c44706..c1b87189 100644 --- a/lib/core/code/templates/function/function.v.template +++ b/lib/core/code/templates/function/function.v.template @@ -1,6 +1,6 @@ @if function.description != '' // @{function.description} @endif -pub fn @receiver @{function.name}(@{params}) @{result} { +pub fn @receiver @{name}(@{params}) @{result} { @{function.body.trim_space().replace('\t', '')} } \ No newline at end of file diff --git a/lib/core/code/templates/struct/struct.v.template b/lib/core/code/templates/struct/struct.v.template index 74fefa75..9ea031f9 100644 --- a/lib/core/code/templates/struct/struct.v.template +++ b/lib/core/code/templates/struct/struct.v.template @@ -1,4 +1,4 @@ -// @{struct_.description} +@{comments} @if struct_.attrs.len > 0 [ @for attr in struct_.attrs @@ -6,7 +6,7 @@ @end ] @end -@{prefix} struct @{name} { +@{prefix}struct @{name} { @for embed in struct_.embeds @{embed.get_type_symbol()} @end diff --git a/lib/schemas/jsonschema/codegen/codegen.v b/lib/schemas/jsonschema/codegen/codegen.v index 2fc388f0..411dd354 100644 --- a/lib/schemas/jsonschema/codegen/codegen.v +++ b/lib/schemas/jsonschema/codegen/codegen.v @@ -162,6 +162,7 @@ pub fn schema_to_struct(schema Schema) Struct { name: schema.title description: schema.description fields: fields + is_pub: true } } diff --git a/lib/schemas/openrpc/codegen/to_code.v b/lib/schemas/openrpc/codegen/to_code.v index c75ed05c..8139c107 100644 --- a/lib/schemas/openrpc/codegen/to_code.v +++ b/lib/schemas/openrpc/codegen/to_code.v @@ -1,7 +1,8 @@ module codegen 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.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 { return code.Param{ name: cd.name