Giter Club home page Giter Club logo

Comments (6)

philwills avatar philwills commented on May 24, 2024 3

Thanks @nrf110, really appreciate the feedback. I've added a bit to the PR to support updating list elements, which surprised me by leading me to make things simpler.

from scanamo.

philwills avatar philwills commented on May 24, 2024

Interesting, thanks for raising this and sorry for breaking it.

Whilst I tend to try and avoid overloading, my preference would probably be towards just making it work for the existing methods.

from scanamo.

philwills avatar philwills commented on May 24, 2024

OK, this isn't quite as simple as I thought it might be due to the joys of http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ExpressionPlaceholders.html#ExpressionAttributeNames, but I've got a plan. I'm thinking you'd write something of the form:

outers.update('id -> id, set('inner \ 'field -> "value"))

Does that seem like a sensible approach? I think it would be nice to avoid encoding the separator within the string.

from scanamo.

nrf110 avatar nrf110 commented on May 24, 2024

Thanks for the quick response!

I definitely prefer your approach over encoding it in the string, and it covers our existing simple use cases. To make this handle all nested fields though, I think there is still the case of updating elements within a List, i.e. foo.bar[2] = baz

Extending your example a bit, how do you feel about something like this? (obviously we'd need to add a few more implicit conversions):

sealed trait PathNode {
  def name: String
  def separator: String
  def placeholder: String
}

case class KeyPathNode(s: Symbol) extends PathNode{
  def name: String = s.name
  def separator = "."
  def placeholder: String = s"#update${s.name}"
}

case class IndexPathNode(i: Int) extends PathNode {
  def name: String = s"[$i]"
  def separator: String = s""
  def placeholder: String = name
}

case class Field(traversal: NonEmptyVector[PathNode]) {
  val placeholder: String = traversal.foldLeft("")((l, r) => s"$l${r.separator}${r.placeholder}")
  val name: String = traversal.tail.foldLeft(traversal.head.name)((l, r) => s"$l${r.separator}${r.name}")
  val attributeNames: Map[String, String] = { // TODO: Not sure if/how we are supposed to map the list indices
    traversal.filter {
      case _: KeyPathNode => true
      case _ => false
    }.map(node => node.placeholder -> node.name).toMap
  }

  def \ (inner: Symbol): Field = Field(traversal.append(KeyPathNode(inner)))
  def apply(idx: Int): Field = Field(traversal.append(IndexPathNode(idx)))
}

usage would then look like:

outers.update('id -> id, ('inner \ 'field)(2) -> "value")

from scanamo.

nrf110 avatar nrf110 commented on May 24, 2024

The implementation looks good, I like the simplification. 👍

from scanamo.

philwills avatar philwills commented on May 24, 2024

Closing this, as there's now an alternative available in 0.9.1

from scanamo.

Related Issues (20)

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.