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

Complex Type #24

Open
brasilikum opened this issue Mar 26, 2017 · 4 comments
Open

Complex Type #24

brasilikum opened this issue Mar 26, 2017 · 4 comments

Comments

@brasilikum
Copy link

I am trying to generate the elm decoder for my jsonresume.

I get the type "ComplexType", which is not defined. What does it mean? Is this JSON to complex for the tool, or is this a bug?

This is the json in question: (resulting elm below)

{
  "basics": {
    "name": "Georg Hartmann",
    "label": "Software Engineer",
    "phone": "",
    "website": "",
    "summary": "",
    "location": {
      "address": "",
      "postalCode": "",
      "city": "",
      "countryCode": "",
      "region": ""
    },
    "profiles": []
  },
  "work": [
    {
      "company": "Q1 Energie AG",
      "position": "Software Engineer",
      "website": "",
      "startDate": "2016-01-01",
      "summary": "zahlz Mobile Payments, Backend, Golang, Go, RESTful Services, Grafana, Monitoring, Docker, Technical Consulting",
      "highlights": [],
      "endDate": "2017-01-01"
    },
    {
      "company": "Georg Hartmann IT-Dienstleistungen",
      "position": "Freiberuflicher Softwareentwickler",
      "website": "",
      "startDate": "2015-04-01",
      "summary": "Design und Implementierung von Backend Services for Mobile Payment in Golang, Technisches Consulting, Web-Entwicklung.",
      "highlights": [],
      "endDate": "2015-12-01"
    },
    {
      "company": "High Performance Computing Laboratory, PUCRS",
      "position": "Praktikant",
      "website": "",
      "startDate": "2014-09-01",
      "summary": "3 Monate Praktikum, 3 Monate schreiben der Bachelorarbeit auf Englisch mit dem Thema: \nUsage Statistics for the clusters with Node.js and MongoDB",
      "highlights": [],
      "endDate": "2015-02-01"
    },
    {
      "company": "Hochschule Osnabrueck (University of Applied Sciences)",
      "position": "Tutor für Mathematik",
      "website": "",
      "startDate": "2014-03-01",
      "summary": "",
      "highlights": [],
      "endDate": "2014-07-01"
    },
    {
      "company": "Recifesites",
      "position": "Praktikant",
      "website": "",
      "startDate": "2012-08-01",
      "summary": "Frontend web development mit HTML, CSS und JS (jQuery)",
      "highlights": [],
      "endDate": "2012-09-01"
    },
    {
      "company": "Grundschule Matancitas",
      "position": "Freiwilliger",
      "website": "",
      "startDate": "2011-03-01",
      "summary": "Freiwilliger in Vollzeit im Rahmen eines Freiwilligen Sozialen Jahres (FSJ)",
      "highlights": [],
      "endDate": "2011-09-01"
    },
    {
      "company": "Barraca da Amizade, Social Project",
      "position": "Freiwilliger",
      "website": "",
      "startDate": "2010-07-01",
      "summary": "Freiwilliger in Vollzeit im Rahmen eines Freiwilligen Sozialen Jahres (FSJ).\nLeitung eines Recycingworkshops",
      "highlights": [],
      "endDate": "2011-02-01"
    }
  ],
  "education": [
    {
      "institution": "Freie Waldorfschule Balingen",
      "area": "",
      "studyType": "Abitur",
      "startDate": "1997-01-01",
      "gpa": "",
      "courses": [],
      "endDate": "2010-01-01"
    },
    {
      "institution": "Hochschule Osnabrück",
      "area": "",
      "studyType": "Bachelor of Science (BS)",
      "startDate": "2012-01-01",
      "gpa": "",
      "courses": [],
      "endDate": "2015-01-01"
    }
  ],
  "skills": [
    {
      "name": "CI",
      "level": "",
      "keywords": []
    },
    {
      "name": "Elm",
      "level": "",
      "keywords": []
    },
    {
      "name": "Web Development",
      "level": "",
      "keywords": []
    },
    {
      "name": "Cascading Style Sheets (CSS)",
      "level": "",
      "keywords": []
    },
    {
      "name": "jQuery",
      "level": "",
      "keywords": []
    },
    {
      "name": "Microsoft Office",
      "level": "",
      "keywords": []
    },
    {
      "name": "MongoDB",
      "level": "",
      "keywords": []
    },
    {
      "name": "Golang",
      "level": "",
      "keywords": []
    },
    {
      "name": "Java",
      "level": "",
      "keywords": []
    },
    {
      "name": "C++",
      "level": "",
      "keywords": []
    },
    {
      "name": "Node.js",
      "level": "",
      "keywords": []
    },
    {
      "name": "JavaScript",
      "level": "",
      "keywords": []
    },
    {
      "name": "HTML 5",
      "level": "",
      "keywords": []
    },
    {
      "name": "Linux",
      "level": "",
      "keywords": []
    },
    {
      "name": "Git",
      "level": "",
      "keywords": []
    },
    {
      "name": "Software Development",
      "level": "",
      "keywords": []
    },
    {
      "name": "MySQL",
      "level": "",
      "keywords": []
    },
    {
      "name": "Representational State Transfer (REST)",
      "level": "",
      "keywords": []
    },
    {
      "name": "Monitoring",
      "level": "",
      "keywords": []
    }
  ],
  "languages": [
    {
      "language": "Spanish",
      "fluency": "Limited working"
    },
    {
      "language": "Portuguese",
      "fluency": "Full professional"
    },
    {
      "language": "French",
      "fluency": "Elementary"
    },
    {
      "language": "German",
      "fluency": "Native or bilingual"
    },
    {
      "language": "English",
      "fluency": "Full professional"
    }
  ]
}

This is the result:

import Json.Encode
import Json.Decode exposing (field)

type alias Something =
    { basics : SomethingBasics
    , work : List ComplexType
    , education : List ComplexType
    , skills : List ComplexType
    , languages : List ComplexType
    }

type alias SomethingBasicsLocation =
    { address : String
    , postalCode : String
    , city : String
    , countryCode : String
    , region : String
    }

type alias SomethingBasics =
    { name : String
    , label : String
    , phone : String
    , website : String
    , summary : String
    , location : SomethingBasicsLocation
    , profiles : List ComplexType
    }

decodeSomething : Json.Decode.Decoder Something
decodeSomething =
    Json.Decode.map5 Something
        (field "basics" decodeSomethingBasics)
        (field "work" Json.Decode.list decodeComplexType)
        (field "education" Json.Decode.list decodeComplexType)
        (field "skills" Json.Decode.list decodeComplexType)
        (field "languages" Json.Decode.list decodeComplexType)

decodeSomethingBasicsLocation : Json.Decode.Decoder SomethingBasicsLocation
decodeSomethingBasicsLocation =
    Json.Decode.map5 SomethingBasicsLocation
        (field "address" Json.Decode.string)
        (field "postalCode" Json.Decode.string)
        (field "city" Json.Decode.string)
        (field "countryCode" Json.Decode.string)
        (field "region" Json.Decode.string)

decodeSomethingBasics : Json.Decode.Decoder SomethingBasics
decodeSomethingBasics =
    Json.Decode.Pipeline.decode SomethingBasics
        |> Json.Decode.Pipeline.required "name" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "label" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "phone" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "website" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "summary" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "location" (decodeSomethingBasicsLocation)
        |> Json.Decode.Pipeline.required "profiles" (Json.Decode.list decodeComplexType)

encodeSomething : Something -> Json.Encode.Value
encodeSomething record =
    Json.Encode.object
        [ ("basics",  encodeSomethingBasics <| record.basics)
        , ("work",  Json.Encode.list <| List.map encodeComplexType <| record.work)
        , ("education",  Json.Encode.list <| List.map encodeComplexType <| record.education)
        , ("skills",  Json.Encode.list <| List.map encodeComplexType <| record.skills)
        , ("languages",  Json.Encode.list <| List.map encodeComplexType <| record.languages)
        ]

encodeSomethingBasicsLocation : SomethingBasicsLocation -> Json.Encode.Value
encodeSomethingBasicsLocation record =
    Json.Encode.object
        [ ("address",  Json.Encode.string <| record.address)
        , ("postalCode",  Json.Encode.string <| record.postalCode)
        , ("city",  Json.Encode.string <| record.city)
        , ("countryCode",  Json.Encode.string <| record.countryCode)
        , ("region",  Json.Encode.string <| record.region)
        ]

encodeSomethingBasics : SomethingBasics -> Json.Encode.Value
encodeSomethingBasics record =
    Json.Encode.object
        [ ("name",  Json.Encode.string <| record.name)
        , ("label",  Json.Encode.string <| record.label)
        , ("phone",  Json.Encode.string <| record.phone)
        , ("website",  Json.Encode.string <| record.website)
        , ("summary",  Json.Encode.string <| record.summary)
        , ("location",  encodeSomethingBasicsLocation <| record.location)
        , ("profiles",  Json.Encode.list <| List.map encodeComplexType <| record.profiles)
        ]
@brasilikum
Copy link
Author

Ok, fund the solution in the source:

ComplexType ->
"something that has to be written by hand!"

I would like to know why that is. Could somebody explain it to me please?

@eeue56
Copy link
Owner

eeue56 commented Mar 27, 2017

@brasilikum, a complex type means that json-to-elm couldn't quite figure out what to do. FWIW, if you use the english option, you would've gotten that message in the browser :)

@eeue56
Copy link
Owner

eeue56 commented Mar 27, 2017

You can also provide the type alias, and json-to-elm has a better time working things out from the type alias! hope that helps :)

@brasilikum
Copy link
Author

brasilikum commented Mar 29, 2017

You can also provide the type alias, and json-to-elm has a better time working things out from the type alias! hope that helps :)

Are you talking about the the Toplevel Alias? And could you elaborate how that helps working things out? I thought that was simply to replace Something in the Aliases. I did it and nothing except for the naming changed.

@brasilikum, a complex type means that json-to-elm couldn't quite figure out what to do. FWIW, if you use the english option, you would've gotten that message in the browser :)

Thanks! As a feedback, I think that should be like a warning, as it is not related to original or pipeline. I think it could make this more obvious == user-friendly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants