Better json management

This commit is contained in:
2024-02-21 13:49:13 -03:00
parent f9dedca988
commit 9c5eb45d95
4 changed files with 48 additions and 42 deletions

View File

@@ -8,4 +8,5 @@ dependency "handy-httpd" version="~>8.2.0"
dependency "ddbc" version="~>0.5.8" dependency "ddbc" version="~>0.5.8"
dependency "requests" version="~>2.1.3" dependency "requests" version="~>2.1.3"
dependency "hibernated" version="~>0.4.0" dependency "hibernated" version="~>0.4.0"
subConfiguration "ddbc" "PGSQL" // subConfiguration "ddbc" "SQLite"
// subConfiguration "ddbc" "PGSQL"

View File

@@ -9,10 +9,13 @@ import util;
* https://www.w3.org/TR/activitypub/#obj * https://www.w3.org/TR/activitypub/#obj
+/ +/
class ASObject { class ASObject {
// Required fields (for root objects)
string context; /// Must be activitystream context string context; /// Must be activitystream context
string id; /// AP requirement for unique identifier string id; /// AP requirement for unique identifier
string type; /// AP requirement for type of object string type; /// AP requirement for type of object
// Optional fields
JSONValue raw; JSONValue raw;
/++ /++
@@ -25,24 +28,23 @@ class ASObject {
with (this) { with (this) {
const(JSONValue)* ascontext = "@context" in json; const(JSONValue)* ascontext = "@context" in json;
if (ascontext == null) if (ascontext) {
throw new Exception("No context in ActivityStream Object"); switch ((*ascontext).type) {
case JSONType.ARRAY:
context = (*ascontext)[0].str;
break;
switch ((*ascontext).type) { case JSONType.STRING:
case JSONType.ARRAY: context = (*ascontext).str;
context = (*ascontext)[0].str; break;
break;
case JSONType.STRING: default:
context = (*ascontext).str; throw new Exception("Invalid @context type for ActivityStream Object");
break; }
default:
throw new Exception("Invalid @context type for ActivityStream Object");
} }
id = json.requiredKey!"id".str; optional(json, "id", id);
type = json.requiredKey!"type".str; optional(json, "type", type);
raw = json; raw = json;
} }

View File

@@ -33,19 +33,16 @@ class Actor : ASObject {
super(json); super(json);
with (this) { with (this) {
inbox = json.requiredKey!"inbox".str; required(json, "inbox", inbox);
outbox = json.requiredKey!"outbox".str; required(json, "outbox", outbox);
following = json.requiredKey!"following".str; required(json, "following", following);
followers = json.requiredKey!"followers".str; required(json, "followers", followers);
liked = json.checkKey!string("liked"); optional(json, "liked", liked);
streams = json.checkKey!string("streams"); optional(json, "streams", streams);
preferredUsername = json.checkKey!string("preferredUsername"); optional(json, "preferredUsername", preferredUsername);
if (JSONValue* j = "endpoints" in json) endpoints = ActorEndpoints.fromJson(json.optional!JSONValue("endpoints"));
endpoints = ActorEndpoints.fromJson(*j);
else
endpoints = ActorEndpoints();
raw = json; raw = json;
} }
@@ -64,12 +61,12 @@ struct ActorEndpoints {
ActorEndpoints endpoints = ActorEndpoints(); ActorEndpoints endpoints = ActorEndpoints();
with (endpoints) { with (endpoints) {
proxyUrl = json.checkKey!string("proxyUrl"); optional(json, "proxyUrl", proxyUrl);
oauthAuthorizationEndpoint = json.checkKey!string("oauthAuthorizationEndpoint"); optional(json, "oauthAuthorizationEndpoint", oauthAuthorizationEndpoint);
oauthTokenEndpoint = json.checkKey!string("oauthTokenEndpoint"); optional(json, "oauthTokenEndpoint", oauthTokenEndpoint);
provideClientKey = json.checkKey!string("provideClientKey"); optional(json, "provideClientKey", provideClientKey);
signClientKey = json.checkKey!string("signClientKey"); optional(json, "signClientKey", signClientKey);
sharedInbox = json.checkKey!string("sharedInbox"); optional(json, "sharedInbox", sharedInbox);
} }
return endpoints; return endpoints;

View File

@@ -3,18 +3,24 @@ module util;
import std.format; import std.format;
import std.json; import std.json;
JSONValue requiredKey(string key)(JSONValue val) { void optional(T)(ref JSONValue val, string key, ref T receiver) {
const(JSONValue)* j = key in val; const(JSONValue)* p = key in val;
if (j) if (p == null)
return *j; return;
throw new Exception(format("Key %s not found in json", key)); static if (is(JSONValue == T))
receiver = *p;
else
receiver = (*p).get!T;
} }
T checkKey(T)(JSONValue val, string key) { T optional(T)(ref JSONValue val, string key) {
if (JSONValue* j = key in val) T t;
return (*j).get!T; optional(val, key, t);
return t;
return T.init; }
void required(T)(ref JSONValue val, string key, ref T receiver) {
receiver = val[key].get!T;
} }