Browse Source

switch json library, fix compilation errors

master
James Fenn 6 months ago
parent
commit
8347913a69
10 changed files with 69 additions and 30 deletions
  1. +1
    -0
      .gitignore
  2. +1
    -0
      Makefile
  3. +3
    -2
      dub.json
  4. +2
    -2
      dub.selections.json
  5. +0
    -7
      source/app.d
  6. +15
    -9
      source/config/base.d
  7. +7
    -2
      source/config/global.d
  8. +21
    -3
      source/model/instance.d
  9. +11
    -4
      source/model/port.d
  10. +8
    -1
      source/model/user.d

+ 1
- 0
.gitignore View File

@@ -5,3 +5,4 @@
.DS_Store
horrific
horrific-test-library
horrific-test-application

+ 1
- 0
Makefile View File

@@ -35,6 +35,7 @@ run: horrific
./horrific

clean:
dub clean
rm -rf pkg/
rm -rf src/
rm -rf HORRIFIC/


+ 3
- 2
dub.json View File

@@ -4,7 +4,8 @@
"Sean Bailey"
],
"dependencies": {
"asdf": "~>0.5.4"
"jsonizer": "~master"
},
"name": "horrific"
"name": "horrific",
"targetType": "executable"
}

+ 2
- 2
dub.selections.json View File

@@ -1,7 +1,7 @@
{
"fileVersion": 1,
"versions": {
"asdf": "0.5.4",
"jsonizer": "0.7.6"
"asdf": "0.5.7",
"jsonizer": "~master"
}
}

+ 0
- 7
source/app.d View File

@@ -1,11 +1,4 @@
import std.stdio;
import config.lock: ConfigLock;

void main(string[] args) {
writeln("Hello!");
ConfigLock lock = new ConfigLock();
lock.lock();
writeln("Lock acquired!");
lock.unlock();
writeln("Lock released!");
}

+ 15
- 9
source/config/base.d View File

@@ -3,7 +3,9 @@ module config.base;
import config.lock : ConfigLock;
import std.file : read, write;
import std.stdio : File;
import asdf;
import jsonizer.tojson;
import jsonizer.fromjson;
import jsonizer.common;

const string CONFIG_DIR = "/etc/horrific";

@@ -16,11 +18,9 @@ class Config(T) {

this(string jsonFile) {
this.jsonFile = jsonFile;
Asdf asdf = File(jsonFile)
.byChunk(4096)
.parseJson(32);
contents = deserialize!(T[])(asdf);

JsonizeOptions opts;
contents = fromJSONString!(T[])(jsonFile, opts);
}

public void append(T item) {
@@ -30,15 +30,21 @@ class Config(T) {

public void remove(T item) {
this.lock.acquire();
this.contents.remove(item);
foreach (i, entry; this.contents) {
if (entry == item) { // hacky solution to remove a single item (std.algorithm is... difficult)
this.contents = this.contents[0..i] ~ this.contents[i+1..$];
break;
}
}

}

public void write() {
public void save() {
if (!this.lock.hasLock) {
throw new Exception("Tried to write without a file lock...");
}

write(this.jsonFile, this.contents.serializeToJson());
write(this.jsonFile, this.contents.toJSONString());
}

}

+ 7
- 2
source/config/global.d View File

@@ -9,8 +9,13 @@ const string CONFIG_DIR = "/etc/horrific";

class GlobalConfig {

auto users = new Config!User(CONFIG_DIR ~ "/users.json");
auto ports = new Config!Port(CONFIG_DIR ~ "/ports.json");
Config!User users;
Config!Port ports;
Config!Instance[string] instances;

this() {
users = new Config!User(CONFIG_DIR ~ "/users.json");
ports = new Config!Port(CONFIG_DIR ~ "/ports.json");
}

}

+ 21
- 3
source/model/instance.d View File

@@ -1,11 +1,29 @@
module model.instance;

import jsonizer.jsonize;
import jsonizer.common;

struct Instance {
string id; // project git name/id
string owner; // project owner name
int[] ports; // reserved port numbers (or null)
mixin JsonizeMe;

@jsonize {
string id; // project git name/id
string owner; // project owner name
int[] ports; // reserved port numbers (or null)
}

bool opEquals(Instance other) {
return this.id != null && this.id == other.id;
}
}

unittest {
Instance first;
first.id = "hello";

Instance second;
assert(first != second && second != first, "Instances with different ids shouldn't be equal.");

second.id = "hello";
assert(first == second && second == first, "Instances with the same id should be equal.");
}

+ 11
- 4
source/model/port.d View File

@@ -1,11 +1,18 @@
module model.port;

import jsonizer.jsonize;
import jsonizer.common;

struct Port {
int port; // reserved port number
string owner; // user that reserved the port
string instance_id; // id of the instance using the port
mixin JsonizeMe;

@jsonize {
int port; // reserved port number
string owner; // user that reserved the port
string instance_id; // id of the instance using the port
}

bool opEquals(Port other) {
return this.port != null && this.port == other.port;
return this.port != 0 && this.port == other.port;
}
}

+ 8
- 1
source/model/user.d View File

@@ -1,7 +1,14 @@
module model.user;

import jsonizer.jsonize;
import jsonizer.common;

struct User {
string id; // user id
mixin JsonizeMe;

@jsonize {
string id; // user id
}

bool opEquals(User other) {
return this.id != null && this.id == other.id;


Loading…
Cancel
Save