Skip to content

Commit

Permalink
🚧 Fixes & Rollup support added.
Browse files Browse the repository at this point in the history
- Added support for rollups into the setup.
- Fixed an issue with notion data wrappers.
- Fixed an issue with an incorrect using statement in the data asset template file.
  • Loading branch information
JonathanMCarter committed Jun 15, 2024
1 parent 16b36b1 commit ef9a00c
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CarterGames.Cart.Data.Notion;
using NotionToUnity;
using UnityEngine;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static class NotionDownloadParser
/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Methods
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Handles parsing the data from the database into the query class for use.
/// </summary>
Expand All @@ -44,7 +44,7 @@ public static class NotionDownloadParser
public static NotionDatabaseQueryResult Parse(string data)
{
var result = new List<NotionDatabaseRow>();

for (var i = 0; i < JSONNode.Parse(data)["results"].AsArray.Count; i++)
{
result.Add(GetRowData(JSONNode.Parse(data)["results"][i]["properties"]));
Expand All @@ -63,68 +63,80 @@ private static NotionDatabaseRow GetRowData(JSONNode element)
{
var keys = new List<string>();
var lookup = new SerializableDictionary<string, NotionProperty>();


foreach (var k in element.Keys)
{
keys.Add(k);
}


for (var i = 0; i < keys.Count; i++)
{
var propName = keys[i];
var propType = element.AsObject[i]["type"].Value;
var adjustedKey = keys[i].Trim().ToLower().Replace(" ", string.Empty);



switch (element.AsObject[i]["type"].Value)
{
case "title":
lookup.Add(adjustedKey, new NotionProperty(propName, propType, element.AsObject[i]["title"][0]["text"]["content"].Value));
break;
case "rich_text":
lookup.Add(adjustedKey, new NotionProperty(propName, propType, element.AsObject[i]["rich_text"][0]["text"]["content"].Value));
break;
case "number":
lookup.Add(adjustedKey, new NotionProperty(propName, propType, element.AsObject[i]["number"].Value));
break;
case "checkbox":
lookup.Add(adjustedKey, new NotionProperty(propName, propType, element.AsObject[i]["checkbox"].Value));
break;
case "select":

if (element.AsObject[i]["select"]["name"] == null)
{
lookup.Add(adjustedKey, new NotionProperty(propName, propType, null));
}
else
{
lookup.Add(adjustedKey, new NotionProperty(propName, propType, element.AsObject[i]["select"]["name"].Value));
}

break;
case "multi_select":

var elements = new NotionArrayWrapper<string>
{
list = new List<string>()
};

for (var j = 0; j < element.AsObject[i]["multi_select"].Count; j++)
{
elements.list.Add(element.AsObject[i]["multi_select"][j]["name"].Value);
}

lookup.Add(adjustedKey, new NotionProperty(propName, propType, JsonUtility.ToJson(elements)));
lookup.Add(adjustedKey, new NotionProperty(propName, propType, GetValueForType(element.AsObject[i]["type"].Value, element.AsObject[i])));
break;
case "rollup":
lookup.Add(adjustedKey, new NotionProperty(propName, propType, GetValueForType(element.AsObject[i]["rollup"]["array"][0]["type"].Value, element.AsObject[i]["rollup"]["array"][0])));
break;
default:
Debug.LogError($"Unable to assign value: {keys[i]} as the Notion data type was not found.");
Debug.LogWarning($"Unable to assign value: {keys[i]} as the Notion data type {element.AsObject[i]["type"].Value} is not supported.");
break;
}
}



return new NotionDatabaseRow(lookup);
}


/// <summary>
/// Gets the value of the notion property type entered.
/// </summary>
/// <param name="type">The type to read.</param>
/// <param name="element">The element to get the value from.</param>
/// <returns>The value found.</returns>
private static string GetValueForType(string type, JSONNode element)
{
switch (type)
{
case "title":
return element["title"][0]["text"]["content"].Value;
case "rich_text":
return element["rich_text"][0]["text"]["content"].Value;
case "number":
return element["number"].Value;
case "checkbox":
return element["checkbox"].Value;
case "select":
return element["select"]["name"] == null ? null : element["select"]["name"].Value;
case "multi_select":

var elements = new NotionArrayWrapper<string>
{
list = new List<string>()
};

for (var j = 0; j < element["multi_select"].Count; j++)
{
elements.list.Add(element["multi_select"][j]["name"].Value);
}

return JsonUtility.ToJson(elements);
default:
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void Apply(NotionDatabaseQueryResult result)
var valueData = row.DataLookup[field.Name.Trim().ToLower()];
var fieldType = field.FieldType;

if (fieldType.BaseType.FullName.Contains("Notion.NotionWrapper"))
if (fieldType.BaseType.FullName.Contains(typeof(NotionWrapper<>).Namespace + ".NotionWrapper"))
{
var instance = valueData.GetValueAs(fieldType);
field.SetValue(newEntry, instance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,42 @@ namespace NotionToUnity
[Serializable]
public class NotionWrapper<T> where T : Object
{
/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Fields
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

[SerializeField] private string id;
[SerializeField] private T value;


/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Properties
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// The value stored in the wrapper.
/// </summary>
public T Value => value;

/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Constructors
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Makes a new wrapper with the entered id.
/// </summary>
/// <param name="id"></param>
public NotionWrapper(string id)
{
this.id = id;
}


/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Methods
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Assigns the reference when called.
/// </summary>
private void Assign()
{
#if UNITY_EDITOR
Expand All @@ -68,13 +91,26 @@ private void Assign()
#endif
}

/* ─────────────────────────────────────────────────────────────────────────────────────────────────────────────
| Operator
───────────────────────────────────────────────────────────────────────────────────────────────────────────── */

/// <summary>
/// Converts the wrapper to its implementation type.
/// </summary>
/// <param name="wrapper">The wrapper to convert.</param>
/// <returns>The value of the wrapper.</returns>
public static implicit operator T(NotionWrapper<T> wrapper)
{
return wrapper.Value;
}




/// <summary>
/// Converts the type to thr wrapper.
/// </summary>
/// <param name="reference">The value to convert.</param>
/// <returns>The wrapper with the value.</returns>
public static implicit operator NotionWrapper<T>(T reference)
{
return new NotionWrapper<T>(reference.name);
Expand Down

0 comments on commit ef9a00c

Please sign in to comment.