Coder Social home page Coder Social logo

muskanmahajan37 / cast Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leafpetersen/cast

0.0 0.0 0.0 6 KB

Dart library for type schemas for casting and parsing structured data

License: BSD 3-Clause "New" or "Revised" License

Dart 100.00%

cast's Introduction

This is a prototype of what a deep cast package might look like.

Usage

Simple schema

Simple schema can be defined and used like this:

  // Lists
  const schema = cast.List(cast.int);
  List<dynamic> someList = ....;
  // Inferred type is List<String>, runtime reified type is List<String>
  var typedList = schema.cast(someList);  
  // Maps
  const schema = cast.Map(cast.String, cast.int);
  Map<dynamic, dynamic> someMap = ....;
  // Inferred type is Map<String, int>, runtime reified type is Map<String, int>
  var typedMap = schema.cast(someMap);

Casts can be applied arbitrarily deep:

  // Maps
  const schema = cast.Map(cast.String, cast.List(cast.int));
  Map<dynamic, dynamic> someMap = ....;
  // Inferred type is Map<String, List<int>>, 
  // runtime reified type is Map<String, List<int>>
  var typedMap = schema.cast(someMap);
  // Inferred type is List<int>, 
  // runtime reified type is List<int>
  var typedList = typedMap["field"];

Structured schema

Complex schemas with different types for different fields can also be defined:

   const typed = cast.Keyed<String, dynamic>({
      "id": cast.int,
      "fields": cast.Keyed({"field1": cast.int, "field2": cast.int}),
      "extra1": cast.OneOf(cast.String, cast.List(cast.String)),
      "extra2": cast.OneOf(cast.String, cast.List(cast.String)),
    });

Given structured data matching that schema:

 var json = <String, dynamic>{
    "id": 0,
    "fields": <String, dynamic>{"field1": 1, "field2": 2},
    "extra1": "hello",
    "extra2": <dynamic>["hello"],
  };

Applying the cast gives back a deeply cast object of the right type:

Map<String, dynamic> result = typed.cast(json);
Map<String, int> fields = result["fields"]; // Implicit cast will not fail

cast's People

Contributors

leafpetersen avatar

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.