Skip to content

Commit

Permalink
add json resolve for memebrs
Browse files Browse the repository at this point in the history
  • Loading branch information
Quin Lynch committed Apr 26, 2024
1 parent 27ef99c commit 8bb87b7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/EdgeDB.Net.QueryBuilder/QueryBuilder/QueryBuilder.Insert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static IInsertQuery<TType, QueryContextSelf<TType>> Insert<TType>(Express
public static IInsertQuery<TType, QueryContextSelf<TType>> Insert<TType>(Expression<Func<QueryContextSelf<TType>, TType>> value)
=> new QueryBuilder<TType>().Insert(value);

/// <inheritdoc cref="IQueryBuilder{TType, QueryContext}.Insert{TNew}(Expression{Func{QueryContext, TNew}})"/>
public static IInsertQuery<TType, QueryContextSelf<TType>> Insert<TType>(Expression<Func<TType>> value)
=> new QueryBuilder<TType>().Insert(value);

/// <inheritdoc cref="IQueryBuilder{TType, QueryContext}.Insert{TNew}(TNew, bool)"/>
public static IInsertQuery<TType, QueryContextSelf<TType>> Insert<TType>(TType value, bool returnInsertedValue)
=> new QueryBuilder<TType>().Insert(value, returnInsertedValue);
Expand Down Expand Up @@ -119,6 +123,12 @@ public IInsertQuery<TNew, TContext> Insert<TNew>(Expression<Func<TContext, TNew>
return EnterNewType<TNew>();
}

public IInsertQuery<TNew, TContext> Insert<TNew>(Expression<Func<TNew>> value)
{
AddNode<InsertNode>(new InsertContext(typeof(TNew), value));
return EnterNewType<TNew>();
}

/// <inheritdoc/>
public IInsertQuery<TNew, TContext> Insert<TNew>(Expression<Func<TContext, TNew>> value)
=> Insert(value, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -50,12 +51,56 @@ public override void Translate(MemberExpression expression, ExpressionContext co
case ParameterExpression param:
TranslateParameterMember(writer, param, path, context);
break;
case ConstantExpression jsonConstant
when jsonConstant.Type.IsAssignableTo(typeof(IJsonVariable)) ||
(
jsonConstant.Type.GenericTypeArguments.Length == 1 &&
jsonConstant.Type.IsAssignableTo(typeof(IStrongBox)) &&
jsonConstant.Type.GenericTypeArguments[0].IsAssignableTo(typeof(IJsonVariable))
):
TranslateJsonMember(writer, jsonConstant, path, context);
break;
case ConstantExpression constant:
TranslateConstantMember(writer, constant, path, context);
break;
}
}

private void TranslateJsonMember(QueryWriter writer, ConstantExpression expression, MemberExpression[] path,
ExpressionContext context)
{
if (expression.Value is not IJsonVariable jsonVariable)
{
if (expression.Value is IStrongBox {Value: IJsonVariable boxedJsonVariable})
jsonVariable = boxedJsonVariable;
else
throw new InvalidOperationException("Could not extract json variable reference");
}

var jsonpath = path[..^2];

if (!EdgeDBTypeUtils.TryGetScalarType(jsonpath[0].Type, out var scalar))
throw new InvalidOperationException($"Expecting a scalar value, found {jsonpath[0].Type.Name}");

var args = new Terms.FunctionArg[jsonpath.Length + 1];

args[0] = jsonVariable.Name;

for (var i = 0; i != jsonpath.Length; i++)
{
var name = jsonpath[^(i + 1)].Member.Name;
args[i + 1] = Value.Of(writer => writer.SingleQuoted(name));
}

writer
.TypeCast(scalar.ToString(), new CastMetadata(scalar, scalar.ToString()))
.Function(
"json_get",
debug: Defer.This(() => $"Json member path"),
args
);
}

private void TranslateConstantMember(QueryWriter writer, ConstantExpression constant, MemberExpression[] path,
ExpressionContext context)
{
Expand Down

0 comments on commit 8bb87b7

Please sign in to comment.