diff icon indicating copy to clipboard operation
diff copied to clipboard

When diffing an embedded pointer field against nil, the comparison should continue rather than break, and it should output a bunch of JSON.

Open kom0055 opened this issue 3 months ago • 0 comments

I suggest that , When diffing an embedded pointer field against nil, the comparison should continue rather than break, and it should output a bunch of JSON. If it infect too widely ,might as well use extra param

func (d *Differ) diffPtr(path []string, a, b reflect.Value, parent interface{}) error {
	if a.Kind() != b.Kind() {
		if a.Kind() == reflect.Invalid {
			if !b.IsNil() {
				return d.diff(path, reflect.ValueOf(nil), reflect.Indirect(b), parent)
			}

			d.cl.Add(CREATE, path, nil, exportInterface(b), parent)
			return nil
		}

		if b.Kind() == reflect.Invalid {
			if !a.IsNil() {
				return d.diff(path, reflect.Indirect(a), reflect.ValueOf(nil), parent)
			}

			d.cl.Add(DELETE, path, exportInterface(a), nil, parent)
			return nil
		}

		return ErrTypeMismatch
	}

	if a.IsNil() && b.IsNil() {
		return nil
	}

	if a.IsNil() {
		d.cl.Add(UPDATE, path, nil, exportInterface(b), parent)
		return nil
	}

	if b.IsNil() {
		d.cl.Add(UPDATE, path, exportInterface(a), nil, parent)
		return nil
	}

	return d.diff(path, reflect.Indirect(a), reflect.Indirect(b), parent)
}

kom0055 avatar Oct 30 '25 20:10 kom0055