Code Monkey home page Code Monkey logo

Comments (5)

theemaster avatar theemaster commented on May 14, 2024 1

My suggestion would probably be to change it inline as follows:

public static User From(Dictionary<string, object> map)
{
  return new User(
    id: map["$id"].ToString(), 
    createdAt: map["$createdAt"].ToString(), 
    updatedAt: map["$updatedAt"].ToString(), 
    name: map["name"].ToString(), 
    password: map.TryGetValue("password", out var password) ? password.ToString() : null,
    hash: map.TryGetValue("hash", out var hash) ? hash.ToString() : null,
    hashOptions: map.TryGetValue("hashOptions", out var hashOptions) ? hashOptions.ToString() : null, 
    registration: map["registration"].ToString(), 
    status: (bool)map["status"], 
    labels: ((JArray)map["labels"]).ToObject<List<object>>(), 
    passwordUpdate: map["passwordUpdate"].ToString(), 
    email: map["email"].ToString(), 
    phone: map["phone"].ToString(), 
    emailVerification: (bool)map["emailVerification"], 
    phoneVerification: (bool)map["phoneVerification"], 
    prefs: Preferences.From(((JObject)map["prefs"]).ToObject<Dictionary<string, object>>()),
    accessedAt: map["accessedAt"].ToString());
}

So we would not need to add an extra function. And I would use named parameters when creating the User-object. Having positional arguments is confusing if the list is so long and not readable. ;)

from sdk-for-dotnet.

stnguyen90 avatar stnguyen90 commented on May 14, 2024

Discussion on Discord: https://discord.com/channels/564160730845151244/564160731327758347/1192519235017113600

from sdk-for-dotnet.

joaquingrech avatar joaquingrech commented on May 14, 2024

My quick fix was this on User.cs. Open to better solution. I only identified as potential "nulls" password, hash and hashOptions. I don't know whether all other fields are always populated for every user account.

private static object? getFromMap(Dictionary<string, object> map, string key)
{
object? val;
map.TryGetValue("password",out val);
return val;
}
public static User From(Dictionary<string, object> map) {
return new User(
id: map["$id"].ToString(),
createdAt: map["$createdAt"].ToString(),
updatedAt: map["$updatedAt"].ToString(),
name: map["name"].ToString(),
password: getFromMap(map,"password")?.ToString(),
hash: getFromMap(map,"hash")?.ToString(),
hashOptions: getFromMap(map,"hashOptions")?.ToString(),
registration: map["registration"].ToString(),
status: (bool)map["status"],
labels: ((JArray)map["labels"]).ToObject<List>(),
passwordUpdate: map["passwordUpdate"].ToString(),
email: map["email"].ToString(),
phone: map["phone"].ToString(),
emailVerification: (bool)map["emailVerification"],
phoneVerification: (bool)map["phoneVerification"],
prefs: Preferences.From(map: ((JObject)map["prefs"]).ToObject<Dictionary<string, object>>()!),
accessedAt: map["accessedAt"].ToString()
);
}

from sdk-for-dotnet.

theemaster avatar theemaster commented on May 14, 2024

Thanks for your quick fix, but I think your getFromMap is not implemented as desired.
You implemented a hardcoded value "password" in TryGetValue. I guess instead it should be:

map.TryGetValue(key,out val);

A more compact version would be:

private static object? getFromMap(Dictionary<string, object> map, string key)
{
    return map.TryGetValue(key, out var val) ? val.toString() : null;
}

from sdk-for-dotnet.

joaquingrech avatar joaquingrech commented on May 14, 2024

Thanks for your quick fix, but I think your getFromMap is not implemented as desired. You implemented a hardcoded value "password" in TryGetValue. I guess instead it should be:

map.TryGetValue(key,out val);

A more compact version would be:

private static object? getFromMap(Dictionary<string, object> map, string key)
{
    return map.TryGetValue(key, out var val) ? val.toString() : null;
}

yep, I hardcoded for testing "password" and forgot to remove it. Glad there is a fix already on the way.

from sdk-for-dotnet.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.