序列化F#函数(Serializing an F# function)
使用F#我有一些配置,我想使用Newtonsoft.Json序列化到磁盘。 配置数据是记录类型。 记录类型中的一个字段是string-> bool类型的函数,用于将字符串与给定值进行比较。 例如
(fun (x:string) -> x = "1")
序列化类型成功但字段未成功存储(记录为“{}”)因此反序列化失败。 这似乎是设计上的。
如何存储函数,以便在反序列化时仍可用于从磁盘填充记录类型并仍可执行?
我把引文看作是将表达式存储为数据的一种方法,但我不确定这是否可行。 如果是,那么我正在努力让这个工作。
如何实现我的需求?
更新;
type Logic = string -> bool
记录类型存储配置;
type Exclusion = | FromDataValue of QItem * Logic * ExcludedItems | FromDataValuesAnd of (QItem * Logic) seq * ExcludedItems | FromDataValuesOr of (QItem * Logic) seq seq * ExcludedItems
“排除”记录由用户填充,并针对某些数据集运行,不包括返回集合中的项目。 QItem表示应用Logic函数的数据集中的字符串,如果返回true,则ExcludedItems中的项目将从结果中排除。
最初,配置是由解决方案中的用户创建的,所以一切正常。 但是,一旦创建了配置,我希望能够将配置保存到光盘,以便在需要时可以加载并再次运行。
所以我需要能够存储它,但我希望能够将其存储为字符串,然后在加载时再次运行它。
Using F# I've some configuration that I want to serialize to disk using Newtonsoft.Json. The config data is a record type. One of the fields in the record type is a function of type string->bool and is used to compare a string to a given value. e.g.
(fun (x:string) -> x = "1")
Serializing the type succeeds but the field isn't successfully stored (it is recorded as '{}') and therefore deserializing fails. This seems to be by design.
How do I store a function so that it can be used to populate the record type from disk when deserialized and still be executable?
I've looked at quotations as a means of storing expressions as data but I'm not sure if this is the way to go. If it is then I'm struggling to get this working.
How can achieve what I need?
Update;
type Logic = string -> bool
The record type storing config;
type Exclusion = | FromDataValue of QItem * Logic * ExcludedItems | FromDataValuesAnd of (QItem * Logic) seq * ExcludedItems | FromDataValuesOr of (QItem * Logic) seq seq * ExcludedItems
The Exclusion record is populated by a user and runs against some data sets excluding items from the return collection. The QItem represents a string in the dataset that the Logic function is applied to, and if returns true the items in ExcludedItems are excluded from the results.
Initially the config is created by a user within the solution so all works fine. However once a config is created I want to be able to save the config to disc so that it can be loaded and run again when required.
So I need to be able to store this, but I want to be able to store it as a string then run it again when loaded.
原文:https://stackoverflow.com/questions/51540040
最满意答案
passport.authenticate
构建一个必须传递给app.get
的连接中间件 。 这意味着loginCtrl
必须返回它。app.get("/login", authController.loginCtrl()) loginCtrl(){ let passportConfig = new PassportConfig(); var config = passportConfig.config; return passport.authenticate(config.passport.strategy, { successRedirect: "/", failureRedirect: "/login" }); }
passport.authenticate
builds a connect middleware that must be passed toapp.get
. It meansloginCtrl
must return it.app.get("/login", authController.loginCtrl()) loginCtrl(){ let passportConfig = new PassportConfig(); var config = passportConfig.config; return passport.authenticate(config.passport.strategy, { successRedirect: "/", failureRedirect: "/login" }); }
相关问答
更多-
所以经过大量的时间调试并得到一些好的帮助,我弄清楚是什么导致了我的问题,实际上有三个错误。 首先,在Facebook战略中,我应该如何构建它: passport.use(new FacebookStrategy({ //Information stored on config/auth.js clientID: configAuth.facebookAuth.clientID, clientSecret: configAuth.facebookAuth.clientSecret, ...
-
在函数内包装passport.authenticate不起作用(Wrapping passport.authenticate inside a function doesn't work)[2022-04-05]
试试这个,让我们知道它是否有效。 (你必须在函数结尾提供(res,req,next),如本链接http://passportjs.org/docs所述 ) server.get('/user/google', function(req, res, next) { passport.authenticate('google', { scope: ['openid email profile'] } ,function(err, user, info){ res.send(us ... -
passport.authenticate与nodejs无法正常工作(passport.authenticate not working properly with nodejs)[2022-02-17]
passport.authenticate构建一个必须传递给app.get的连接中间件 。 这意味着loginCtrl必须返回它。 app.get("/login", authController.loginCtrl()) loginCtrl(){ let passportConfig = new PassportConfig(); var config = passportConfig.config; return passport.authenticate(config.pa ... -
var passport = require('passport'); var LocalStrategy = require('passport-local').Strategy; passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' }, function(email, password, next) { //do what you want here ...
-
这可能是因为您在应用中注册了哪个网站。 如果您已将其注册到http:// localhost:3000 / ,则应使用域localhost进行回调。 如果您已将其注册为http://127.0.0.1:3000/,那么您应该使用127 ... It could be because of what site you have registered in your app. If you have registered it to http://localhost:3000/ you should work ...
-
我能够通过更新xml2js包来解决这个问题,不幸的是,我不完全确定它为什么不起作用。 I was able to resolve this issue by updating the xml2js package, unfortunately, I'm not entirely sure why it wasn't working.
-
NodeJS,passport-jwt:验证除列表以外的所有用户(NodeJS, passport-jwt: Authenticate all user except in list)[2022-03-14]
实际上,你甚至不需要express-unless你可以使用表达允许注册中间件的事实,这些中间件可以随时执行来完成你的过滤 const express = require('express'); const app = express(); function authenticateSomeRoutesMiddleware(req, res, next) { if (/(login|register)/.test(req.originalUrl)) { // No authenti ... -
只要将任何其他疲倦的程序员接到此处即可。 function __promisifiedPassportAuthentication() { return new Promise((resolve, reject) => { passport.authenticate('local', (err, user, info) => { ... })(req, res) // <-- that guy right there } } Just ...
-
您可以添加在护照操作之前运行的中间件 router.post("/login", usernameToLowerCase, passport.authenticate("local", { successRedirect: "/", failureRedirect: "/login" }), function (req, res) { }); function usernameToLowerCase(req, res, next){ req.body.username = req. ...
-
经过这么多的发现,Oww gosh得到了答案。 问题在于依赖关系。 koa-passport是用@next标签安装的,即npm install koa-passport@next而不是npm install koa-passport@latest Oww gosh, after so much of finding, got the answer. The issue was with the dependencies. koa-passport was to be installed with @next ...