Coder Social home page Coder Social logo

Support for tuples about conform HOT 3 CLOSED

vasspilka avatar vasspilka commented on July 22, 2024 3
Support for tuples

from conform.

Comments (3)

mertonium avatar mertonium commented on July 22, 2024

This is also a problem for the redix_sentinel lib - where the mix config looks like:

config :my_app, :sentinel_config,
    sentinels: [
      [host: "10.200.X.X", port: 26379],
      [host: "10.200.X.X", port: 26379],
      [host: "10.200.X.X", port: 26379]
    ],
    role: "master",
    group: "misc"

@bitwalker if you could provide some guidance on how a config like this is supposed to look in .conf format, I could take a crack at implementing a write_datatype function.

from conform.

mertonium avatar mertonium commented on July 22, 2024

@vasspilka - for what it's worth, I solved my problem with transforms.

I changed my schema to look like this:

...
"my_app.blocklist_redis.sentinels": [
  commented: false,
  datatype: [list: :ip],
  doc: "Sentinel host:port combinations",
  hidden: false,
  to: "my_app.blocklist_redis.sentinels"
],
"my_app.blocklist_redis.role": [
  commented: false,
  datatype: :binary,
  doc: "Sentinel role",
  hidden: false,
  to: "my_app.blocklist_redis.role"
],
"my_app.blocklist_redis.group": [
  commented: false,
  datatype: :binary,
  doc: "Sentinel group",
  hidden: false,
  to: "my_app.blocklist_redis.group"
],
...

and then added a transform like this:

transforms: [
  "my_app.sentinel_config": fn pid ->
    # Helper function to convert a list of :ip types into a list of [host: x.x.x.x, port: xxxx] tuples
    # (I haven't quite figured out how to use "import" yet :) )
    ip_reducer = fn val ->
      Enum.reduce(val, [], fn {host, port}, acc ->
        [[host: host, port: String.to_integer(port)] | acc]
      end)
    end

    redis_config =
      pid
      |> Conform.Conf.find("my_app.sentinel_config.$")
      |> Enum.reduce(Keyword.new, fn {[_, _, _, key], val}, acc ->
        cond do
          is_nil(val) -> acc
          is_list(val) -> Keyword.put_new(acc, List.to_atom(key), ip_reducer.(val))
          true -> Keyword.put_new(acc, List.to_atom(key), val)
        end
      end)

    Conform.Conf.remove(pid, "my_app.sentinel_config.$")
    redis_config
  end
],

from conform.

vasspilka avatar vasspilka commented on July 22, 2024

Yes, that's what I ended up doing as well :) . I am closing this issue in favor of Transforms https://hexdocs.pm/conform/getting-started.html#content Sadly they are not well documented but that is another issue, I could great a PR with better docs for Transforms, Types @bitwalker is there interest for this?

from conform.

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.