better support for parsing generating numeric types

This commit is contained in:
timurgordon
2025-01-21 02:30:38 +00:00
parent 8b9717bb74
commit eef88b5375
5 changed files with 205 additions and 48 deletions

View File

@@ -28,51 +28,6 @@ pub mut:
struct_ Struct @[omitempty]
}
pub type Type = Array | Object | Result | Integer | Alias | String
pub struct Integer {
bytes u8
}
pub fn type_from_symbol(symbol_ string) Type {
mut symbol := symbol_.trim_space()
if symbol.starts_with('[]') {
return Array{type_from_symbol(symbol.all_after('[]'))}
} else if symbol == 'int' {
return Integer{}
} else if symbol == 'string' {
return String{}
}
return Object{symbol}
}
pub fn (t Type) symbol() string {
return match t {
Array { '[]${t.typ.symbol()}' }
Object { t.name }
Result { '!${t.typ.symbol()}'}
Integer {'int'}
Alias {t.name}
String {'string'}
}
}
pub struct String {}
pub struct Array {
pub:
typ Type
}
pub struct Object {
pub:
name string
}
pub struct Result {
pub:
typ Type
}
// // todo: maybe make 'is_' fields methods?
// pub struct Type {

109
lib/core/code/types.v Normal file
View File

@@ -0,0 +1,109 @@
module code
struct Float {
bytes int
}
// Integer types
const type_i8 = Integer{
bytes: 8
}
const type_u8 = Integer{
bytes: 8
signed: false
}
const type_i16 = Integer{
bytes: 16
}
const type_u16 = Integer{
bytes: 16
signed: false
}
const type_i32 = Integer{
bytes: 32
}
const type_u32 = Integer{
bytes: 32
signed: false
}
const type_i64 = Integer{
bytes: 64
}
const type_u64 = Integer{
bytes: 64
signed: false
}
// Floating-point types
const type_f32 = Float{
bytes: 32
}
const type_f64 = Float{
bytes: 64
}
pub type Type = Array | Object | Result | Integer | Alias | String
pub struct Integer {
bytes u8
signed bool = true
}
pub fn type_from_symbol(symbol_ string) Type {
mut symbol := symbol_.trim_space()
if symbol.starts_with('[]') {
return Array{type_from_symbol(symbol.all_after('[]'))}
} else if symbol == 'int' {
return Integer{}
} else if symbol == 'string' {
return String{}
}
return Object{symbol}
}
pub fn (t Type) symbol() string {
return match t {
Array { '[]${t.typ.symbol()}' }
Object { t.name }
Result { '!${t.typ.symbol()}'}
Integer {
mut str := ''
if !t.signed {
str += 'u'
}
if t.bytes != 0 {
'${str}${t.bytes}'
} else {
'${str}int'
}
}
Alias {t.name}
String {'string'}
}
}
pub struct String {}
pub struct Array {
pub:
typ Type
}
pub struct Object {
pub:
name string
}
pub struct Result {
pub:
typ Type
}

View File

@@ -6,6 +6,8 @@ import freeflowuniverse.herolib.schemas.jsonschema { Schema, SchemaRef, Referenc
const vtypes = {
'integer': 'int'
'string': 'string'
'u32': 'u32'
'boolean': 'bool'
}
pub fn schema_to_v(schema Schema) !string {
@@ -75,7 +77,20 @@ pub fn schema_to_type(schema Schema) !Type {
typ: schemaref_to_type(schema.items as SchemaRef)!
}
} else {
if schema.typ in vtypes.keys() {
if schema.typ == 'integer' && schema.format != '' {
match schema.format {
'int8' { code.type_i8 }
'uint8' { code.type_u8 }
'int16' { code.type_i16 }
'uint16' { code.type_u16 }
'int32' { code.type_i32 }
'uint32' { code.type_u32 }
'int64' { code.type_i64 }
'uint64' { code.type_u64 }
else { code.Integer{} } // Default to 'int' if the format doesn't match any known type
}
}
else if schema.typ in vtypes.keys() {
type_from_symbol(vtypes[schema.typ])
} else if schema.title != '' {
type_from_symbol(schema.title)

View File

@@ -0,0 +1,77 @@
module jsonschema
// Define numeric schemas
const schema_u8 = Schema{
typ: "integer"
format: 'uint8'
minimum: 0
maximum: 255
description: "An unsigned 8-bit integer."
}
const schema_i8 = Schema{
typ: "integer"
format: 'int8'
minimum: -128
maximum: 127
description: "A signed 8-bit integer."
}
const schema_u16 = Schema{
typ: "integer"
format: 'uint16'
minimum: 0
maximum: 65535
description: "An unsigned 16-bit integer."
}
const schema_i16 = Schema{
typ: "integer"
format: 'int16'
minimum: -32768
maximum: 32767
description: "A signed 16-bit integer."
}
const schema_u32 = Schema{
typ: "integer"
format: 'uint32'
minimum: 0
maximum: 4294967295
description: "An unsigned 32-bit integer."
}
const schema_i32 = Schema{
typ: "integer"
format: 'int32'
minimum: -2147483648
maximum: 2147483647
description: "A signed 32-bit integer."
}
const schema_u64 = Schema{
typ: "integer"
format: 'uint64'
minimum: 0
maximum: 18446744073709551615
description: "An unsigned 64-bit integer."
}
const schema_i64 = Schema{
typ: "integer"
format: 'int64'
minimum: -9223372036854775808
maximum: 9223372036854775807
description: "A signed 64-bit integer."
}
const schema_f32 = Schema{
typ: "number"
description: "A 32-bit floating-point number."
}
const schema_f64 = Schema{
typ: "number"
description: "A 64-bit floating-point number."
}

View File

@@ -1,5 +1,6 @@
module openapi
import x.json2 as json {Any}
import freeflowuniverse.herolib.schemas.jsonschema {Schema, Reference, SchemaRef}
// todo: report bug: when comps is optional, doesnt work
@@ -83,7 +84,7 @@ pub:
// }```
pub struct ServerSpec {
pub:
url string @[required] // A URL to the target host. This URL supports ServerSpec Variables and MAY be relative, to indicate that the host location is relative to the location where the OpenAPI document is being served. Variable substitutions will be made when a variable is named in {brackets}.
url string @[required] // A URL to the target host. This URL supports ServerSpec Variables and MAY be relative, to indicate that the host location is relative to the location where the OpenAPI document is being served. Variable substitutions will be made when a variable is named in {brackets}.
description string // An optional string describing the host designated by the URL. CommonMark syntax MAY be used for rich text representation.
variables map[string]ServerVariable // A map between a variable name and its value. The value is used for substitution in the servers URL template.
}
@@ -208,7 +209,7 @@ pub mut:
pub struct MediaType {
pub mut:
schema SchemaRef // The schema defining the content of the request, response, or parameter.
example string // Example of the media type. The example object SHOULD be in the correct format as specified by the media type. The example field is mutually exclusive of the examples field. Furthermore, if referencing a schema which contains an example, the example value SHALL override the example provided by the schema.
example Any @[json: '-']// Example of the media type. The example object SHOULD be in the correct format as specified by the media type. The example field is mutually exclusive of the examples field. Furthermore, if referencing a schema which contains an example, the example value SHALL override the example provided by the schema.
examples map[string]ExampleRef // Examples of the media type. Each example object SHOULD match the media type and specified schema if present. The examples field is mutually exclusive of the example field. Furthermore, if referencing a schema which contains an example, the examples value SHALL override the example provided by the schema.
encoding map[string]Encoding // A map between a property name and its encoding information. The key, being the property name, MUST exist in the schema as a property. The encoding object SHALL only apply to requestBody objects when the media type is multipart or application/x-www-form-urlencoded.
}