是否可以在不使用的情况下获取Google Cloud服务帐户的授权持有人令牌gcloud?
也就是说,我想提出一个HTTP请求(可能是我的JSON密钥文件以某种方式签名),这将为我提供相当于
gcloud auth application-default print-access-token
Run Code Online (Sandbox Code Playgroud)
此请求将在我自己的服务器上进行,我可能不希望在gcloud那里安装,并且我无法访问可能提供此内容的任何内部服务器元数据(例如,与计算引擎一样).
是否有一些oauth端点提供这样的东西?
或者,有没有办法生成长寿命令牌gcloud?
我是Google Cloud生态系统的新手,请原谅我的无知和术语......
Rust是否支持使用泛型返回类型的闭包?例如,我想写这样的东西:
let get<T: FromValue> = |s: &str| -> Option<T> { ... }
Run Code Online (Sandbox Code Playgroud)
但这种语法显然是错误的.
我想做什么
我正在使用rust-mysql-simple,我正在from_row为我的Userstruct 编写一个方法,从数据库行构建一个用户.
该库不提供(据我所知)一种按列名查找查询结果行值的方法.所以要解决这个问题,我的方法看起来像(这个编译并正常工作):
fn from_row(result: &QueryResult, row: Vec<Value>) -> User {
let mut map: HashMap<_, _> = row.into_iter().enumerate().collect();
let mut get = |s: &str| {
result.column_index(s)
.and_then(|i| map.remove(&i) )
};
User {
id: get("id").and_then(|x| from_value_opt(x).ok() )
}
}
Run Code Online (Sandbox Code Playgroud)
这result是一个对象,它包含有关查询列名的信息(用于查找列名的列索引),并row包含查询结果行中的有序值.from_value_opt是一个由库提供的方法,它接受Value并返回一个Result<T, MyError>.该值被强制转换为字段的类型.
我试图移动.and_then(|x| from_value_opt(x).ok() )到get关闭只是为了清理代码一些.但是,当我这样做时,闭包返回类型被解释为第一次出现get调用的结果.
我将闭包重写为嵌套方法,如下所示: …
我有一个项目,它具有依赖于铁 的依赖项(cookie实用程序)>= 0.3, <= 0.4.
我的项目依赖于铁0.3(所以我可以使用router尚未更新到最新铁的中间件).
当我尝试编译我的项目时,cookie实用程序会拉出0.4铁的版本,并且由于使用了不同版本的铁,我会收到错误.
但是,我可以这样做:
cargo update -p <cookie utility>
Run Code Online (Sandbox Code Playgroud)
它(通常)改变了包对铁的依赖性以匹配我正在使用的铁,并消除了对铁的无关依赖0.4.(奇怪的是,我有时必须在更新前多次运行该命令.)
显然我无法指定依赖项的依赖项版本:在Cargo.toml或Cargo.lock中设置项目依赖项的依赖项的特定版本.
如果货物可以猜到我想要使用单一版本的铁,那将是很好的,但我明白为什么它不能.但是,我对为什么cargo update -p <package>实际起作用感到困惑; 它似乎不直观,它会更新包的依赖关系.
我想我的第一个真正的问题是:如何指定依赖项的依赖项版本(当且仅当我想要的版本在该库的受支持版本范围内时)?我不认为上面提到的问题中提出的解决方案是理想的.我觉得如果Cargo能够很好地支持这一点会更好,因此库可以将其依赖版本范围保持为功能允许的开放状态.
与此同时,我发现这个似乎做我想要的"技巧"(cargo update -p <pkg>).我没有看起来超级难,但这种行为似乎没有在任何明显的地方描述.我的第二个问题是:这是一种合并依赖关系的有效方法吗?有什么地方我可以找到更多相关信息吗?
并重现的步骤:
cargo new --bin ironapp; cd ironapp.cargo new cookie_util.cookie_util/Cargo.toml添加一个依赖性:iron = ">= 0.3, <= 0.4".Cargo.toml添加两个依赖关系:iron = "0.3.0"和cookie_util = { path = "cookie_util"} …阅读"大"文件(可能是文本或二进制文件)而不进入unsafe领域的最有效的通用方法是什么?当我在网上搜索"大块读取大块文件"时,我感到很惊讶.
例如,我的一个用例是使用rust-crypto(该Md5模块允许您&[u8]迭代地添加块)来计算文件的MD5校验和.
这就是我所拥有的,它似乎比其他一些方法表现得稍好一些read_to_end:
use std::{
fs::File,
io::{self, BufRead, BufReader},
};
fn main() -> io::Result<()> {
const CAP: usize = 1024 * 128;
let file = File::open("my.file")?;
let mut reader = BufReader::with_capacity(CAP, file);
loop {
let length = {
let buffer = reader.fill_buf()?;
// do stuff with buffer here
buffer.len()
};
if length == 0 {
break;
}
reader.consume(length);
}
Ok(())
}
Run Code Online (Sandbox Code Playgroud) 我有三个相关的模型,如下所示:
class Product < ActiveRecord::Base
belongs_to :user
has_many :descriptions, {
dependent: :destroy,
before_add: [:add_user_id_to_description, :validate_description]
}
has_many :documents, through: :descriptions
# ...
def validate_description(d)
unless d.valid?
d.errors[:user_id].each do |err|
self.errors.add(:base, "Doc error: #{err}")
end
end
end
end
class Document < ActiveRecord::Base
belongs_to :user
has_many :descriptions, {
dependent: :destroy,
before_add: [:add_user_id_to_description, :validate_description]
}
has_many :products, through: :descriptions
end
class Description < ActiveRecord::Base
belongs_to :user
belongs_to :product
belongs_to :document
end
Run Code Online (Sandbox Code Playgroud)
当我做的事情:
doc = user.documents.build
doc.update_attributes(:product_ids => [1,2])
Run Code Online (Sandbox Code Playgroud)
而description验证失败,然后我得到false,并在适当的错误 …
ruby activerecord ruby-on-rails has-many-through ruby-on-rails-3
如何在我的Maven项目中运行Kotlin REPL?
这有效,但很难看:
kotlinc-jvm -cp target/classes/:`ruby -e "puts Dir['target/**/*.jar'].join(':')"`
Run Code Online (Sandbox Code Playgroud)
我在下面尝试了不同的变体(在使用Maven将编译器JAR复制为依赖项之后),但没有任何作用(Error: Could not find or load main class org.jetbrains.kotlin.runner.Main):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>-classpath</argument>
<argument>${project.basedir}/target/dependency/kotlin-compiler-1.0.0.jar</argument>
<argument>org.jetbrains.kotlin.runner.Main</argument>
</arguments>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud) 有没有办法将方法的值添加到serdestruct派生时的序列化输出Serialize?我正在寻找像"虚拟领域"这样的东西.
我知道我可以定义自己的Serializer/ Visitor或serde_json::builder用来获得一个Value,我只是想先检查是否有任何方法可以使用serde_macro魔法来做到这一点.
要明确我想要这样的东西:
#[derive(Serialize, Deserialize, Debug)]
struct Foo {
bar: String,
#[serde(call="Foo::baz")]
baz: i32 // but this is not a real field
}
impl Foo {
fn baz(&self) -> i32 { self.bar.len() as i32 }
}
Run Code Online (Sandbox Code Playgroud) 在我的一些Rails应用程序中,我的ActiveRecord模型似乎在初始化时建立数据库连接(例如,当我这样做时rails console),而在其他应用程序中,似乎只有在我引用模型类或实例化模型对象时才建立连接.
例如,我只去了一个应用程序,打开了Rails控制台并写道:
SomeModel.connected?
Run Code Online (Sandbox Code Playgroud)
它又归来了false.我去了另一个应用程序,输入了相同的命令(对于不同的模型),然后返回true.我去了第三个应用程序并输入了相同的命令.这一次,它等了片刻然后返回true,这让我觉得该connected?方法由于某种原因触发了连接.
这种行为差异似乎与Rails版本或模型的内容无关.这可能是我在初始化程序中所做的奇怪事情,但我不这么认为.
那么Rails何时建立连接?或者预期的行为是什么?
附加信息
我将补充说,它似乎不会connected?返回false,因为Rails 无法连接到数据库.
例如,在我的第一个应用程序中,我做了:
SomeModel.connected?
# => false
SomeModel.table_exists? # or any other command that makes Rails look at db
# => true
SomeModel.connected?
# => true
Run Code Online (Sandbox Code Playgroud) 我无法预先确定如何消除使用相同谓词的资源的歧义.我是RDF的新手,所以请原谅我的术语:我会试着用例子来解释我的意思.
我有一个Interview资源/模型与(简化)上下文,如下所示:
{
"id": {
"@id": "http://purl.org/dc/terms/identifier"
},
"interviewers": {
"@id": "http://purl.org/dc/terms/contributor",
"@type": "@id",
"@container": "@set"
},
"title": {
"@id": "http://purl.org/dc/terms/title"
},
"interviewees": {
"@id": "http://purl.org/dc/terms/contributor",
"@type": "@id",
"@container": "@set"
}
}
Run Code Online (Sandbox Code Playgroud)
我Interviewer和Interviewee资源有这样的情境:
{
"id": {
"@id": "http://purl.org/dc/terms/identifier"
},
"name": {
"@id": "info:repository/ive/name"
}
}
Run Code Online (Sandbox Code Playgroud)
然后我创建一个如下所示的资源:
{
"id": "06bad25f-83c1-4ee5-b055-0cb87d4c06be",
"interviewers": [
{
"id": "b0c262ce-7eb3-47f2-b212-a0e71cca0c92",
"name": "Somebody",
"@context": {
...
},
"@id": "urn:uuid:b0c262ce-7eb3-47f2-b212-a0e71cca0c92",
"@type": [
"http://id.loc.gov/vocabulary/relators/ivr"
]
}
],
"title": "Interview with So and So", …Run Code Online (Sandbox Code Playgroud) 我有两个AR模型和第三个has_many :through连接模型,如下所示:
class User < ActiveRecord::Base
has_many :ratings
has_many :movies, through: :ratings
end
class Movie < ActiveRecord::Base
has_many :ratings
has_many :users, through: :ratings
end
class Rating < ActiveRecord::Base
belongs_to :user
belongs_to :movie
after_destroy do
puts 'destroyed'
end
end
Run Code Online (Sandbox Code Playgroud)
有时,用户会想要直接放弃电影(不直接破坏评级).但是,当我这样做时:
# puts user.movie_ids
# => [1,2,3]
user.movie_ids = [1, 2]
Run Code Online (Sandbox Code Playgroud)
after_destroy虽然正确删除了连接记录,但未调用rating的回调.如果我像这样修改我的用户模型:
class User < ActiveRecord::Base
has_many :ratings
has_many :movies,
through: :ratings,
before_remove: proc { |u, m| Rating.where(movie: m, user: u).destroy_all }
end
Run Code Online (Sandbox Code Playgroud)
一切正常,但这真的很难看,然后Rails再次尝试删除连接模型.
我如何使用dependent: :destroy此协会的策略,而不是dependent: …
rust ×4
activerecord ×2
ruby ×2
file ×1
gcloud ×1
io ×1
json-ld ×1
kotlin ×1
rdf ×1
rust-cargo ×1