How to specify HashMapping when using Hocon - f#

Given the HOCON below for a consitent-hashing-poolrouter, how do I specify the hashMapping.
akka {
actor {
serializers {
wire = "Akka.Serialization.WireSerializer, Akka.Serialization.Wire"
}
serialization-bindings {
"System.Object" = wire
}
deployment {
/data-collector {
router = consistent-hashing-pool
nr-of-instances = 10
}
}
}
loggers = ["Akka.Logger.NLog.NLogLogger,Akka.Logger.NLog"]
}
Given
let config = Configuration.load()
let systemName = config.GetString("app-config.actorsystem", "my-system")
let system = System.create systemName config
let collectorRouter =
let hashMapping (msg:obj) =
match msg with
| :? Message as msg ->
match msg with
| Parse (_, req) -> req.uniqueId |> box
| _ -> "-" |> box
ConsistentHashingPool (10, ConsistentHashMapping hashMapping)
let dataCollectorProps =
{ props (dataCollector settings.collector) with
Router = Some (collectorRouter :> RouterConfig)} //(FromConfig.Instance.WithFallback collectorRouter)
let test = spawn system "data-collector" <| dataCollectorProps
Router = Some (collectorRouter :> RouterConfig) work
Router = Some (FromConfig.Instance.WithFallback collectorRouter) doesn't
What is the correct way to specify the hashMapping function?
Edit 1
The warning from the console is
Akka.Routing.ConsistentHashingRoutingLogic|Message [Message] must be handled by hashMapping, or implement [IConsistentHashable] or be wrapped in [ConsistentHashableEnvelope]

There are three ways of specifying the hash key. My favourite is to implement IConsistentHashable and return the key (based on some properties of the message) from the ConsistentHashKey property.
Example excerpt from my Consistent Hashing article (in C#; sorry I don't know F#):
public class CurrencyPriceChangeMessage : IConsistentHashable
{
public string CurrencyPair { get; }
public decimal Price { get; }
public object ConsistentHashKey
{
get
{
return this.CurrencyPair;
}
}
The hash key will be used to route messages with the same key always to the same routee.

Related

Map generic type to record using pattern matching

I've a Union type like this:
type AccountOpenened =
{ Owner: string
AccountId: Guid
CreatedAt: DateTimeOffset
StartingBalance: decimal }
type AccountClosed = { AccountId: Guid }
type AccountEvent = AccountOpenend | AccountClosed
I also have a 3rd party library from which I can get IReadOnlyList<IEvent> whereas IEvent as this signature:
public interface IEvent {
object Data { get; }
}
The library also provides a generic class Event<T> which has this signature:
public class Event<T> : IEvent {
public T Data { get; set; }
}
What I'm trying to achieve is to map an instance of IReadOnlyList<IEvent> to an AccountEvent list using exhaustive pattern matching.
I tried something like this:
let map (input: IEvent): AccountEvent =
match input with
| :? (Event<AccountOpened>) as e ->
{ Owner = e.Data.Owner
AccountId = e.Data.AccountId
CreatedAt = e.Data.CreatedAt
StartingBalance = e.Data.StartingBalance }
| :? (Event<AccountClosed>) as e ->
{ AccountId = e.Data.AccountId }
| _ -> failwith "Unknown Event"
This doesn't work because I get this compiler error:
This expression was expected to have type 'AccountEvent' but here has type 'AccountCreation'
What am I doing wrong here?

F# assign value to a method argument

I am trying to override a method provided by an interface/API and have to assign a new value to one of the method's argument. If I try to assign to the passed argument, it'll give an error.
override _.Emit(eventInfo:SequenceStartEventInfo, emitter:IEmitter) =
eventInfo <- SequenceStartEventInfo(eventInfo.Source)
[...]
I am looking for behavior matching the following C# code:
public override void Emit(SequenceStartEventInfo eventInfo, IEmitter emitter) {
eventInfo = new SequenceStartEventInfo(eventInfo.Source)
...
}
If I change it and try to pass by reference (eventInfo:byref<SequenceStartEventInfo>) then it'll no longer match the available overloads.
The F# language reference on parameters and methods doesn't provide any help when dealing with this specific case. What is the best way to handle this Scenario?
In C# you can assign a new value to the argument variable, but as it is not passed by reference, this won't change the variable of the caller (even though the passed object is a reference type):
public static void Main()
{
var rt = new RefType { Value = 3 };
Change(rt);
Console.WriteLine(rt.Value); // still 3
}
public class RefType {
public int Value { get; set; }
}
public static void Change(RefType notByRef){
notByRef = new RefType { Value = 42 };
}
the F# equivalent (where parameters are immutable) would be shadowing:
type RefType() =
member val Value = 0 with get, set
let Change notByRef =
let notByRef = RefType(Value = 42);
// now, `notByRef` hides the method parameter
()
let [<EntryPoint>] Main _ =
let rt = RefType(Value = 3);
Change(rt);
printfn "%i" rt.Value // still 3
0

How do I use `member val` in object expressions in fsharp?

I have an interface in F# (generated from some typescript) that looks like this:
type [<AllowNullLiteral>] AppOptions =
abstract credential: Admin.Credential.Credential option with get, set
abstract databaseAuthVariableOverride: Object option with get, set
abstract databaseURL: string option with get, set
abstract storageBucket: string option with get, set
abstract projectId: string option with get, set
I thought that the easiest way to create an instance of that type would be to create a simple object expression, but I can't get it to work. Isn't member val allowed in object expression? If not, what is the easiest way to create a simple instance of an interface with getters and setters on all properties? This is what I tried that didn't work:
let options =
{ new Admin.AppOptions with
member val credential: Admin.Credential.Credential option = None with get, set
member val databaseAuthVariableOverride: obj option = None with get, set
member val databaseURL: string option = None with get, set
member val databaseURL: string option = None with get, set
member val projectId: string option = None with get, set
}
I dont think member var is in the object expression spec only the long form properties like this are allowed:
let options =
let mutable credential = None
let mutable databaseAuthVariableOverride = None
let mutable databaseURL = None
let mutable storageBucket = None
let mutable projectId = None
{ new AppOptions with
member x.credential with get() = credential and set v = credential <- v
member x.databaseAuthVariableOverride with get() = databaseAuthVariableOverride and set v = databaseAuthVariableOverride <- v
member x.databaseURL with get() = databaseURL and set v = databaseURL <- v
member x.storageBucket with get() = storageBucket and set v = storageBucket <- v
member x.projectId with get() = projectId and set v = projectId <- v
}
Which would project C# like code by the compiler like this:
[assembly: FSharpInterfaceDataVersion(2, 0, 0)]
[assembly: AssemblyVersion("0.0.0.0")]
[CompilationMapping(SourceConstructFlags.Module)]
public static class _
{
[Serializable]
[AllowNullLiteral]
[CompilationMapping(SourceConstructFlags.ObjectType)]
public interface AppOptions
{
override FSharpOption<object> credential
{
get;
set;
}
override FSharpOption<object> databaseAuthVariableOverride
{
get;
set;
}
override FSharpOption<string> databaseURL
{
get;
set;
}
override FSharpOption<string> storageBucket
{
get;
set;
}
override FSharpOption<string> projectId
{
get;
set;
}
}
See sharplab
A normal type is probably more succinct here:
type t() =
interface AppOptions with
member val credential = None with get, set
member val databaseAuthVariableOverride = None with get, set
member val databaseURL = None with get, set
member val storageBucket = None with get, set
member val projectId = None with get, set

Wildcard "use" binding works in sequence expression, but not otherwise

Using the wildcard pattern with use works within a sequence expression, but not otherwise. Is there a reason for this?
let mkDisposable() =
{ new IDisposable with
member __.Dispose() = () }
let mkSeq() =
seq {
use _ = mkDisposable() //OK
()
}
let f() =
use _ = mkDisposable() //ERROR: 'use' bindings must be of the form 'use <var> = <expr>'
()
I believe that this is a natural (but unexpected) consequence of the desugaring of computation expressions (a sequence expression in this case, but this behavior applies to all computation expressions). As the spec indicates,
use pat = expr
cexpr
is translated to
Using(expr, fun pat -> cepxr)
Because this is a shallow syntactic translation, you can use any pattern that could be used when writing a function, including just _. However, for normal use bindings, the left hand side of the binding must be an identifier, not a pattern (see section 6.6.3 of the spec).
I've done a bit of digging and it seems that the special way seq expressions are handled changes the rules of use. The seq expression is in fact transformed into the following with an IDisposable field that is disposed upon completion of the sequence.
internal sealed class mkSeq#11<a> : GeneratedSequenceBase<a>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never), CompilerGenerated, DebuggerNonUserCode]
public IDisposable matchValue = matchValue;
[DebuggerBrowsable(DebuggerBrowsableState.Never), CompilerGenerated, DebuggerNonUserCode]
public int pc = pc;
[DebuggerBrowsable(DebuggerBrowsableState.Never), CompilerGenerated, DebuggerNonUserCode]
public a current = current;
public mkSeq#11(IDisposable matchValue, int pc, a current)
{
}
public override int GenerateNext(ref IEnumerable<a> next)
{
switch (this.pc)
{
case 2:
{
break;
}
case 3:
{
goto IL_55;
}
default:
{
this.matchValue = Program.mkDisposable();
this.pc = 2;
break;
}
}
this.pc = 3;
LanguagePrimitives.IntrinsicFunctions.Dispose<IDisposable>(this.matchValue);
this.matchValue = null;
this.pc = 3;
IL_55:
this.current = default(a);
return 0;
}
public override void Close()
{
switch (this.pc)
{
case 1:
{
goto IL_41;
}
case 3:
{
goto IL_41;
}
}
this.pc = 3;
LanguagePrimitives.IntrinsicFunctions.Dispose<IDisposable>(this.matchValue);
IL_41:
this.pc = 3;
this.current = default(a);
}
public override bool get_CheckClose()
{
switch (this.pc)
{
case 1:
{
return false;
}
case 3:
{
return false;
}
}
return true;
}
[CompilerGenerated, DebuggerNonUserCode]
public override a get_LastGenerated()
{
return this.current;
}
[CompilerGenerated, DebuggerNonUserCode]
public override IEnumerator<a> GetFreshEnumerator()
{
return new Program<a>.mkSeq#11(null, 0, default(a));
}
}
Normally use is transformed into this:
IDisposable e = Program.mkDisposable();
try
{
}
finally
{
IDisposable disposable = e as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
Without a variable name the compiler will ignore the result of the expression and thus it cannot be disposed. To be honest it seems like a special case should be made for use so all the boilerplate is created behind the scenes like we see in seq expressions.

How can I override the 'map' constructor in a Grails domain class?

I need to perform some initialization when new instances of my domain class are created.
class ActivationToken {
String foo
String bar
}
When I do this I want bar to be initialized by code inside ActivationToken:
def tok = new ActivationToken(foo:'a')
I cannot see how to 'override' the 'constructor' to make this happen. I know in this case I could just add a normal constructor but this is just a simple example.
The map constructor is coming from Groovy - not Grails in this case. I did some experimentation, and this is what I came up with:
class Foo {
String name = "bob"
int num = 0
public Foo() {
this([:])
}
public Foo(Map map) {
map?.each { k, v -> this[k] = v }
name = name.toUpperCase()
}
public String toString() {
"$name=$num"
}
}
assert 'BOB=0' == new Foo().toString()
assert 'JOE=32' == new Foo(name:"joe", num: 32).toString()
Basically, it appears that you'll have to manually override the constructors if you need to process the property after construction.
Alternately, you can override individual setters, which is cleaner and safer in general:
class Foo {
String name = "bob"
int num = 0
public void setName(n) {
name = n.toUpperCase()
}
public String toString() {
"$name=$num"
}
}
assert 'bob=0' == new Foo().toString()
assert 'JOE=32' == new Foo(name:"joe", num: 32).toString()
Note that the default value isn't processed, but that should be OK in most instances.
The solution above is also good for cases where initializing an object from parameters in a web request, for example, where you wish to ignore extraneous values, catching Missing property exceptions.
public Foo(Map map) {
try {
map?.each { k, v -> this[k] = v }
}
catch(Exception e){
}
}

Resources