我正在尝试在Elixir中编写Amazon Product Advertising API客户端.开发人员指南描述了签署API请求的过程,其中必须使用请求和"秘密访问密钥"创建HMAC-SHA26哈希.这是我写的用来处理签名请求的函数:
defp sign_request(url) do
url_parts = URI.parse(url)
request = "GET\n" <> url_parts.host <> "\n" <> url_parts.path <> "\n" <> url_parts.query
url <> "&Signature=" <> :crypto.hmac(:sha256, 'ThisIsMySecretAccessKey', request)
end
Run Code Online (Sandbox Code Playgroud)
传递给函数的url看起来像这样: http://webservice.amazon.com/onca/xml?AssociateTag=ThisIsMyAssociateTag&AWSAccessKeyId=ThisIsMyAWSAccessKeyId&Keywords=stuff&Operation=ItemSearch&SearchIndex=Apparel&Service=AWSECommerceService&Timestamp=2014-11-22T12%3A00%3A00Z&Validate=True&Version=2013-08-01
我遇到的问题是,虽然:crypto.hmac/3返回二进制文件,但该二进制文件不是字符串; 通过返回值String.valid?/1回报false.因此,我无法将返回值连接到url字符串的末尾以对请求进行签名.
我使用:crypto.hmac/3不正确吗?有什么我想念的吗?我还有另一种方法吗?
BEA和M.的字母代表什么?我记得看到对首字母缩略词"BEAM"的解释,但我还没有找到它.
它出现在错误代码中:
? gentoo iex
Erlang/OTP 17 [erts-6.4.1] [source] [64-bit] [smp:8:8] [async-threads:10] [kernel-poll:false]
Interactive Elixir (1.0.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> import Math
08:05:02.839 [error] Loading of /var/opt/proj/elx/ubuntu/Elixir.Math.beam failed: :badfile
** (CompileError) iex:1: module Math is not loaded and could not be found
08:05:02.846 [error] beam/beam_load.c(1104): Error loading module 'Elixir.Math':
non-ascii garbage '78705400' instead of chunk type id
(elixir) src/elixir_exp.erl:123: :elixir_exp.expand/2
iex(1)>
Run Code Online (Sandbox Code Playgroud)
因此,看起来.beam文件存在某种问题,可能是由于我使用了vi.(注意Elixir程序员:不要编辑.beam文件,这很痛苦.)
这个问题解释了BEAM虚拟机是什么,而不是字母代表什么.似乎很难在Erlang中心快速找到关于词源的更多信息.据说 BEAM是Erlang和Elixir的秘诀.
我正在尝试以编程方式将预加载附加到我的某个具有has_many, through:关系的模型的查询中.
我的模块:
defmodule MyApp.Chemical do
use MyApp.Web, :model
schema "chemicals" do
has_many :company_chemicals, MyApp.CompanyChemical
has_many :companies, through: [:company_chemicals, :companies]
field :name, :string
end
def with_companies(query) do
from chem in query,
left_join: comp_chem in assoc(chem, :company_chemicals),
join: company in assoc(comp_chem, :company),
preload: [companies: company]
end
end
defmodule MyApp.Company do
use MyApp.Web, :model
schema "companies" do
has_many :company_chemicals, MyApp.CompanyChemical
has_many :chemicals, through: [:company_chemicals, :chemicals]
field :name, :string
end
end
defmodule MyApp.CompanyChemical do
use MyApp.Web, :model
schema "company_chemicals" do
belongs_to …Run Code Online (Sandbox Code Playgroud) 如何在Elixir中将二进制字符串转换为十六进制字符串,反之亦然?
对于其他"主流"语言,关于此主题,有一些关于SO的帖子.甚至还有一篇SO帖子对各种C#实现进行了基准测试
我们如何在长生不老药中做到这一点?
我的实施太难看了,不能分享...... :(
可以使用以下方法通过iex初始化Elixir节点:
iex --sname node1@10.99.1.50 --cookie foo
Run Code Online (Sandbox Code Playgroud)
然后另一个可以使用以下命令在REPL中连接此节点:
Node.connect(:"node1@10.99.1.50")
Run Code Online (Sandbox Code Playgroud)
似乎连接是通过TCP协议.但是,我没有在文档中找到一个参数来指定使用哪个端口.有没有人对此有任何想法?
我有团队,每个团队都有用户,因此有一个连接表将用户链接到团队,因为它有多对多关系,这是我的模型:
defmodule App.Team do
use App.Web, :model
schema "teams" do
field :owner_id, :integer
has_many :team_users, {"team_user", App.TeamUser}
end
end
defmodule App.User do
use App.Web, :model
schema "users" do
# field :email, :string
has_many :team_user, App.TeamUser
end
end
Run Code Online (Sandbox Code Playgroud)
这是连接模型:
defmodule App.TeamUser do
use App.Web, :model
@primary_key false
schema "team_user" do
belongs_to :user, App.User
belongs_to :team, App.Team
end
end
Run Code Online (Sandbox Code Playgroud)
如果我运行查询以获得所有结果团队用户的用户的所有团队,如下所示:
teams_users =
from(t in Team, where: t.owner_id == ^user_id)
|> Repo.all()
|> Repo.preload(:team_users)
Run Code Online (Sandbox Code Playgroud)
我得到这个日志:
[%App.Team{__meta__: #Ecto.Schema.Metadata<:loaded>, id: 1,
is_base_team: true, owner_id: 3, …Run Code Online (Sandbox Code Playgroud) 我想在自定义混合任务中通过Ecto显示来自我的数据库的数据.如何在我的任务中获得Ecto仓库(或启动它)?
我试过这样的东西,但它不起作用:
defmodule Mix.Tasks.Users.List do
use Mix.Task
use Mix.Config
use Ecto.Repo, otp_app: :app
@shortdoc "List active users"
@moduledoc """
List active users
"""
def run(_) do
import Ecto.Query, only: [from: 1]
Mix.shell.info "=== Active users ==="
query = from u in "users"
sync = all(query)
Enum.each(users, fn(s) -> IO.puts(u.name) end)
end
end
Run Code Online (Sandbox Code Playgroud)
当我启动mix users.list时,这会给我以下输出:
** (ArgumentError) repo Mix.Tasks.Users.List is not started, please ensure it is part of your supervision tree
lib/ecto/query/planner.ex:64: Ecto.Query.Planner.query_lookup/5
lib/ecto/query/planner.ex:48: Ecto.Query.Planner.query_with_cache/6
lib/ecto/repo/queryable.ex:119: Ecto.Repo.Queryable.execute/5
Run Code Online (Sandbox Code Playgroud)
有什么想法或其他方法来解决这个问题?
你是如何在Elixir中获得的?在JavaScript中(大多数语言都有等价的),我可以遍历列表中的各个项目并执行一些副作用,例如输出到控制台.
[1,2,3].forEach(function(num) {
console.log(num);
});
//=> 1
//=> 2
//=> 3
Run Code Online (Sandbox Code Playgroud)
在长生不老药中是否有相同的含量?
假设我在Elixir有一张地图:
m = %{"a"=>1, "b"=>2, "c" => 3}
Run Code Online (Sandbox Code Playgroud)
如果我打电话Map.values(m),我保证返回值总是按[1, 2, 3]顺序而不是说,[3, 1, 2]?
这是我从文档中不清楚的一件事.经过一些初步测试,我认为是.
同样,我怎样才能为"单一"UTF8字符打字?
在类型定义中,我可以使用泛型"any string"或"any utf8 string"
@type tile :: String.t # matches any string
@type tile :: <<_::8>> # matches any single byte
Run Code Online (Sandbox Code Playgroud)
但似乎我无法匹配第一位为0
@type tile :: <<0::1, _::7>>
Run Code Online (Sandbox Code Playgroud)
单个UTF比特序列的情况是
@type tile :: <<0::1, _::7>> |
<<6::3, _::5, 2::2, _::6>> |
<<14::4, _::4, 2::2, _::6, 2::2, _::6>> |
<<30::5, _::3, 2::2, _::6, 2::2, _::6, 2::2, _::6>>
Run Code Online (Sandbox Code Playgroud)
(例如,当使用模式匹配时,这些位模式匹配
<<14::4, _::4, 2::2, _::6, 2::2, _::6>> = "?"
Run Code Online (Sandbox Code Playgroud)
成功.)
但是当在typespecs中使用时,编译器会抱怨很多
== Compilation error in file lib/board.ex ==
** (ArgumentError) argument error
(elixir) lib/kernel/typespec.ex:1000: Kernel.Typespec.typespec/3 …Run Code Online (Sandbox Code Playgroud) elixir ×10
ecto ×2
erlang ×2
erlang-otp ×2
acronym ×1
architecture ×1
beam ×1
binary ×1
dialyzer ×1
elixir-mix ×1
foreach ×1
hexdump ×1
hmac ×1
javascript ×1
sha256 ×1
string ×1