2017-07-01 3 views
0

Fluentクエリビルダにループ内から節を追加するにはどうすればよいですか?私はちょうど辞書の配列をループし、辞書の内容ごとに述語を追加したい。例えば、この配列のために:Fluent(Swift)の辞書配列からORグループ内のネストされたANDグループを追加

var data: [[String: String]] = [ 
     [ 
       "name": "Joe", 
       "email": "[email protected]" 
     ], 
     [ 
       "name": "John", 
       "email": "[email protected]" 
     ] 
] 

私はこのクエリを作成したいと思う:

var contacts = try Contact.makeQuery().or { orGroup in 
     try orGroup.and { andGroup in 
      try andGroup.filter("name", "Joe") 
      try andGroup.filter("email", "[email protected]") 
     } 
     try orGroup.and { andGroup in 
      try andGroup.filter("name", "John") 
      try andGroup.filter("email", "[email protected]") 
     } 
}.all() 

感謝。

答えて

0

私は単にするだろう:

var contacts = try Contact.makeQuery().or { orGroup in 
    try data.forEach { inner in 
     guard let name = inner["name"], let email = inner["email"] else { 
      throw SomeError 
     } 
     try orGroup.and { andGroup in 
      try andGroup.filter("name", name) 
      try andGroup.filter("email", email) 
     } 
    } 
}.all() 
関連する問題