Git Product home page Git Product logo

lofi-schema-elm's Introduction

#lofi #schema in Elm

Low fidelity schemas. Write with a friendly syntax and convert to other forms easily.

Interactive demo

I’ve made an online tool to convert #lofi schemas. Convert to React PropTypes, Mongoose schemas, Joi validations, and MySQL commands.

Basic usage

import Html exposing (Html, section, h2, pre, text)
import Lofi.Parse exposing (parseElement)
import Lofi.Schema exposing (Schema, fromElement)
import Lofi.Schema.Output.MySQL as MySQL
import Lofi.Schema.Output.Mongoose as Mongoose
import Lofi.Schema.Output.Joi as Joi
import Lofi.Schema.Output.ReactProps as ReactProps
import Lofi.Schema.Output.Swift as Swift
import Lofi.Schema.Output.Elm as Elm
import Lofi.Schema.Output.Go as Go

type alias Model =
  { collectionName : String
  , individualName : String
  , lines : List String
  }

model : Model
model =
  { collectionName = "Users"
  , individualName = "User"
  , lines =
      [ "First name"
      , "Middle name #optional"
      , "Last name #text #max: 255"
      , "Email #email"
      , "Date of birth #date"
      , "Last signed in at #time #now"
      , "Favorite number #number #default: 7"
      ]
  }

view : Model -> Html Msg
view model =
  let
    elements =
      List.map parseElement model.lines

    schema : Schema
    schema =
      { collectionName = model.collectionName
      , individualName = model.individualName
      , items = List.map fromElement elements
      }
  in
    section []
    [ article []
      [ h2 [] [ text "React PropTypes" ]
      , pre [] [ text (ReactProps.createPropTypesCode schema) ]
      ]
    , article []
      [ h2 [] [ text "Mongoose" ]
      , pre [] [ text (Mongoose.createModelCode schema) ]
      ]
    , article []
      [ h2 [] [ text "Swift" ]
      , pre [] [ text (Swift.createStructCode schema) ]
      ]
    , article []
      [ h2 [] [ text "Elm" ]
      , pre [] [ text (Elm.createTypeAliasCode schema) ]
      ]
    , article []
      [ h2 [] [ text "Golang" ]
      , pre [] [ text (Go.createStructCode schema) ]
      ]
    , article []
      [ h2 [] [ text "Joi" ]
      , pre [] [ text (Joi.createSchemaCode schema) ]
      ]
    , article []
      [ h2 [] [ text "MySQL" ]
      , pre [] [ text (MySQL.createTableCommand schema) ]
      , pre [] [ text (MySQL.insertRowCommand schema) ]
      ]
    ]

lofi-schema-elm's People

Contributors

penntaylor avatar royalicing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lofi-schema-elm's Issues

Real / floating-point numbers

One option is for numbers to be flexible by default, and then get constrained by hashtags:

#number / Double
#number #whole / Signed Integer
#number #whole #positive / Unsigned Integer
#number #positive / Double

Or the other is to try to match what people most closely think of as numbers. Perhaps this is whole numbers? Even positive whole numbers?

#number / Unsigned Integer
#number #real / Double
#number #allowNegative / Signed Integer
#number #real #allowNegative / Double

Negative / positive numbers

Work out how to denote negative/positive numbers. Some options:

-- Positive only by default
{ real : Bool
, allowNegative : Bool
, default : Maybe Float
}

-- #number / Unsigned Int / { allowNegative = False, allowPositive = True }
-- #number #allowNegative / Signed Int / { allowNegative = True, allowPositive = True }
-- Positive and negative by default
{ real : Bool
, allowNegative : Bool
, allowPositive : Bool
, default : Maybe Float
}

-- #number / Signed Int / { allowNegative = True, allowPositive = True }
-- #number #positive / Unsigned Int / { allowNegative = False, allowPositive = True }
-- #number #negative / Signed Int but negative only / { allowNegative = True, allowPositive = False }
-- #number #positive #negative / Signed Int / { allowNegative = True, allowPositive = True } / Discouraged!

Not sure which is better currently:

  • The first is simpler, but not sure if being positive by default is a good default or could catch people out. If it was simply #negative, is that clear as saying allow negative as well as positive, or does it suggest only allows negative numbers?
  • The second requires an extra tag #positive for positive numbers, and by default is signed allowing both positive and negative numbers which I’m unsure is a good default or not.

Yet another alternative is to use #min/#max instead:

-- Positive and negative by default
{ real : Bool
, default : Maybe Float
, min : Maybe Float
, max : Maybe Float
}

-- #number / Signed Int / { min = None, max = None }
-- #number #positive / Unsigned Int / { min = Just 0, max = None } / Discouraged??
-- #number #min: 0 / Unsigned Int / { min = Just 0, max = None } / Encouraged?
-- #number #min: 5 / Unsigned Int / { min = Just 5, max = None } / Encouraged?
-- #number #min: -200 / Signed Int / { min = Just -200, max = None } / Encouraged?
-- #number #negative / Signed Int but negative only / { min = None, max = Just 0 } / Discouraged??
-- #number #max: 0 / Signed Int but negative only / { min = None, max = Just 0 } / Encouraged?
-- #number #positive #negative / Signed Int / { min = None, max = None } / Discouraged!

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.