I just created my JSON Schema and I can't find if there is some kind of unsigned integer.
Is there an unsigned integer type available in JSON Schema?
No, there isn't. Check the 'type' section for details. But you can do
{
"type" : "integer",
"minimum" : 0
}
Related
I'm writing a communication protocol schema for a list of parameters which can be of multiple values: uint64, float64, string or bool.
How can I set a table field to a union of multiple primitive scalar & non-scalar primitive type?
I've already tried using a union of those types, but I end up with the following error when building:
$ schemas/foobar.fbs:28: 0: error: type referenced but not defined
(check namespace): uint64, originally at: schemas/request.fbs:5
Here's the schema in its current state:
namespace Foobar;
enum RequestCode : uint16 { Noop, Get, Set, BulkGet, BulkSet }
union ParameterValue { uint64, float64, bool, string }
table Parameter {
name:string;
value:ParameterValue;
unit:string;
}
table Request {
code:RequestCode = Noop;
payload:[Parameter];
}
table Result {
request:Request;
success:bool = true;
payload:[Parameter];
}
The end result I'm looking for is the Request and Result tables to contain a list of parameters, where a parameter contains a name and value, and optionally the units.
Thx in advance!
Post-answer solution:
Here's what I came up with in the end, thx to Aardappel.
namespace foobar;
enum RequestCode : uint16 { Noop, Get, Set, BulkGet, BulkSet }
union ValueType { UnsignedInteger, SignedInteger, RealNumber, Boolean, Text }
table UnsignedInteger {
value:uint64 = 0;
}
table SignedInteger {
value:int64 = 0;
}
table RealNumber {
value:float64 = 0.0;
}
table Boolean {
value:bool = false;
}
table Text {
value:string (required);
}
table Parameter {
name:string (required);
valueType:ValueType;
unit:string;
}
table Request {
code:RequestCode = Noop;
payload:[Parameter];
}
table Result {
request:Request (required);
success:bool = true;
payload:[Parameter];
}
You currently can't put scalars directly in a union, so you'd have to wrap these in a table or a struct, where struct would likely be the most efficient, e.g.
struct UInt64 { u:uint64 }
union ParameterValue { UInt64, Float64, Bool, string }
This is because a union must be uniformly the same size, so it only allows types to which you can have an offset.
Generally though, FlatBuffers is a strongly typed system, and the schema you are creating here is undoing that by emulating dynamically typed data, since your data is essentially a list of (string, any type) pairs. You may be better off with a system designed for this particular use case, such as FlexBuffers (https://google.github.io/flatbuffers/flexbuffers.html, currently only C++) which explicitly has a map type that is all string -> any type pairs.
Of course, even better is to not store data so generically, but instead make a new schema for each type of request and response you have, and make parameter names into fields, rather than serialized data. This is by far the most efficient, and type safe.
I have tried using Int and Integer types in Kotlin.Although I don't see any difference.
Is there a difference between Int and Integer types in Kotlin?Or are they the same?
Int is a Kotlin Class derived from Number. See doc
[Int] Represents a 32-bit signed integer. On the JVM, non-nullable values of
this type are represented as values of the primitive type int.
Integer is a Java Class.
If you were to search the Kotlin spec for "Integer", there is no Kotlin Integer type.
If you use the expression is Integer in IntelliJ, the IDE warns...
This type shouldn't be used in Kotlin, use Int instead.
Integer will interoperate well with Kotlin Int, but they do have some subtle differences in behavior, for example:
val one: Integer = 1 // error: "The integer literal does not conform to the expected type Integer"
val two: Integer = Integer(2) // compiles
val three: Int = Int(3) // does not compile
val four: Int = 4 // compiles
In Java, there are times where you need to explicitly "box" an integer as an object. In Kotlin only Nullable integers (Int?) are boxed. Explicitly trying to box a non-nullable integer will give a compiler error:
val three: Int = Int(3) // error: "Cannot access '<init>': it is private in 'Int'
val four: Any = 4 // implicit boxing compiles (or is it really boxed?)
But Int and Integer (java.lang.Integer) will be treated the same most of the time.
when(four) {
is Int -> println("is Int")
is Integer -> println("is Integer")
else -> println("is other")
} //prints "is Int"
when(four) {
is Integer -> println("is Integer")
is Int -> println("is Int")
else -> println("is other")
} //prints "is Integer"
A Int is a primitive Type. This is equivalent to JVM int.
A nullable Int Int? is a boxed Type. This is equivalent to java.lang.Integer.
Just have a look at https://kotlinlang.org/docs/reference/basic-types.html#representation
On the Java platform, numbers are physically stored as JVM primitive
types, unless we need a nullable number reference (e.g. Int?) or
generics are involved. In the latter cases numbers are boxed.
That means, that Int is represented as primitive int. Only in cases when nullability or generics are involved, the backing Wrapper Type Integer must be used.
If you use Integer from the beginning, you always work with the wrapper type and never with the primitive int.
I am using swagger-codegen to generate client SDK in Objective C language. One of my APIs is taking boolean parameter. Parameter is defined in Swagger Specification as follows:
"parameters": [
{
"name": "recursive",
"in": "query",
"description": "",
"required": false,
"type": "boolean"
}
]
The generated client SDK is checking if BOOL variabe is nil. BOOL is a primitive type in Objective C, cannot be checked against nil. It is causing compilation error.
-(NSNumber*) folderDeleteFolderWithCompletionBlock: (NSString*) path recursive: (BOOL) recursive {
...
if(recursive != nil)
{
queryParams[#"recursive"] = recursive;
}
...
}
If we use NSNumber + (NSNumber *)numberWithBool:(BOOL)value in place of BOOL, this issue can be fixed. How can I map boolean in swagger specification to NSNumber in client SDK?
I made few changes in ObjcClientCodegen.java like replace following line
typeMapping.put("boolean", "BOOL");
with
typeMapping.put("boolean", "NSNumber");
Even I remove BOOL from defaultIncludes and languageSpecificPrimitives HashSets
defaultIncludes = new HashSet<String>(
Arrays.asList(
/*"bool",
"BOOL",*/
"int",
"NSString",
"NSObject",
"NSArray",
"NSNumber",
"NSDate",
"NSDictionary",
"NSMutableArray",
"NSMutableDictionary")
);
languageSpecificPrimitives = new HashSet<String>(
Arrays.asList(
"NSNumber",
"NSString",
"NSObject",
"NSDate"/*,
"bool",
"BOOL"*/)
);
In-spite this generated client SDK is using BOOL data type instead of NSNumber.
Any help in this regard will be highly appreciated.
The latest version of Swagger-Codegen has this issue addressed.
How can I map a single char column in Doctrine 2, using annotations? I would like to have a char type, instead a single char string.
You can always use the string type with the fixed option:
/**
* #Column(type="string", length=2, options={"fixed" = true})
*/
protected $country;
The above code snippet produces the following SQL:
`country` char(2) NOT NULL,
Doctrine doesn't have a CHAR type defined out of the box, however it does allow you to define custom types, which you could use to create a 'char' type to use in annotations.
The Doctrine documentation has an example of this: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html#custom-mapping-types
You might end up providing your own full-column definition:
/**
* #Column(type="string", columnDefinition="CHAR(2) NOT NULL")
*/
protected $country = null;
I have a domain entity class with a property:
public virtual double? Result { get; set; }
The property is being mapped using the NHibernate 3.2 mapping-by-code stuff:
public class SampleResultMap : ClassMapping<SampleResult>
{
public SampleResultMap()
{
Id(c => c.Id,
map => map.Generator(Generators.Identity));
Property(c => c.Result, map =>
{
map.NotNullable(false);
});
// More properties, etc.
}
}
This works fine and the SQL Server 2008 R2 table is created properly with a data type of float.
However, the SchemaValidator.Validate call gives this error:
NHibernate.HibernateException was unhandled
Wrong column type in Foo.dbo.SampleResult for column Result.
Found: float, Expected DOUBLE PRECISION
Looking at the SQL that the call to SchemaExport.Create generates there is this definition for the table:
create table SampleResult (
Id INT IDENTITY NOT NULL,
DateEnteredUtc DATETIME not null,
ElementId INT not null,
Unit INT not null,
ResultText NVARCHAR(50) null,
[Result] DOUBLE PRECISION null,
Detected BIT not null,
Qualifier NVARCHAR(10) null,
SampleId INT not null,
Deleted BIT not null,
primary key (Id)
)
From a quick reading of the NHibernate 3.2 sources it appears that the validator is comparing “DOUBLE PRECISION” to “float”.
Has anyone else seen this? I assume it is a bug but I haven't used the validator before so wanted to find out if I’m doing something wrong.
I had a similar issue with natively generated IDs on a SQLite DB which was caused because SQLite only supports auto increment on integer columns.
NHibernate had correctly created the ID column as an integer but when it was validating it, it thought it should be an int instead of an integer. Because int just maps to integer on SQLite I just created a new dialect to use integer instead of int and it now works fine.
As DOUBLE PRECISION is the same as FLOAT(53) on SQLServer it might be the same thing, it's seeing float instead of double precision. As a work around you could try changing the dialect to use FLOAT(53) instead:
using System.Data;
using NHibernate.Dialect;
public class UpdatedMsSql2008Dialect : MsSql2008Dialect
{
public UpdatedMsSql2008Dialect()
: base()
{
RegisterColumnType(DbType.Double, "FLOAT(53)");
}
}
The SQLServer dialect source seems to suggest it should work. Definitely looks like there is a bug with the validator though.