Issues with Contentful SDK code after migration - xcode

See the code below I am having issues with. I have added most of the function even tho I am only getting the error in line 3. Just to give a better understanding of what I am trying to do.
func getTopArticles(_ vc: ArticleListViewController, subCatId: String) {
var articleDict = [String: Article]()
Constants.CLIENT.fetchEntries(["content_type":Constants.CONTENT_TYPE_ARTICLE,
"fields.top10Listing.sys.id":subCatId, "order":Constants.SORT_CREATED_AT_DESCENDING]) { //Getting error here
switch $0 {
case let .success(articleResult):
if articleResult.items.isEmpty {
vc.noTopArticlePresent()
}
else{
for articleEntry in articleResult.items {
let article = Article (entry:articleEntry)
vc.art.append(article)
// store into search cache
Constants.ARTICLE_CACHE.fetch(key: "articles").onSuccess({ (result) in
if let dictValue = result as? [String:Article]
{
articleDict = dictValue
articleDict[article.articleId] = article
}
Constants.ARTICLE_CACHE.set(value: articleDict, key: "articles")
}).onFailure({ (error) in
Constants.ARTICLE_CACHE.set(value: articleDict, key: "articles")
})
}
Constants.CACHE.set(value: NSKeyedArchiver.archivedData(withRootObject: vc.art), key: subCatId)
DispatchQueue.main.async {
vc.dothis()
}
}
}
}
Getting error in line 3. See error below
Argument labels '(__:,_:)' do not match any available overloads

you are missing the argument label matching for the method call.
The function signature is fetchEntries(matching:completion)
Taking your example above, and adding the our call would look like the following:
Constants.CLIENT.fetchEntries(matching:
["content_type": Constants.CONTENT_TYPE_ARTICLE,
"fields.top10Listing.sys.id":subCatId, "order":Constants.SORT_CREATED_AT_DESCENDING]) { (result: Result<ArrayResponse<Entry>>) in // Or anonymous argument $0 as you were using in your example.
Generally, if you see an error telling you argument labels don't match, you can find the declaration in the source and compare to see if you have a mismatch with the naming of your arguments being passed in. See more about argument labels and parameter names in Swift here.

Related

How to pass varargs in Spring shell with kotlin

I am trying to write a spring shell app and pass some var args. Basicaly I want to pass
fetch FA-207542 FA-207984 FA-211258 FA-202298
Documentation here is still TBD
#ShellMethod(value = "fetches all pic related data for processing")
fun fetch(vararg tickets: String) {
println("number of tickets is ${tickets.size}")
}
results
shell:>fetch FA-207542 FA-207984 FA-211258 FA-202298
number of tickets is 1
I've tried using a StringArray and I get an error:
fun fetch(tickets: StringArray) {
println("number of tickets is ${tickets.size()}")
}
error:
shell:>fetch FA-207542 FA-207984 FA-211258 FA-202298
Parameter specified as non-null is null: method FetchCommand.fetch, parameter tickets
Details of the error have been omitted. You can use the stacktrace command to print the full stacktrace.
With java (sry), I got this working:
#ShellComponent
public class MyCommands {
#ShellMethod
public String fetch(String... tickets) {
return String.format("Number of tickets is %d.", tickets.length);
}
}
then:
shell:>fetch --tickets FA-207542 FA-207984 FA-211258 FA-202298
Number of tickets is 4.
shell:>
Same results achieved with: #ShellCommand(arity = -1) resp. arity = Integer.MAX_VALUE.#

enum giving error with vapor 4 and fluent

This puzzles me:
I have a model in which I want to use an enum. I first declare the enum:
enum MenuChoices: String, Codable {
case reachableAt
case attentionTo
case reasonVisit
case reasonProblem
}
Then it is in my fields of the class:
#Enum(key: "menu_choices")
var menuChoices: MenuChoices
I then create it in the database using a migration:
struct CreateUserMenu: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.enum("menu_choices")
.case("reachable_at")
.case("attention_to")
.case("reason_visit")
.case("reason_problem")
.create()
.flatMap { menu_choices in
return database.schema("user_menus")
.id()
.field("created_at", .datetime, .required)
.field("updated_at",.datetime, .required)
.field("deleted_at",.datetime)
.field("menu_choices", menu_choices)
.field("be_nl", .string)
.field("be_fr", .string)
.field("en_us", .string)
.create()
}
}
}
So far so good. This migration works and the database looks ok. But when I want to add some data to seed the database in another migration I get an error :
let test = UserMenu( menuChoices: MenuChoices.reachableAt, beNl: "nl", beFr: "fra", enUs: "eng")
let _ = test.save(on: database)
+ App.addUserMenus on default
Would you like to continue?
y/n> y
[ ERROR ] previousError(MySQL error: Server error: Data truncated for column 'menu_choices' at row 1)
Fatal error: Error raised at top level: previousError(MySQL error: Server error: Data truncated for column 'menu_choices' at row 1): file /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.8.25.8/swift/stdlib/public/core/ErrorType.swift, line 200
USSS-Voyager-II:24yours data$
Unfortunately this error doesn’t really help to pinpoint the source of the problem
The problem is that there is no mapping between the swift definition of the enum and the Fluent enum. Putting a string literal value for your swift enum definition that matches the string literal value of the Fluent will fix the problem.
enum MenuChoices: String, Codable {
case reachableAt = "reachable_at"
case attentionTo = "attention_to"
case reasonVisit = "reason_visit"
case reasonProblem = "reason_problem"
}

Declared but not used

I am new to Golang. I am confused why I got the error that partial is not used in the code below?
// Using var keyword
var partial string
for i, request := range requestVec {
if i == (len(requestVec)-1) && !strings.Contains(request, "\r\n\r\n") {
partial = request
break
}
}
I assign request to partial in the if statement.
It is a compiler error in Go to declare a variable without using it.
In the code in you're example the variable partial is never used, only assigned, so is seen as unused. If you added additional code that read the variable then the error would be resolved. For example
var partial string
for i, request := range requestVec {
if i == (len(requestVec)-1) && !strings.Contains(request, "\r\n\r\n") {
partial = request
break
}
}
fmt.Println(partial) // We are now using `partial`

swift 4.2 - how can I check with guard if a var has a valid enum value

I need to check if a var, f.e. passed by a func, is a valid enum value. Not per se passed but just as an example here.
enum CollectionDict : String { // Mapping to String Model = "Model" or should I ...
case Model
case Type
case Element
case ....
}
....
guard InColectionDict != CollectionDict else { return false }
....
Obviously my sample guards line is wrong. What should I use or do to get the guard right or at least just compare/validate the InColectionDict variable with the enum CollectionDict in a one-liner?
I did hope to get away with..
func makeItem ( _ item: String , with key : String , inCollection : CollectionDict ) -> Bool {
guard let res = CollectionDict.inCollection else { return false }
But it give me an error.
Of course thank you in advance.
Swift is strongly typed. If your function has an non-optional Enum parameter, then at run-time it's guaranteed to be a valid enum value.

Union parameters with TypeScript 1.4

I have some troubles using union parameters. Imagine I have such a function:
interface IColumnDefinition {
id: string;
title: string;
}
addColumn(definition: string|IColumnDefinition) {
if (typeof (definition) === "string") {
definition = { id: definition, title: definition };
}
var column = new JSColumn(definition);
...
}
I want the user to either pass a complete object that defines a new column, or just pass a string, in which case I create a default object.
However, TypeScript has 2 problems:
definition = { id: definition }; is not allowed, since TypeScript thinks that definition might not be a string - which is wrong, since I make a typeof one line above.
new JSColumn(definition) is not possible, since the constructor requires a IColumnDefinition object, and definition could also be a string - which is also wrong, because the if above made sure that it is always a correct object.
What can I do to convince TypeScript that these are not errors? Is there anything like a cast, which says "Hey, this variable is of type xyz. Trust me, I know what I do."
I'm currently defining the parameter as any, which isn't really an option as I lose the whole advantage of type-checking.
If you want to circumvent the type system of TypeScript you can always use <any> cast.
In this case however there's a better solution:
function addColumn(definition: string | IColumnDefinition) {
var colDef: IColumnDefinition;
if (typeof definition === "string") {
colDef = { id: definition, title: definition };
} else {
colDef = definition;
}
var column = new JSColumn(colDef);
// ...
}
Another option, which might be less clear but generates a smaller JS code:
function addColumn(definition: string | IColumnDefinition) {
var column = new JSColumn(
(typeof definition === "string")
? { id: definition, title: definition }
: definition);
// ...
}

Resources