Action.ts 50.64 KiB
//
//  Action.swift
//
//  Copyright © 2020 memri. All rights reserved.
//
/*extension MemriContext {
private func getDataItem(_ dict:[String:Any], _ dataItem:DataItem?,
        _ viewArguments:ViewArguments? = nil) throws -> DataItem {
        // TODO refactor: move to function
        guard let stringType = dict["type"] as? String else {
            throw "Missing type attribute to indicate the type of the data item"
        guard let family = DataItemFamily(rawValue: stringType) else {
            throw "Cannot find find family \(stringType)"
        guard let ItemType = DataItemFamily.getType(family)() as? Object.Type else {
            throw "Cannot find family \(stringType)"
        var initArgs = dict
        initArgs.removeValue(forKey: "type")
        guard let item = ItemType.init() as? DataItem else {
            throw "Cannot cast type \(ItemType) to DataItem"
        // TODO: fill item
        for prop in item.objectSchema.properties {
            if prop.name != ItemType.primaryKey(),
                let inputValue = initArgs[prop.name] {
                let propValue: Any
                if let expr = inputValue as? Expression {
                    if let v = viewArguments {
                    propValue = try expr.execute(v) as Any
            else {
                    let viewArgs = ViewArguments(cascadingView.viewArguments.asDict())
                    viewArgs.set(".", dataItem)
                    propValue = try expr.execute(viewArgs) as Any
            else {
                    propValue = inputValue
                item.set(prop.name, propValue)
        return item
private func buildArguments(_ action:Action, _ dataItem:DataItem?,
        _ viewArguments:ViewArguments? = nil) throws -> [String: Any] {
        var args = [String: Any]()
        for (argName, inputValue) in action.arguments {
            var argValue: Any?
            // preprocess arg
            if let expr = inputValue as? Expression {
                argValue = try expr.execute(viewArguments ?? cascadingView.viewArguments) as Any
            else {