Commit 34a4a2ac authored by Christian Hergert's avatar Christian Hergert

Merge branch 'wip/strv-iterate' into 'master'

Strv iteration

See merge request !1
parents 138182ef 19a1f1e4
using GLib;
using Template;
class Article : Object {
public string title {
get { return "Sample Article"; }
}
public string author {
get { return Environment.get_user_name(); }
}
private string _date = (new DateTime.now_local()).to_string();
public string date {
get { return _date; }
}
private string[] _sections = {"Iterate", "over", "strv"};
public string[] sections {
get { return _sections; }
}
}
static int main (string[] args) {
var file = GLib.File.new_for_path("article.tmpl");
var tmpl = new Template.Template (null);
try {
tmpl.parse_file (file, null);
var scope = new Template.Scope ();
scope["article"].assign_object(new Article());
var expanded = tmpl.expand_string (scope);
stdout.printf ("%s\n", expanded);
} catch (GLib.Error ex) {
stderr.printf ("%s\n", ex.message);
return 1;
}
return 0;
}
......@@ -57,6 +57,46 @@ string_get_value (TmplIterator *iter,
return FALSE;
}
static gboolean
strv_move_next (TmplIterator *iter)
{
guint index = GPOINTER_TO_INT (iter->data1);
index++;
if (iter->instance)
{
gchar **strv = iter->instance;
iter->data1 = GINT_TO_POINTER (index);
if (!strv[index])
{
iter->instance = NULL;
}
return strv[index] != 0;
}
return FALSE;
}
static gboolean
strv_get_value (TmplIterator *iter,
GValue *value)
{
guint index = GPOINTER_TO_INT (iter->data1);
if (iter->instance)
{
gchar **strv = iter->instance;
gchar *str = strv[index];
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, str);
return TRUE;
}
return FALSE;
}
static gboolean
list_model_move_next (TmplIterator *iter)
{
......@@ -127,6 +167,30 @@ tmpl_iterator_init (TmplIterator *iter,
iter->data2 = GUINT_TO_POINTER (n_items);
}
}
else if (G_VALUE_HOLDS_VARIANT(value) &&
g_variant_is_of_type (
g_value_get_variant(value), G_VARIANT_TYPE_STRING_ARRAY))
{
iter->instance = (const gchar **) g_variant_get_strv (
g_value_get_variant (value), NULL);
iter->move_next = strv_move_next;
iter->get_value = strv_get_value;
iter->destroy = NULL;
iter->data1 = GINT_TO_POINTER (-1);
}
else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
{
iter->instance = (const gchar **) g_value_get_boxed (value);
iter->move_next = strv_move_next;
iter->get_value = strv_get_value;
iter->destroy = NULL;
iter->data1 = GINT_TO_POINTER (-1);
}
else
{
g_critical ("Don't know how to iterate %s",
g_strdup_value_contents (value));
}
/* TODO: More iter types */
}
......
......@@ -300,6 +300,48 @@ tmpl_scope_set_object (TmplScope *self,
tmpl_symbol_assign_object (tmpl_scope_get_full (self, name, TRUE), value);
}
/**
* tmpl_scope_set_variant:
* @self: A #TmplScope
* @name: a name for the symbol
* @value: (nullable): the variant to set it to, or %NULL
*
* Sets the value of the symbol named @name to the variant @value.
*
* If @value has a floating reference, it is consumed.
*/
void
tmpl_scope_set_variant (TmplScope *self,
const gchar *name,
GVariant *value)
{
g_return_if_fail (self != NULL);
g_return_if_fail (name != NULL);
tmpl_symbol_assign_variant (tmpl_scope_get_full (self, name, TRUE),
value);
}
/**
* tmpl_scope_set_strv:
* @self: A #TmplScope
* @name: a name for the symbol
* @value: (nullable) (array zero-terminated=1): the value to set it to, or %NULL
*
* Sets the value of the symbol named @name to the strv @value.
*/
void
tmpl_scope_set_strv (TmplScope *self,
const gchar *name,
const gchar **value)
{
g_return_if_fail (self != NULL);
g_return_if_fail (name != NULL);
tmpl_symbol_assign_variant (tmpl_scope_get_full (self, name, TRUE),
g_variant_new_strv (value, -1));
}
/**
* tmpl_scope_set_string:
* @self: A #TmplScope
......
......@@ -76,6 +76,17 @@ TMPL_AVAILABLE_IN_ALL
void tmpl_scope_set_object (TmplScope *self,
const gchar *name,
gpointer value);
TMPL_AVAILABLE_IN_3_28
void tmpl_scope_set_strv (TmplScope *self,
const gchar *name,
const gchar **value);
TMPL_AVAILABLE_IN_3_28
void tmpl_scope_set_variant (TmplScope *self,
const gchar *name,
GVariant *value);
TMPL_AVAILABLE_IN_ALL
void tmpl_scope_set_resolver (TmplScope *self,
TmplScopeResolver resolver,
......
......@@ -222,6 +222,13 @@ tmpl_symbol_assign_string (TmplSymbol *self,
g_value_unset (&value);
}
/**
* tmpl_symbol_assign_object:
* @self: A #TmplSymbol
* @v_object: (type GObject.Object) (nullable): a #GObject or %NULL.
*
* Sets the value to the object @v_object.
*/
void
tmpl_symbol_assign_object (TmplSymbol *self,
gpointer v_object)
......@@ -235,3 +242,47 @@ tmpl_symbol_assign_object (TmplSymbol *self,
tmpl_symbol_assign_value (self, &value);
g_value_unset (&value);
}
/**
* tmpl_symbol_assign_variant:
* @self: A #TmplSymbol
* @v_object: (nullable): a #GVariant or %NULL.
*
* Sets the value to the #GVariant @v_variant.
*
* If @v_variant has a floating reference, it is consumed.
*/
void
tmpl_symbol_assign_variant (TmplSymbol *self,
GVariant *v_variant)
{
GValue value = G_VALUE_INIT;
g_return_if_fail (self != NULL);
g_value_init (&value, G_TYPE_VARIANT);
g_value_set_variant (&value, v_variant);
tmpl_symbol_assign_value (self, &value);
g_value_unset (&value);
}
/**
* tmpl_symbol_assign_strv:
* @self: A #TmplSymbol
* @strv: (nullable) (array zero-terminated=1): the value to set, or %NULL
*
* Sets the value to the strv @strv.
*/
void
tmpl_symbol_assign_strv (TmplSymbol *self,
const gchar **strv)
{
GValue value = G_VALUE_INIT;
g_return_if_fail (self != NULL);
g_value_init (&value, G_TYPE_VARIANT);
g_value_set_variant (&value, g_variant_new_strv (strv, -1));
tmpl_symbol_assign_value (self, &value);
g_value_unset (&value);
}
......@@ -58,6 +58,14 @@ void tmpl_symbol_assign_string (TmplSymbol *self,
TMPL_AVAILABLE_IN_ALL
void tmpl_symbol_assign_object (TmplSymbol *self,
gpointer v_object);
TMPL_AVAILABLE_IN_3_28
void tmpl_symbol_assign_variant (TmplSymbol *self,
GVariant *v_variant);
TMPL_AVAILABLE_IN_3_28
void tmpl_symbol_assign_strv (TmplSymbol *self,
const gchar **strv);
TMPL_AVAILABLE_IN_ALL
void tmpl_symbol_assign_expr (TmplSymbol *self,
TmplExpr *expr,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment