Giter VIP home page Giter VIP logo

kvc's Introduction

KVC - Key Value Coding for Erlang data structures

[email protected]

Overview:

kvc supports Key Value Coding-like queries on common Erlang data structures. A common use case for kvc is to quickly access one or more deep values in decoded JSON, or some other nested data structure. It can also help with some aggregate operations. It solves similar problems that you might want to use a tool like XPath or jQuery for, but it is far simpler and strictly less powerful. It's inspired by Apple's NSKeyValueCoding protocol from Objective-C.

The following common Erlang data structures are supported:

  • list()
  • dict()
  • gb_trees()
  • proplist()
  • {struct, proplist()} (commonly used in mochijson2)
  • {proplist()} (EEP 18)
  • map() Erlang 17+

Only the following data types are permitted for keys, and they must be UTF-8 if any type coercion takes place:

  • atom()
  • binary()
  • string()

Another limitation is that it is assumed that the given data structure has a homogeneous key type. For example, if any key is binary(), all keys should be binary().

Collection Operators

The following collection operators are supported. Note that the numerics have straightforward implementations and no special care is taken reduce floating point error.

Their native representation is binary. Atom and string are also supported but require an additional type coercion for the look-up.

  • <<"@sum">>
  • <<"@min">>
  • <<"@max">>
  • <<"@count">>
  • <<"@avg">>
  • <<"@distinctUnionOfArrays">>
  • <<"@distinctUnionOfObjects">>
  • <<"@unionOfArrays">>
  • <<"@unionOfObjects">>

Status:

Not used in production, but it has a test suite that passes.

If you decide to use this in your production app, you should use lists for paths and try to use the same type as the keys in your data structure.

If you'd like to contribute to kvc, a good implementation for setters is the biggest missing piece.

Usage:

Two styles of queries are supported, the more performant native interface uses a list of keys for the path. If a string, binary, or atom are given then it will be split on '.' peroids to form this key list.

Simple proplist() example:

%% Native key list of atoms that match the data type (fastest)
wibble =:= kvc:path([foo, bar, baz], [{foo, [{bar, [{baz, wibble}]}]}]).

%% Native key list of binaries, does not match key data type (slower)
wibble =:= kvc:path([<<"foo">>, <<"bar">>, <<"baz">>],
                    [{foo, [{bar, [{baz, wibble}]}]}]).

%% These bare keys must be parsed first (slowest)
wibble =:= kvc:path('foo.bar.baz', [{foo, [{bar, [{baz, wibble}]}]}]).
wibble =:= kvc:path(<<"foo.bar.baz">>, [{foo, [{bar, [{baz, wibble}]}]}]).
wibble =:= kvc:path("foo.bar.baz", [{foo, [{bar, [{baz, wibble}]}]}]).

mochijson2 {struct, proplist()} example:

<<"wibble">> =:= kvc:path([<<"foo">>, <<"bar">>, <<"baz">>],
                     {struct,
                      [{<<"foo">>,
                        {struct,
                         [{<<"bar">>,
                           {struct, [{<<"baz">>, <<"wibble">>}]}}]}}]}).

<<"wibble">> =:= kvc:path('foo.bar.baz',
                     {struct,
                      [{<<"foo">>,
                        {struct,
                         [{<<"bar">>,
                         {struct, [{<<"baz">>, <<"wibble">>}]}}]}}]}).

maps example:

<<"wibble">> = kvc:path("foo.bar.baz",
                   #{<<"foo">> => #{<<"bar">> => #{<<"baz">> => <<"wibble">>}}}).

Collection operator example:

2.0 =:= kvc:path([<<"foo">>,<<"bar">>,<<"baz">>,<<"@avg">>],
                 {struct,
                  [{<<"foo">>,
                   {struct,
                    [{<<"bar">>,
                     {struct, [{<<"baz">>, [1, 2, 3]}]}}]}}]}).

2.0 =:= kvc:path('foo.bar.baz.@avg',
                 {struct,
                  [{<<"foo">>,
                   {struct,
                    [{<<"bar">>,
                     {struct, [{<<"baz">>, [1, 2, 3]}]}}]}}]}).

to_proplist normalization:

[{<<"foo">>, [{<<"bar">>, <<"baz">>}]}] =:=
    kvc:to_proplist({struct,
                     [{<<"foo">>,
                       {struct,
                        [{<<"bar">>, <<"baz">>}]}}]}).

kvc's People

Contributors

andreineculau avatar etrepum avatar msantos avatar nifoc avatar zfox-alertlogic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kvc's Issues

Default value isn't used when calling kvc:value/3 with "empty" proplist '[{}]'

First of all, I'm not sure that it's supposed to work at all, but since kvc is mean to be used for accessing fields in different json representations, I guess it's a valid case.

So the problem appears when you use some of the json parsers which represent json objects as proplists (e.g. jsx https://github.com/talentdeficit/jsx), and if your json is an empty object like "{}", the parsed representation will be [{}]. If you try to access this object, and try to get a value for any field, it will return an empty list, even if you specify a default value.

Some examples:

1> kvc:value(<<"foo">>, [{}], not_found).            
[]
2> kvc:value(<<"foo">>, [{"bar"}], not_found).
[]
3> kvc:value(<<"foo">>, [{"bar", true}], not_found).
not_found

new tag?

Thanks for merging the pull requests!

I'm not religious about it, but it would be better to reference a KVC tag, rather than a git hash. Care to consider rolling out one? Thanks

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.