Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion io.github.diegoivan.libvalentine.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"app-id": "io.github.diegoivanme.libvalentine",
"runtime": "org.gnome.Platform",
"runtime-version": "42",
"runtime-version": "43",
"sdk": "org.gnome.Sdk",
"command": "libvalentine",
"sdk-extensions" : [
Expand Down
2 changes: 1 addition & 1 deletion src/Functions/delegates.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* delegates.vala
*
* Copyright 2022 Diego Iván <diegoivan.mae@gmail.com>
* Copyright 2022-2023 Diego Iván <diegoivan.mae@gmail.com>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
Expand Down
25 changes: 8 additions & 17 deletions src/Interfaces/TypeParser.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* TypeParser.vala
*
* Copyright 2022 Diego Iván <diegoivan.mae@gmail.com>
* Copyright 2022-2023 Diego Iván <diegoivan.mae@gmail.com>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
Expand All @@ -23,19 +23,15 @@
*/
[Version (since="0.2")]
public interface Valentine.TypeParser : Object {
internal abstract Gee.LinkedList<Property?> properties { get; set; default = new Gee.LinkedList<Property?> (); }
internal abstract Gee.LinkedList<ParserType> parser_types { get; set; default = new Gee.LinkedList<ParserType> (); }
internal abstract HashTable<string, Property> properties { get; set; }
internal abstract HashTable<Type, ParserType> parser_types { get; set; }

internal virtual Gee.ArrayList<Property?> get_parsable_properties () {
var parsable_types = new Gee.ArrayList<Property?> ();
foreach (Property property in properties) {
if (supports_type (property.type)) {
parsable_types.add (property);
continue;
internal virtual void remove_unparsable_properties () {
foreach (unowned string name in properties.get_keys ()) {
if (!supports_type (properties[name].type)) {
properties.remove (name);
}
}

return parsable_types;
}

/**
Expand All @@ -45,11 +41,6 @@ public interface Valentine.TypeParser : Object {
* @return Whether it is supported or not.
*/
public virtual bool supports_type (Type type) {
foreach (ParserType t in parser_types) {
if (t.type == type) {
return true;
}
}
return type.is_enum () || type.is_flags ();
return type in parser_types || type.is_enum () || type.is_flags ();
}
}
73 changes: 22 additions & 51 deletions src/Internal/SerializerLine.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* SerializerLine.vala
*
* Copyright 2022 Diego Iván <diegoivan.mae@gmail.com>
* Copyright 2022-2023 Diego Iván <diegoivan.mae@gmail.com>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -31,62 +31,33 @@ internal sealed class Valentine.SerializerLine : Valentine.AbstractLine<string>
position = pos;
}

public void serialize (Gee.ArrayList<Property?> serializable_properties, Gee.LinkedList<SerializableType> serializable_types) {
for (int i = 0; i < serializable_properties.size; i++) {
Property property = serializable_properties.get (i);
Value val = Value (property.type);
public void serialize (HashTable<string, Property> properties, HashTable<Type, SerializableType> serializable_types) {
for (List<unowned Property>? list = properties.get_values (); list != null; list = list.next) {
unowned Property property = list.data;
var @value = Value (property.type);

object.get_property (property.name, ref val);
object.get_property (property.name, ref @value);

bool found = false;
string s = "";
foreach (SerializableType type in serializable_types) {
if (type.type == property.type) {
s = type.func (val);
found = true;
SerializableType? type = serializable_types[property.type];
unowned TypeSerializationFunc? serialization_func = null;

string str = write_mode.parse_string (s, separator);
if (serializable_properties.size - 1 == i) {
str += "\n";
}
else {
str += "%s".printf (separator);
}

result += str;
break;
}
if (type == null) {
serialization_func = get_default_func (property.type);
}
else {
serialization_func = type.func;
}

if (!found) {
if (val.type ().is_flags ()) {
s = value_flags_to_string (val);
string str = write_mode.parse_string (s, separator);
if (serializable_properties.size - 1 == i) {
str += "\n";
}
else {
str += "%s".printf (separator);
}

result += str;
continue;
}

if (val.type ().is_enum ()) {
s = value_enum_to_string (val);
string str = write_mode.parse_string (s, separator);
if (serializable_properties.size - 1 == i) {
str += "\n";
}
else {
str += "%s".printf (separator);
}
result = write_mode.parse_string (serialization_func (value), separator);
// Write separator if it's not the last property.
result += "%s".printf (list.next == null ? "\n" : separator);
}
}

result += str;
continue;
}
}
public unowned TypeSerializationFunc get_default_func (Type type) {
if (type.is_enum ()) {
return value_enum_to_string;
}
return value_flags_to_string;
}
}
9 changes: 3 additions & 6 deletions src/Internal/Types.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* structs.vala
*
* Copyright 2022 Diego Iván <diegoivan.mae@gmail.com>
* Copyright 2022-2023 Diego Iván <diegoivan.mae@gmail.com>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
Expand All @@ -25,21 +25,18 @@ namespace Valentine {
}

private abstract class ParserType {
public Type type;
}

private class SerializableType : ParserType {
public TypeSerializationFunc func;
public SerializableType (Type t, owned TypeSerializationFunc f) {
type = t;
public SerializableType (owned TypeSerializationFunc f) {
func = (owned) f;
}
}

private class DeserializableType : ParserType {
public TypeDeserializationFunc func;
public DeserializableType (Type t, owned TypeDeserializationFunc f) {
type = t;
public DeserializableType (owned TypeDeserializationFunc f) {
func = (owned) f;
}
}
Expand Down
71 changes: 36 additions & 35 deletions src/ObjectSerializer.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* ObjectSerializer.vala
*
* Copyright 2022 Diego Iván <diegoivan.mae@gmail.com>
* Copyright 2022-2023 Diego Iván <diegoivan.mae@gmail.com>
*
* This file is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -29,8 +29,8 @@
public sealed class Valentine.ObjectSerializer<T> : Valentine.AbstractWriter, Valentine.TypeParser {
private List<Object> object_list = new List<Object> ();

internal Gee.LinkedList<Property?> properties { get; set; default = new Gee.LinkedList<Property?> (); }
internal Gee.LinkedList<ParserType> parser_types { get; set; default = new Gee.LinkedList<SerializableType> (); }
internal HashTable<string, Property> properties { get; set; default = new HashTable<string, Property> (str_hash, str_equal); }
internal HashTable<Type, ParserType> parser_types { get; set; default = new HashTable<Type, ParserType> (int_hash, int_equal); }

/**
* Constructs a new {@link ObjectSerializer} with the Type given
Expand All @@ -40,13 +40,13 @@ public sealed class Valentine.ObjectSerializer<T> : Valentine.AbstractWriter, Va
public ObjectSerializer () requires (typeof(T).is_object ()) {
Type obj_type = typeof (T);

ObjectClass klass = (ObjectClass) obj_type.class_ref ();
var klass = (ObjectClass) obj_type.class_ref ();
foreach (ParamSpec spec in klass.list_properties ()) {
if (READABLE in spec.flags) {
properties.add (new Property () {
properties[spec.name] = new Property () {
name = spec.name,
type = spec.value_type,
});
type = spec.value_type
};
}
}
}
Expand All @@ -56,20 +56,20 @@ public sealed class Valentine.ObjectSerializer<T> : Valentine.AbstractWriter, Va
}

construct {
parser_types.add (new SerializableType (typeof (string), value_string_to_string));
parser_types.add (new SerializableType (typeof (int), value_int_to_string));
parser_types.add (new SerializableType (typeof (uint), value_uint_to_string));
parser_types.add (new SerializableType (typeof (float), value_float_to_string));
parser_types.add (new SerializableType (typeof (double), value_double_to_string));
parser_types.add (new SerializableType (typeof (long), value_long_to_string));
parser_types.add (new SerializableType (typeof (ulong), value_ulong_to_string));
parser_types.add (new SerializableType (typeof (bool), value_boolean_to_string));
parser_types.add (new SerializableType (typeof (char), value_char_to_string));
parser_types.add (new SerializableType (typeof (uchar), value_uchar_to_string));
parser_types.add (new SerializableType (typeof (string[]), value_string_array_to_string));
parser_types.add (new SerializableType (typeof (Variant), value_variant_to_string));
parser_types.add (new SerializableType (typeof (File), value_file_to_string));
parser_types.add (new SerializableType (typeof (DateTime), value_datetime_to_string));
parser_types[typeof(string)] = new SerializableType (value_string_to_string);
parser_types[typeof(int)] = new SerializableType (value_int_to_string);
parser_types[typeof(uint)] = new SerializableType (value_int_to_string);
parser_types[typeof(float)] = new SerializableType (value_int_to_string);
parser_types[typeof(double)] = new SerializableType (value_int_to_string);
parser_types[typeof(long)] = new SerializableType (value_int_to_string);
parser_types[typeof(ulong)] = new SerializableType (value_int_to_string);
parser_types[typeof(bool)] = new SerializableType (value_int_to_string);
parser_types[typeof(char)] = new SerializableType (value_int_to_string);
parser_types[typeof(uchar)] = new SerializableType (value_int_to_string);
parser_types[typeof(string[])] = new SerializableType (value_int_to_string);
parser_types[typeof(Variant)] = new SerializableType (value_int_to_string);
parser_types[typeof(File)] = new SerializableType (value_int_to_string);
parser_types[typeof(DateTime)] = new SerializableType (value_int_to_string);
}

/**
Expand All @@ -89,36 +89,37 @@ public sealed class Valentine.ObjectSerializer<T> : Valentine.AbstractWriter, Va
public override string to_string () requires (typeof(T).is_object ()) {
string separator = separator_mode.get_separator ();
string output = "";
remove_unparsable_properties ();

Gee.ArrayList<Property?> parsable_properties = get_parsable_properties ();

// First, the column titles will be written with the property names that were obtained
for (int i = 0; i < parsable_properties.size; i++) {
// Here we check if it is the last one of the array to stop writing separators, and instead
// use a line break
if (i == parsable_properties.size - 1) {
output += "%s\n".printf (parsable_properties.get (i).name);
for (List<unowned Property>? list = properties.get_values (); list != null; list = list.next) {
// Here we check if it is the lat one of the list, so we stop writing separators.
// list.data is a Valentine.Property
if (list.next == null) {
output += "%s\n".printf (list.data.name);
continue;
}
output += "%s%s".printf (parsable_properties.get (i).name, separator);

output += "%s%s".printf (list.data.name, separator);
}

try {
int length = (int) object_list.length ();
var communication = new AsyncQueue<SerializerLine> ();

var processsing_thread_pool = new ThreadPool<SerializerLine>.with_owned_data ((serializer_line) => {
serializer_line.serialize (parsable_properties, (Gee.LinkedList<SerializableType>) parser_types);
serializer_line.serialize (properties, (HashTable<Type, SerializableType>) parser_types);
communication.push_sorted (serializer_line, lines_sort_func);
}, 8, false);

for (int i = 0; i < length; i++) {
processsing_thread_pool.add (new SerializerLine (object_list.nth_data (i), separator, write_mode, i));
int i = 0;
foreach (var object in object_list) {
processsing_thread_pool.add (new SerializerLine (object, separator, write_mode, i));
i++;
}

while (communication.length () != length);

for (int i = 0; i < length; i++) {
for (int j = 0; j < length; j++) {
output += communication.pop ().result;
}
}
Expand All @@ -140,7 +141,7 @@ public sealed class Valentine.ObjectSerializer<T> : Valentine.AbstractWriter, Va
*/
[Version (since="0.2.5")]
public void add_custom_parser_for_type (Type type, owned TypeSerializationFunc func) requires (typeof(T).is_object ()) {
parser_types.add (new SerializableType (type, (owned) func));
parser_types[type] = new SerializableType ((owned) func);
}

private int lines_sort_func (SerializerLine a, SerializerLine b) {
Expand Down