<commit_before>module Models.Ad exposing (..)

import Date
import Json.Decode as Json
import Json.Decode.Pipeline as P
import Json.Decode.Extra exposing (date)
import Models.User exposing (User)

type alias Ad =
  { heading: String
  , content: String
  , answers: Answers
  , domain : Maybe String
  , position : Maybe String
  , location : Maybe String
  , createdBy: User
  , createdAt: Date.Date
  , id: Int
  }

type Answers = AnswerCount Int | AnswerList (List Answer)

type alias Answer =
  { content: String
  , createdBy: User
  , createdAt: Date.Date
  }


adDecoder : Json.Decoder Ad
adDecoder =
  P.decode Ad
    |> P.required "heading" Json.string
    |> P.required "content" Json.string
    |> P.required "answers" answersDecoder
    |> P.optional "domain" (Json.map Just Json.string) Nothing
    |> P.optional "position" (Json.map Just Json.string) Nothing
    |> P.optional "location" (Json.map Just Json.string) Nothing
    |> P.required "created_by" Models.User.userDecoder
    |> P.required "created_at" date
    |> P.required "id" Json.int

--answers can be either list of answers or a number
answersDecoder : Json.Decoder Answers
answersDecoder =
  Json.oneOf
    [ Json.map AnswerCount Json.int
    , Json.map AnswerList (Json.list answerDecoder)
    ]

answerDecoder : Json.Decoder Answer
answerDecoder =
  P.decode Answer
    |> P.required "content" Json.string
    |> P.required "created_by" Models.User.userDecoder
    |> P.required "created_at" date<commit_msg>Parse id in answers<commit_after>module Models.Ad exposing (..)

import Date
import Json.Decode as Json
import Json.Decode.Pipeline as P
import Json.Decode.Extra exposing (date)
import Models.User exposing (User)

type alias Ad =
  { heading: String
  , content: String
  , answers: Answers
  , domain : Maybe String
  , position : Maybe String
  , location : Maybe String
  , createdBy: User
  , createdAt: Date.Date
  , id: Int
  }

type Answers = AnswerCount Int | AnswerList (List Answer)

type alias Answer =
  { content: String
  , createdBy: User
  , createdAt: Date.Date
  , id : Int
  }


adDecoder : Json.Decoder Ad
adDecoder =
  P.decode Ad
    |> P.required "heading" Json.string
    |> P.required "content" Json.string
    |> P.required "answers" answersDecoder
    |> P.optional "domain" (Json.map Just Json.string) Nothing
    |> P.optional "position" (Json.map Just Json.string) Nothing
    |> P.optional "location" (Json.map Just Json.string) Nothing
    |> P.required "created_by" Models.User.userDecoder
    |> P.required "created_at" date
    |> P.required "id" Json.int

--answers can be either list of answers or a number
answersDecoder : Json.Decoder Answers
answersDecoder =
  Json.oneOf
    [ Json.map AnswerCount Json.int
    , Json.map AnswerList (Json.list answerDecoder)
    ]

answerDecoder : Json.Decoder Answer
answerDecoder =
  P.decode Answer
    |> P.required "content" Json.string
    |> P.required "created_by" Models.User.userDecoder
    |> P.required "created_at" date
    |> P.required "id" Json.int
{'fraction_non_alphanumeric': 0.10505450941526263, 'fraction_numerical': 0.0, 'mean_word_length': 3.41399416909621, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6681170', 'n_tokens_mistral': 979, 'n_tokens_neox': 923, 'n_words': 287}