小编din*_*igo的帖子

如果仅更改了空格和换行符,如何忽略'git status'中的文件

我经常使用一些选项卡而不是空格来推送代码,反之亦然,或者插入换行符以提高代码可读性.我如何告诉git不要寻找这些变化?

git

10
推荐指数
2
解决办法
5698
查看次数

使用gson反序列化时,将TypeToken转换为Type时出错

我有一个类使用此函数反序列化泛型的ArrayList,正如此线程的第一个答案所述:Java抽象类函数泛型类型

public <T> ArrayList<T> arrayType(String data){
    return g.fromJson(data,   TypeToken.get(new ArrayList<T>().getClass()));
}
Run Code Online (Sandbox Code Playgroud)

Eclipse要求我强制转换TypeToken导致这个(函数fromJson需要一个Type,而不是TypeToken)

public <T> ArrayList<T> arrayType(String data){
    return g.fromJson(data,  (Type) TypeToken.get(new ArrayList<T>().getClass()));
}
Run Code Online (Sandbox Code Playgroud)

结果我得到这个错误:

java.lang.ClassCastException: com.google.gson.reflect.TypeToken cannot be cast to java.lang.reflect.Type
Run Code Online (Sandbox Code Playgroud)

gson用户手册中,他们告诉您这是调用该函数的正确方法

Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
Run Code Online (Sandbox Code Playgroud)

我无法看到我做错了什么(如果它是一个有效的答案,为什么我得到这个演员错误?)

java eclipse generics casting gson

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

用 Jest 模拟 new Function()

我在尝试使用构造函数模拟模块时遇到问题

// code.js
const ServiceClass = require('service-library');
const serviceInstance = new ServiceClass('some param');
exports.myFunction = () => {
  serviceInstance.doSomething();
};
Run Code Online (Sandbox Code Playgroud)

和测试代码:

// code.test.js
const ServiceClass = require('service-library');
jest.mock('service-library');
const {myFunction} = require('../path/to/my/code');

test('check that the service does something', () => {
  // ????
});
Run Code Online (Sandbox Code Playgroud)

它不像文档示例模拟模块,因为您需要在导入后实例化模块。也不像 Mocking a Function。

我怎么能doSomething()在测试时模拟这个功能?

作为参考,我试图在@google-cloud/*这里模拟包。我有一些项目可以利用这一点。

unit-testing mocking node.js google-cloud-platform jestjs

7
推荐指数
2
解决办法
9142
查看次数

使用Jackson反序列化某些JSON标记

有没有一种方法,我不必从JSON中"手动"提取标签,如果我不想反序列化所有这些,以便我可以使用这个构造函数?

public class Tweet {
    public String username;
    public String message;
    public String image_url;

    @JsonCreator
    public Tweet(
            @JsonProperty("from_user")          String username,
            @JsonProperty("text")               String message,
            @JsonProperty("profile_image_url")  String url) {
        this.username = username;
        this.message = message;
        this.image_url = url;
    }
}
Run Code Online (Sandbox Code Playgroud)

这里是JSON:

   {
         "created_at":"Wed, 15 Aug 2012 18:17:55 +0000",
         "from_user":"felix_panda",
         "from_user_id":132396774,
         "from_user_id_str":"132396774",
         "from_user_name":"felix suganda",
         "geo":null,
         "id":235802515571101696,
         "id_str":"235802515571101696",
         "iso_language_code":"en",
         "metadata":{
            "result_type":"recent"
         },
         "profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/2393214158\/profile_normal.jpg",
         "profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/2393214158\/profile_normal.jpg",
         "source":"&lt;a href=&quot;http:\/\/www.tweetcaster.com&quot; rel=&quot;nofollow&quot;&gt;TweetCaster for Android&lt;\/a&gt;",
         "text":"@Android how do u fix you lost your data connection because you left home network …
Run Code Online (Sandbox Code Playgroud)

java json jackson deserialization

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

iframe内的Angular 2触发插值

我想在iframe中显示模板化网页的内容.但是在加载内容之后,模板不会通过角度进行插值.是因为变化检测系统?可以通过其他方式实现吗?

@Component({
  selector: 'my-app',
  template : `<iframe [src]="template" width="100%" height="300px"></iframe>`
})
export class App {
  template: string = `
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <title>Page title</title>

      </head>
      <body>
        <p>{{strings[0]}}</p>
        <p>{{strings[1]}}</p>
        <p>{{strings[2]}}</p>
      </body>
    </html>
  `;
  strings: string[] = ['first element', 'second element', 'third element'];
  constructor(){
    this.template = 'data:text/html;charset=utf-8,' + encodeURI(this.template);
  }
}

bootstrap(App)
Run Code Online (Sandbox Code Playgroud)

http://plnkr.co/edit/mWrqv1?p=preview

它似乎无法使用innerHtml绑定:

@Component({
  selector: 'my-app',
  template : `<div [innerHtml]="template"></div>`
})
export class App {
  template: string …
Run Code Online (Sandbox Code Playgroud)

iframe angular

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

运行junit测试一定的时间

我想测试一段时间,比如5秒钟,然后通过测试,如果没有任何错误被断言.这可能带注释吗?可以使用类似的东西@Test(uptime=5000)吗?

java testing junit

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

别名在脚本中丢失

我想在GNU最新别名gdatedate当程序在Mac上运行

#!/bin/bash
if [[ $(uname) -eq 'Darwin' ]]; then
    alias date="gdate"
    echo 'you are on a mac!'
    type date
fi
# rest of the program
Run Code Online (Sandbox Code Playgroud)

鉴于此代码,如果我直接在终端上运行int它打印:

you are on a mac!
date is an alias for gdate
Run Code Online (Sandbox Code Playgroud)

但是,如果我像./test.sh打印一样运行脚本本身:

you are on a mac!
date is /bin/date
Run Code Online (Sandbox Code Playgroud)

为什么不从脚本中应用别名?

macos bash shell

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

如何正确创建别名

我已经测试了这个命令

$ nmap -sP 192.168.1.* | grep 192  | awk '{print $5}'
Run Code Online (Sandbox Code Playgroud)

产生这个输出

192.168.1.1
192.168.1.33
192.168.1.34
192.168.1.36
192.168.1.41
Run Code Online (Sandbox Code Playgroud)

然后将其添加到我的.bash_alias文件中,然后获取它.

# This alias shows IPs on the local network
alias list-ip="nmap -sP 192.168.1.* | grep 192  | awk '{print $5}'"
Run Code Online (Sandbox Code Playgroud)

但是它产生了这个输出

Nmap scan report for 192.168.1.1
Nmap scan report for 192.168.1.33
Nmap scan report for 192.168.1.34
Nmap scan report for 192.168.1.36
Nmap scan report for 192.168.1.41
Run Code Online (Sandbox Code Playgroud)

我对我正在做的事情一无所知.我只是希望输出就像我在命令行上运行它,它应该是.

bash awk alias

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

在后台shell中分配变量

我想在后台分配一个变量,而我正在显示这样的进度指示器

sleep_echo(){
  sleep $1
  echo $2
}
show_ellapsed_time () {
  #...
}

(mivar=$(sleep_echo 3 hello)) &
show_ellapsed_time $!
echo $mivar
echo "after asignment"
Run Code Online (Sandbox Code Playgroud)

当我在终端中运行它时,它输出进度指示器,然后回声myvar

processing 00:00:03 [\]
hello
after asignment
Run Code Online (Sandbox Code Playgroud)

但是,当我在脚本中运行它时,分配不会发生

processing 00:00:03 [\]

after asignment
Run Code Online (Sandbox Code Playgroud)

为什么这会失败?是不是调用了te函数?

bash shell

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

C#多态和泛型类型匹配错误

我有一个类似的结构

enum AnimalType {dog, cat}

class Animal{}
class Dog : Animal {}
class Cat : Animal {}

class Vet<T> where T : Animal {}
class DogVet : Vet<Dog> {}
class CatVet : Vet<Cat> {}
Run Code Online (Sandbox Code Playgroud)

为什么我不能这样做呢?

...
Vet<Animal> myVet = new DogVet();
...
Run Code Online (Sandbox Code Playgroud)

为什么我不能添加这样的元素Dictionary

...
Dictionary<AnimalType, Vet<Animal>> _vetList = new Dictionary<AnimalType, Vet<Animal>>();
_vetList.Add(AnimalType.dog, new DogVet());
...
Run Code Online (Sandbox Code Playgroud)

该怎么做?

c# generics

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

自动增量字段不是SQL上的主键

我使用表格将一些ID转换为其他ID,反之亦然.

+--------+-----------------------------------------------+
| userId | visitorId                                     |
+--------+-----------------------------------------------+
|      1 | 015dc155838800b156253da37f0004078003b07000c48 |
|      2 | 015dbfdaeac50014c675298c726b04072003e06a00918 |
|      3 | 015dc26525ea00032eed79a4737d04092003a08a00978 |
+--------+-----------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

我这样做是使用此代码

CREATE TABLE Users (
    userId int PRIMARY KEY AUTO_INCREMENT,
    visitorId varchar(255),
);
Run Code Online (Sandbox Code Playgroud)

简单,不是吗?我注册了每个新访问者,并userId为她生成一个唯一的ID().但是如何避免插入两次相同的visitorId

INSERT INTO Users (visitorId) VALUE ("1234"), ("1234"), ("1234")
+--------+-----------+
| userId | visitorId |
+--------+-----------+
|      1 | 1234      |
|      2 | 1234      |
|      3 | 1234      |
+--------+-----------+
Run Code Online (Sandbox Code Playgroud)

mysql sql database

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