-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Additional properties): Added DynamicModel base class for models…
… with AdditionalProperties
- Loading branch information
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright 2019 IBM Corp. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace IBM.Cloud.SDK.Core.Model | ||
{ | ||
/// <summary> | ||
/// This class is the base class for generated models with additional properties. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class DynamicModel<T> | ||
{ | ||
/// <summary> | ||
/// A Dictionary to keep additional properties. | ||
/// </summary> | ||
public Dictionary<string, T> AdditionalProperties { get; } = new Dictionary<string, T>(); | ||
|
||
/// <summary> | ||
/// Add a property to the AdditionalProperties dictionary. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <param name="value"></param> | ||
public void Add(string key, T value) | ||
{ | ||
AdditionalProperties.Add(key, value); | ||
} | ||
|
||
/// <summary> | ||
/// Remove a property from the AdditionalProperties dictionary. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
public void Remove(string key) | ||
{ | ||
AdditionalProperties.Remove(key); | ||
} | ||
|
||
/// <summary> | ||
/// Get a single property from the AdditionalProperties dictionary. | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <returns></returns> | ||
public T Get(string key) | ||
{ | ||
AdditionalProperties.TryGetValue(key, out T value); | ||
return value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright 2019 IBM Corp. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
using IBM.Cloud.SDK.Core.Model; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Collections.Generic; | ||
|
||
namespace IBM.Cloud.SDK.Core.Tests | ||
{ | ||
[TestClass] | ||
public class DynamicModelTests | ||
{ | ||
[TestMethod] | ||
public void TestAdd() | ||
{ | ||
DynamicModel<object> dynamicModel = new DynamicModel<object>(); | ||
object myObject = new object(); | ||
dynamicModel.Add("myObject", myObject); | ||
|
||
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsKey("myObject")); | ||
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsValue(myObject)); | ||
} | ||
|
||
[TestMethod] | ||
public void TestRemove() | ||
{ | ||
DynamicModel<object> dynamicModel = new DynamicModel<object>(); | ||
object myObject = new object(); | ||
dynamicModel.Add("myObject", myObject); | ||
|
||
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsKey("myObject")); | ||
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsValue(myObject)); | ||
|
||
dynamicModel.Remove("myObject"); | ||
Assert.IsFalse(dynamicModel.AdditionalProperties.ContainsKey("myObject")); | ||
Assert.IsFalse(dynamicModel.AdditionalProperties.ContainsValue(myObject)); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGet() | ||
{ | ||
DynamicModel<object> dynamicModel = new DynamicModel<object>(); | ||
object myObject = new object(); | ||
dynamicModel.Add("myObject", myObject); | ||
|
||
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsKey("myObject")); | ||
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsValue(myObject)); | ||
|
||
object myObject2 = dynamicModel.Get("myObject"); | ||
Assert.IsTrue(myObject2 == myObject); | ||
} | ||
|
||
[TestMethod] | ||
public void TestGetAdditionalProperties() | ||
{ | ||
DynamicModel<object> dynamicModel = new DynamicModel<object>(); | ||
object myObject = new object(); | ||
dynamicModel.Add("myObject", myObject); | ||
|
||
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsKey("myObject")); | ||
Assert.IsTrue(dynamicModel.AdditionalProperties.ContainsValue(myObject)); | ||
|
||
Dictionary<string, object> additionalProperties = dynamicModel.AdditionalProperties; | ||
Assert.IsTrue(additionalProperties == dynamicModel.AdditionalProperties); | ||
} | ||
} | ||
} |