in codegen/src/Azure.Iot.Operations.ProtocolCompiler/TypeGenerator/JsonSchemaStandardizer.cs [137:197]
private SchemaType GetPrimitiveTypeFromJsonElement(JsonElement rootElt, JsonElement schemaElt, string? internalDefsKey, string schemaFilePath, CodeName genNamespace)
{
switch (schemaElt.GetProperty("type").GetString())
{
case "array":
return new ArrayType(GetSchemaTypeFromJsonElement(rootElt, schemaElt.GetProperty("items"), internalDefsKey, schemaFilePath, genNamespace));
case "object":
return new MapType(GetSchemaTypeFromJsonElement(rootElt, schemaElt.GetProperty("additionalProperties"), internalDefsKey, schemaFilePath, genNamespace));
case "boolean":
return new BooleanType();
case "number":
return schemaElt.GetProperty("format").GetString() == "float" ? new FloatType() : new DoubleType();
case "integer":
return schemaElt.GetProperty("maximum").GetUInt64() switch
{
< 128 => new ByteType(),
< 256 => new UnsignedByteType(),
< 32768 => new ShortType(),
< 65536 => new UnsignedShortType(),
< 2147483648 => new IntegerType(),
< 4294967296 => new UnsignedIntegerType(),
< 9223372036854775808 => new LongType(),
_ => new UnsignedLongType(),
};
case "string":
if (schemaElt.TryGetProperty("format", out JsonElement formatElt))
{
return formatElt.GetString() switch
{
"date" => new DateType(),
"date-time" => new DateTimeType(),
"time" => new TimeType(),
"duration" => new DurationType(),
"uuid" => new UuidType(),
_ => throw new Exception($"unrecognized 'string' schema (format = {formatElt.GetString()})"),
};
}
if (schemaElt.TryGetProperty("contentEncoding", out JsonElement encodingElt))
{
return encodingElt.GetString() switch
{
"base64" => new BytesType(),
_ => throw new Exception($"unrecognized 'string' schema (contentEncoding = {encodingElt.GetString()})"),
};
}
if (schemaElt.TryGetProperty("pattern", out JsonElement patternElt))
{
return patternElt.GetString() switch
{
"^(?:\\+|-)?(?:[1-9][0-9]*|0)(?:\\.[0-9]*)?$" => new DecimalType(),
_ => throw new Exception($"unrecognized 'string' schema (pattern = {patternElt.GetString()})"),
};
}
return new StringType();
default:
throw new Exception($"unrecognized schema (type = {schemaElt.GetProperty("type").GetString()})");
}
}