小编ian*_*nks的帖子

在Typescript中创建通用方法装饰器

在这段代码中,我试图让用户为通用装饰器函数fetchJson指定一个类型.

它会像这样工作:

  1. 用以下方法装饰方法: @fetchJson<User>
  2. 然后我们用一个自动调用的函数替换该函数.then(res => res.json()),并返回一个包含在Promise中的类型值.

我遇到的问题是我不知道如何将返回分配给descriptor.value用户指定的T.是否有更好的方法来执行此操作?我觉得我完全错过了一些东西.

interface PromiseDescriptorValue<T>{
  (...args: any[]): Promise<T>;
}

const fetchJson = <T>(
  target: Object,
  propertyKey: string,
  descriptor: TypedPropertyDescriptor<PromiseDescriptorValue<Response>> // Response is a whatwg-fetch response -- https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/whatwg-fetch/whatwg-fetch.d.ts#L58
): TypedPropertyDescriptor<PromiseDescriptorValue<T>> => {
  const oldMethod = descriptor.value;

  descriptor.value = function(...args: any[]) {
    return oldMethod.apply(this, args).then((res: Response) => res.json());
  };

  return descriptor;
};


// TS2322: Type 'TypedPropertyDescriptor<PromiseDescriptorValue<Response>>'
// is not assignable to type
// 'TypedPropertyDescriptor<PromiseDescriptorValue<T>>'. Type
// 'PromiseDescriptorValue<Response>' is not assignable to type
// …
Run Code Online (Sandbox Code Playgroud)

generics types decorator typescript

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

来自MongoDB中值的动态键

说我有这个:

{
        "_id" : "ENVD",
        "years" : [
                {
                        "year" : "2013",
                        "avgInstructor" : 5.144999999999998
                },
                {
                        "year" : "2012",
                        "avgInstructor" : 5.194436090225564
                }
        ]
}
Run Code Online (Sandbox Code Playgroud)

我需要能够在2012-13中找到avgInstructor字段中的差异.我以为我可以用某种方式转换键$project,这将使年份成为关键,avgInstructor评级为值.所以它看起来像这样:

{
        "_id" : "ENVD",
        "years" : {
                        "2013" : 5.144999999999998,
                        "2012" : 5.194436090225564
         }

}
Run Code Online (Sandbox Code Playgroud)

这可能吗?请记住,我的主要目标是能够像这个伪代码一样运行减法:years['2013'].avgInstructor - years['2013'].avgInstructor.所以如果你看到一个更简单的方法,那也会很棒.我不确定在聚合管道的上下文中最好的方法.有人可以帮忙吗?

mongodb mongodb-query

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

为 FFI 返回具有生命周期的结构

我正在尝试将woothee-rust板条箱中的函数公开给 Ruby。为此,我正在解析输入字符串并尝试将结果作为 C 结构返回。我遇到了解析器的生命周期“活得不够长”的问题。我不确定为什么解析器的生命周期必须超过函数。

#![feature(libc)]
#![feature(cstr_to_str)]
#![feature(cstr_memory)]
extern crate libc;
extern crate woothee;

use woothee::parser::{Parser,WootheeResult};
use std::ffi::{CStr,CString};

#[no_mangle]
pub extern fn parse<'a>(ua_string: *const libc::c_char) -> WootheeResult<'a> {
    let input = unsafe { CStr::from_ptr(ua_string) };
    let parser = Parser::new();
    parser.parse(input.to_str().unwrap()).unwrap()
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

#![feature(libc)]
#![feature(cstr_to_str)]
#![feature(cstr_memory)]
extern crate libc;
extern crate woothee;

use woothee::parser::{Parser,WootheeResult};
use std::ffi::{CStr,CString};

#[no_mangle]
pub extern fn parse<'a>(ua_string: *const libc::c_char) -> WootheeResult<'a> {
    let input = unsafe { CStr::from_ptr(ua_string) };
    let parser = Parser::new();
    parser.parse(input.to_str().unwrap()).unwrap()
}
Run Code Online (Sandbox Code Playgroud)

ffi lifetime rust

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

防止neo4j在启动时启动(Ubuntu 14.04)

目前,每次启动Ubuntu时都会启动Neo4j服务.这是不受欢迎的行为,因为我使用它作为开发机器并不总是需要运行neo4j.

这是它的init.d脚本.你认为我应该编辑这个脚本以满足我的需求吗?我该怎么做呢?有更简单的替代方案吗?如果可能的话,我宁愿避免修改此代码.

#! /bin/sh

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Neo4j Graph Database"
NAME=neo4j
DAEMON=/var/lib/$NAME/bin/$NAME
DAEMON_ARGS="start"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME-service

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
. /lib/lsb/init-functions

do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running …
Run Code Online (Sandbox Code Playgroud)

ubuntu init neo4j

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

标签 统计

decorator ×1

ffi ×1

generics ×1

init ×1

lifetime ×1

mongodb ×1

mongodb-query ×1

neo4j ×1

rust ×1

types ×1

typescript ×1

ubuntu ×1