Giter VIP home page Giter VIP logo

go-dotnotation's People

Contributors

joeycumines avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-dotnotation's Issues

Set on target that haven't the maps in the middle of path

Hi, thanks for the package first. I found and issue when I try to set a nested property in a map[string]inteface{} that is empty.

Think on an empty map and you try to set 'nested.prop' it fail because this line: https://github.com/joeycumines/go-dotnotation/blob/master/dotnotation/accessor.go#L29
return an error because nested is not there, so I change that with this:

func (p Accessor) Set(target interface{}, key string, value interface{}) error {
  properties := p.parser(key)
  var targetValue interface{} // added this
  for i, property := range properties {
    if i == (len(properties) - 1) {
      // we reached the last property
      return p.setter(target, property, value)
    }
    // attempt to get the next level, so we can set the last property
    var err error
    targetValue, err = p.getter(target, property) // assign to targetValue
    if err != nil {
      nextLevel := make(map[string]interface{}) // create next level as map[string]interface{}
      err = p.setter(target, property, nextLevel) // set to current target the next level
      target = nextLevel // change the target to nextLevel so next iteration will set on it.
      if err != nil {
        return err
      }
    } else {
      target = targetValue // let the code follow the idea of original target
    }
  }
  return errors.New("no properties parsed from key: " + key)
}

So basically we are going through an empty map and we will make the middle missing map. If u wish I can make a PR but I'm really new in Go world.

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.