小编Nux*_*Nux的帖子

带条件的 Kotlin distinctBy

我有多个具有相同键的对象的数组,其他对象具有空值,我希望使用 distinctBy 删除重复项并获取值具有最长字符串的对象。

data class ObjA(val key: String, val value: String)

fun test() {
    val listOfA = mutableListOf(
            ObjA("one", ""), //This will be taken in distinctBy.
            ObjA("one", "o"),
            ObjA("one", "on"),
            ObjA("one", "one"), //This will be ignored in distinctBy. I WANT THIS ONE.

            ObjA("two", ""), //This will be returned in distinctBy
            ObjA("two", "2"),
            ObjA("two", "two"), //But I want this.

            ObjA("three", "3"),
            ObjA("four", "4"),
            ObjA("five", "five")
    )

    val listOfAWithNoDuplicates = listOfA.distinctBy { it.key }

    for (o in listOfAWithNoDuplicates) {
        println("key: ${o.key}, value: …
Run Code Online (Sandbox Code Playgroud)

kotlin

4
推荐指数
1
解决办法
3351
查看次数

如何模拟 typeORM 的 getCustomRepository

我想对一个在其构造函数中使用 getCustomRepository 的类进行单元测试,但我只是想不出一种简单的方法来模拟它。这是我的班级代码

import {getCustomRepository} from 'typeorm';

export class Controller {
  private repository: UserRepository;

  constructor() {
    this.repository = getCustomRepository(UserRepository); //I want to mock this.
  }

  async init() {
    return this.repository.findUser(1);
  }
}
Run Code Online (Sandbox Code Playgroud)

这是测试

describe('User Tests', () => {
  it('should return user', async () => {
    //Fake user to be resolved.
    const user = new User();
    user.id = 2;

    //I want to mock  getCustomRepository(UserRepository); here
    //getCustomRepository = jest.fn().mockResolvedValue(UserRepository); HERE HOW???

    //Mocking find user
    UserRepository.prototype.findUser = jest.fn().mockResolvedValue(user);

    const controller = new Controller(); …
Run Code Online (Sandbox Code Playgroud)

unit-testing node.js typescript jestjs typeorm

4
推荐指数
1
解决办法
1万
查看次数

如何在 Windows 10 上安装 bcrypt

我想在我的 Express 项目中安装bcrypt。我已按照此页面中为 Windows 用户提供的说明进行操作。我只是运行给定的命令npm install --global --production windows-build-tools,它成功地如下图所示。

在此处输入图片说明 但是当我运行时仍然出现npm i bcrypt以下错误:

node-pre-gyp WARN 使用needle for node-pre-gyp https 下载 node-pre-gyp WARN 尝试下载(404):https : //github.com/kelektiv/node.bcrypt.js/releases/download/v3 .0.3/bcrypt_lib-v3.0.3-node-v64-win32-x64-unknown.tar.gz node-pre-gyp WARN 未找到 bcrypt@3.0.3 和 node@10.15.0 (node-v64 ABI,未知)(使用 node-gyp 回退到源代码编译)gyp ERR!堆栈错误:找不到 Python 可执行文件“python2.7”,您可以设置 PYTHON 环境变量。....更多错误

我设法从这个答案设置Python的路径在这里。现在我收到这个错误:

node-pre-gyp WARN 使用needle 进行node-pre-gyp https 下载 node-pre-gyp WARN 尝试下载(404) https://github.com/kelektiv/node.bcrypt.js/releases/download/v3。 0.3/bcrypt_lib-v3.0.3-node-v64-win32-x64-unknown.tar.gz

node-pre-gyp WARN 未找到 bcrypt@3.0.3 和 node@10.15.0 的预构建二进制文件(node-v64 ABI,未知)(使用 node-gyp 回退到源代码编译)在此解决方案一中构建项目一次。要启用并行构建,请添加“/m”开关。吹鱼.cc bcrypt.cc bcrypt_node.cc win_delay_load_hook.cc

bcrypt node.js express

3
推荐指数
2
解决办法
1万
查看次数

从列表中删除不在另一个列表中的元素 - Kotlin

我有两个可变列表,listOfA 有很多对象,包括重复项,而 listOfB 有更少的对象。所以我想使用 listOfB 来过滤 listOfA 中的相似对象,这样所有列表最后都会有相同数量的具有等效键的对象。下面的代码可以解释更多。

fun main() {
    test()
}

data class ObjA(val key: String, val value: String)
data class ObjB(val key: String, val value: String, val ref: Int)

fun test() {
    val listOfA = mutableListOf(
            ObjA("one", ""),
            ObjA("one", "o"),
            ObjA("one", "on"),
            ObjA("one", "one"),

            ObjA("two", ""),
            ObjA("two", "2"),
            ObjA("two", "two"),

            ObjA("three", "3"),
            ObjA("four", "4"),
            ObjA("five", "five")
    )

    //Use this list's object keys to get object with similar keys in above array.
    val listOfB = mutableListOf(
            ObjB("one", "i", …
Run Code Online (Sandbox Code Playgroud)

kotlin

3
推荐指数
1
解决办法
3097
查看次数

Github 操作:尝试使用工作目录启动进程“/usr/bin/bash”时发生错误。没有这样的文件或目录

我正在创建两个共享许多功能的 flutter 应用程序,并决定创建一个单独的包供两者使用。

以下是目录结构

  • 我的项目
    • .github/workflows/main.yaml
    • 应用
      • 应用程序1
        • lib/main_devnet.dart
      • 应用程序2
        • lib/main_devnet.dart
      • 图形
        • 主程序.dart

graph 包中有一些代码是由 code_builder 通过运行生成的

flutter pub run build_runner build --delete-conflicting-outputs
Run Code Online (Sandbox Code Playgroud)

在我的计算机中,构建 apk 时一切正常,但我试图使用 github 操作自动化这项工作。

这是我当前的工作流程,我无法正常工作。

.github/workflows/main.yaml

flutter pub run build_runner build --delete-conflicting-outputs
Run Code Online (Sandbox Code Playgroud)

将我的代码推送到 github 时,出现以下错误,导致 CI​​ 失败:

Run flutter pub get
  flutter pub get
  shell: /usr/bin/bash -e {0}
Error: An error occurred trying to start process '/usr/bin/bash' with working directory '/home/runner/work/my-app-repository/my-app-repository/./apps/app2/'. No such file or directory
Run Code Online (Sandbox Code Playgroud)

我认为问题在于使用矩阵定义工作目录,正如您在错误中看到的那样, my-app-repositorymy-app-repository/my-app-repository这个词重复了两次,导致错误的目录并使整个过程失败。

我该如何解决这个问题?

flutter github-actions

1
推荐指数
1
解决办法
1万
查看次数