Git Product home page Git Product logo

Comments (3)

JeanMertz avatar JeanMertz commented on August 16, 2024

FYI, here's the transformer I wrote, in case this is helpful to someone (this depends on #72):

func transformSetIfMissing(spec *transform.Config, data []byte) ([]byte, error) {
	for k, v := range *spec.Spec {
		var err error

		// If no error is returned, it means the path was found, and we return the
		// existing value.
		if _, err = transform.GetJSONRaw(data, k, true); err == nil {
			return data, nil
		}

		// If any error other than `NonExistentPath` is returned, something went
		// wrong and we return the error.
		if err != nil && err != transform.NonExistentPath {
			return nil, err
		}

		b, err := json.Marshal(v)
		if err != nil {
			return nil, transform.ParseError(fmt.Sprintf("Warn: Unable to coerce element to json string: %v", v))
		}

		data, err = transform.SetJSONRaw(data, b, k)
		if err != nil {
			return nil, err
		}
	}

	return data, nil
}

and the tests:

func TestTransformSetIfMissing(t *testing.T) {
	t.Parallel()

	tests := map[string]struct {
		spec map[string]interface{}
		in   string
		out  string
	}{
		"set default": {
			map[string]interface{}{"hello": "world"},
			`{ "hi": "universe" }`,
			`{ "hi": "universe", "hello": "world" }`,
		},

		"ignore if exists": {
			map[string]interface{}{"hi": "world"},
			`{ "hi": "universe" }`,
			`{ "hi": "universe" }`,
		},

		"set nested default": {
			map[string]interface{}{"hi.world": false},
			`{ "hi": { "universe": true } }`,
			`{ "hi": { "universe": true, "world": false } }`,
		},

		"complex structure": {
			map[string]interface{}{"hi": map[string]interface{}{"my": "world"}},
			`{ "hello": "universe" }`,
			`{ "hello": "universe", "hi": { "my": "world" } }`,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			cfg := &transform.Config{Spec: &tt.spec}

			b, err := transformSetIfMissing(cfg, []byte(tt.in))
			require.NoError(t, err)

			assert.JSONEq(t, tt.out, string(b))
		})
	}
}

from kazaam.

ryanleary avatar ryanleary commented on August 16, 2024

Thanks for the report!

from kazaam.

thomas-tharp avatar thomas-tharp commented on August 16, 2024

Closing as this is the documented behavior of the default transformer

from kazaam.

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.