Skip to content

Instantly share code, notes, and snippets.

@mscharley
Last active May 7, 2017 09:26
Show Gist options
  • Save mscharley/78c92c449f4ac03003eec1bfe547dd29 to your computer and use it in GitHub Desktop.
Save mscharley/78c92c449f4ac03003eec1bfe547dd29 to your computer and use it in GitHub Desktop.
Decoding many, many fields with Circe.
import cats._
import cats.implicits._
import io.circe._
import io.circe.generic.semiauto._
implicit lazy val guildDecoder: Decoder[Guild] =
(c: HCursor) =>
implicitly[Monad[Either[DecodingFailure, ?]]] sequence Vector(
c.downField("id").as[Snowflake],
c.downField("name").as[String],
c.downField("icon").as[Option[String]],
c.downField("splash").as[Option[String]],
c.downField("owner_id").as[Snowflake],
c.downField("region").as[String],
c.downField("afk_channel_id").as[Option[String]],
c.downField("afk_timeout").as[Int],
c.downField("embed_enabled").as[Option[Boolean]],
c.downField("embed_channel_id").as[Option[Snowflake]],
c.downField("verification_level").as[Int],
c.downField("default_message_notifications").as[Int],
c.downField("roles").as[Vector[Role]],
c.downField("emojis").as[Vector[Emoji]],
c.downField("features").as[Vector[String]],
c.downField("mfa_level").as[Int],
c.downField("joined_at").as[String],
c.downField("large").as[Boolean],
c.downField("unavailable").as[Boolean],
c.downField("member_count").as[Int],
c.downField("voice_states").as[Vector[VoiceState]],
c.downField("members").as[Vector[GuildMember]],
c.downField("channels").as[Vector[GuildChannel]],
c.downField("presences").as[Vector[PresenceUpdate]]
) map { v =>
Guild(
id = v(0).asInstanceOf[Snowflake],
name = v(1).asInstanceOf[String],
icon = v(2).asInstanceOf[Option[String]],
splash = v(3).asInstanceOf[Option[String]],
ownerId = v(4).asInstanceOf[Snowflake],
region = v(5).asInstanceOf[String],
afkChannelId = v(6).asInstanceOf[Option[String]],
afkTimeout = v(7).asInstanceOf[Int],
embedEnabled = v(8).asInstanceOf[Option[Boolean]],
embedChannelId = v(9).asInstanceOf[Option[Snowflake]],
verificationLevel = v(10).asInstanceOf[Int],
defaultMessageNotifications = v(11).asInstanceOf[Int],
roles = v(12).asInstanceOf[Vector[Role]],
emojis = v(13).asInstanceOf[Vector[Emoji]],
features = v(14).asInstanceOf[Vector[String]],
mfaLevel = v(15).asInstanceOf[Int],
joinedAt = v(16).asInstanceOf[String],
large = v(17).asInstanceOf[Boolean],
unavailable = v(18).asInstanceOf[Boolean],
memberCount = v(19).asInstanceOf[Int],
voiceStates = v(20).asInstanceOf[Vector[VoiceState]],
members = v(21).asInstanceOf[Vector[GuildMember]],
channels = v(22).asInstanceOf[Vector[GuildChannel]],
presences = v(23).asInstanceOf[Vector[PresenceUpdate]]
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment