Skip to content

Commit

Permalink
Fix CDM types (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
ozangoktan authored Sep 6, 2024
1 parent 1bbdc1b commit 2c5d877
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CogniteSdk.Types/Alpha/Common/Identity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public override string ToString()
}
else
{
return $"{{{InstanceId}}}";
return InstanceId.ToString();
}

}
Expand Down
60 changes: 48 additions & 12 deletions CogniteSdk.Types/Beta/DataModels/Core/Timeseries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Text.Json;

namespace CogniteSdk.Beta.DataModels.Core
{
Expand All @@ -15,13 +17,10 @@ public class CogniteTimeSeriesBase : CogniteCoreInstanceBase
/// Defines whether the time series is a step series or not.
/// </summary>
public bool? IsStep { get; set; }

private readonly TimeSeriesType? _type;
/// <summary>
/// Type of datapoints the time series contains.
/// </summary>
public string Type { get { return _type == null ? null : Enum.GetName(typeof(TimeSeriesType), _type).ToLower(); } }

public TimeSeriesType? Type { get; set; }
/// <summary>
/// The physical unit of the time series as described in the source.
/// </summary>
Expand All @@ -46,18 +45,11 @@ public class CogniteTimeSeriesBase : CogniteCoreInstanceBase


/// <summary>
/// Empty constructor. For partial updates only.
/// Empty constructor.
/// </summary>
public CogniteTimeSeriesBase()
{
}
/// <summary>
/// Constructor.
/// </summary>
public CogniteTimeSeriesBase(TimeSeriesType type)
{
_type = type;
}
}

/// <summary>
Expand All @@ -74,4 +66,48 @@ public enum TimeSeriesType
/// </summary>
Numeric,
}

/// <summary>
/// Converts string to TimeSeriesType
/// </summary>
public class ObjectToTimeSeriesTypeConverter : JsonConverter<TimeSeriesType?>
{
/// <summary>
/// Reads string into an TimeSeriesType
/// </summary>
public override TimeSeriesType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException($"JsonTokenType was of type {reader.TokenType}, must be a string");
}

var typeVal = reader.GetString().ToLower();

switch (typeVal)
{
case "numeric":
return TimeSeriesType.Numeric;
case "string":
return TimeSeriesType.String;
default:
throw new ArgumentOutOfRangeException(nameof(CogniteTimeSeriesBase.Type), "TimeSeries type can either be numeric or string");
}
}

/// <summary>
/// Writes a TimeSeriesType to string.
/// </summary>
public override void Write(Utf8JsonWriter writer, TimeSeriesType? value, JsonSerializerOptions options)
{
if (value == null)
writer.WriteNullValue();
else
writer.WriteStringValue(Enum.GetName(typeof(TimeSeriesType), value).ToLower());
}
}
}
81 changes: 81 additions & 0 deletions CogniteSdk.Types/Common/Converters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Collections.Generic;
using CogniteSdk.Beta.DataModels;

namespace CogniteSdk
{
Expand Down Expand Up @@ -32,6 +33,8 @@ public override MultiValue Read(ref Utf8JsonReader reader, Type typeToConvert, J
return MultiValue.Create(reader.GetDouble());
case JsonTokenType.Null:
return MultiValue.Create();
case JsonTokenType.StartObject:
return MultiValue.Create(new ObjectToInstanceIdJsonConverter().Read(ref reader, typeToConvert, options));
default:
throw new JsonException($"Unable to parse value of type: {reader.TokenType}");
}
Expand Down Expand Up @@ -67,6 +70,84 @@ public override void Write(Utf8JsonWriter writer, MultiValue value, JsonSerializ
}
}

/// <summary>
/// Converts JSON to .NET type
/// </summary>
public class ObjectToInstanceIdJsonConverter : JsonConverter<InstanceIdentifier>
{
/// <summary>
/// Reads JSON into an InstanceIdentifier
/// </summary>
public override InstanceIdentifier Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException($"JsonTokenType was of type {reader.TokenType}, must be StartObject");
}

var instanceId = new InstanceIdentifier();
var keys = new Dictionary<string, Func<InstanceIdentifier, string, InstanceIdentifier>>() {
{ nameof(InstanceIdentifier.Space).ToLower(), (x, y) => {x.Space = y; return x; } },
{ nameof(InstanceIdentifier.ExternalId).ToLower(), (x, y) => {x.ExternalId = y; return x; } }
};
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
return instanceId;
}

if (reader.TokenType != JsonTokenType.PropertyName)
{
throw new JsonException($"JsonTokenType must be of type PropertyName, was {reader.TokenType}");
}

var propertyName = reader.GetString();

if (string.IsNullOrWhiteSpace(propertyName))
{
throw new JsonException("Failed to get property name");
}

reader.Read();

if (keys.TryGetValue(propertyName.ToLower(), out var method))
{
method(instanceId, ReadValue(ref reader, options));
}
}

return instanceId;
}

/// <summary>
/// Writes an InstanceIdentifier to string.
/// </summary>
public override void Write(Utf8JsonWriter writer, InstanceIdentifier value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}

private string ReadValue(ref Utf8JsonReader reader, JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.String:
return reader.GetString();
case JsonTokenType.Null:
return null;
case JsonTokenType.Number:
if (reader.TryGetInt64(out var number))
{
return number.ToString();
}
return reader.GetDouble().ToString();
default:
throw new JsonException($"'{reader.TokenType}' is not supported");
}
}
}

/// <summary>
/// Converts JSON to .NET type
/// </summary>
Expand Down
35 changes: 33 additions & 2 deletions CogniteSdk/test/fsharp/Alpha/DataPoints.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ open CogniteSdk.Beta.DataModels.Core
let testSpace = "dotnet-sdk-integration-test-space"

module Fixtures =
open System.Text.Json.Nodes

type DMFixture() =
do
writeClient.Beta.DataModels
Expand All @@ -25,6 +27,35 @@ module Fixtures =

interface IDisposable with
member __.Dispose() =
let nodes =
writeClient.Beta.DataModels
.FilterInstances<JsonNode>(
InstancesFilter(
Filter =
EqualsFilter(
Property = [ "node"; "space" ],
Value = RawPropertyValue(Value = testSpace)
),
Limit = 1000,
InstanceType = InstanceType.node
)
)
.GetAwaiter()
.GetResult()

if not <| Seq.isEmpty (nodes.Items) then
writeClient.Beta.DataModels
.DeleteInstances(
nodes.Items
|> Seq.map (fun x ->
let item = new InstanceIdentifierWithType(InstanceType.node, x.Space, x.ExternalId)

item)
)
.GetAwaiter()
.GetResult()
|> ignore

writeClient.Beta.DataModels.DeleteSpaces([ testSpace ]).GetAwaiter().GetResult()
|> ignore

Expand All @@ -44,7 +75,7 @@ module DataPointsTests =
ExternalId = externalIdString,
Properties =
CogniteTimeSeriesBase(

Check warning on line 77 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / Build Release Package

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.

Check warning on line 77 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / Build Release Package

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.

Check warning on line 77 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / build

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.

Check warning on line 77 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / build

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.

Check warning on line 77 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / build

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.
TimeSeriesType.Numeric,
Type = TimeSeriesType.Numeric,
Name = "Insert datapoints test",
Description = "dotnet sdk test"
)
Expand Down Expand Up @@ -92,7 +123,7 @@ module DataPointsTests =
ExternalId = externalIdString,
Properties =
CogniteTimeSeriesBase(

Check warning on line 125 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / Build Release Package

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.

Check warning on line 125 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / Build Release Package

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.

Check warning on line 125 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / build

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.

Check warning on line 125 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / build

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.

Check warning on line 125 in CogniteSdk/test/fsharp/Alpha/DataPoints.fs

View workflow job for this annotation

GitHub Actions / build

This construct causes code to be less generic than indicated by the type annotations. The type variable 'CogniteTimeSeriesBase has been constrained to be type 'CogniteTimeSeriesBase'.
TimeSeriesType.Numeric,
Type = TimeSeriesType.Numeric,
Name = "Delete datapoints test",
Description = "dotnet sdk test"
)
Expand Down
2 changes: 2 additions & 0 deletions Oryx.Cognite/src/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ open CogniteSdk
open CogniteSdk.Alpha
open CogniteSdk.Beta
open CogniteSdk.Beta.DataModels
open CogniteSdk.Beta.DataModels.Core

open System.Text.Json.Serialization

Expand Down Expand Up @@ -71,6 +72,7 @@ module Common =
options.DefaultIgnoreCondition <- JsonIgnoreCondition.WhenWritingNull

options.Converters.Add(MultiValueConverter())
options.Converters.Add(ObjectToTimeSeriesTypeConverter())
options.Converters.Add(ObjectToDictionaryJsonConverter())
options.Converters.Add(AclConverter())
options.Converters.Add(TransformationSchemaConverter())
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.5.0
4.6.0

0 comments on commit 2c5d877

Please sign in to comment.