Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mappify Map from Object like JSON.stringify #11461

Closed
DartBot opened this issue Jun 23, 2013 · 4 comments
Closed

Mappify Map from Object like JSON.stringify #11461

DartBot opened this issue Jun 23, 2013 · 4 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Jun 23, 2013

This issue was originally filed by @DuVo


It should be usefull if we can extract an embedded maps view from an object, like JSON.stringify can do, but with Map.

For example, a num, String, bool, null will return itself.
An objects list or map return a list or a map of mappified object.

@DartBot
Copy link
Author

DartBot commented Jun 23, 2013

This comment was originally written by @DuVo


Here is my basic implementation. I didn't used it a lot... so many bugs can be still present :

dynamic mappify(dynamic object) {
  List seen = [];

  void checkCycle(final object) {
    for (var item in seen) {
      if (identical(item, object)) {
        throw new MapperCyclicException(object);
      }
    }
    seen.add(object);
  }

  dynamic mapFromObject(dynamic object) {
    if (object is num || object is String || object is bool || object == null) {
      return object;
    }else if (object is Map) {
      checkCycle(object);
      Map map = {};
      object.forEach((key, value) {
        map[key.toString()] = mapFromObject(value);
      });
      seen.remove(object);
      return map;
    } else if (object is Iterable) {
      checkCycle(object);
      var tmp = new List.generate(object.length, (i) => mapFromObject(object[i]), growable: false);
      seen.remove(object);
      return tmp;
    } else {
      checkCycle(object);
      Map map = {};
      InstanceMirror instanceMirror = reflect(object);
      ClassMirror classMirror = instanceMirror.type;
      for(Symbol key in classMirror.variables.keys) {
        map[MirrorSystem.getName(key)] = mapFromObject(instanceMirror.getField(key).reflectee);
      }
      for(Symbol key in classMirror.getters.keys) {
        map[MirrorSystem.getName(key)] = mapFromObject(instanceMirror.getField(key).reflectee);
      }
      seen.remove(object);
      return map;
    }
  }

  return mapFromObject(object);
}

@lrhn
Copy link
Member

lrhn commented Jun 24, 2013

While this is definitely possible using mirrors, as you have shown, I don't think it's something we will want in the core libraries.
A Map and a non-Map object are different things (unlike in JavaScript), and treating an plain object as a Map is really hiding a lot of mirror operations.

Seems like an obvious choice for a package, but not something I think fits in the core library.


Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Jun 24, 2013

This comment was originally written by @DuVo


I'm not necessarily talking about adding in the core. And I am aware that a Map can't handle all information from a mirrors. But when I thought about it, it's really like the stringify method, from json.

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Jun 24, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed priority-unassigned labels Feb 29, 2016
@lrhn lrhn added core-m and removed core-m labels Aug 11, 2017
@floitschG floitschG added core-n and removed core-m labels Aug 29, 2017
@natebosch
Copy link
Member

Mirrors are no longer supported on the web.

For both web and VM our current recommendation is to use codegen. I think there are also a couple VM libraries that do this type of thing with mirrors but it's not something we plan to support in the SDK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. core-n type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants