// Tests dynamic field key resolution including string interpolations, chained
// resolution, and comprehension with let bindings. Covers panic fix for issue799
// (struct used in string interpolation) and issue3828 (let inside comprehension
// with dynamic field key).
-- in.cue --
a: "foo"
"\(a)": b: c: d: e
e: 2

b: "bar"
X="\(b)": {
	a: 1
}
c: X

withError: {
	// Panic when running cue eval with a struct used in a string interpolation.
	// https://cuelang.org/issue/799
	issue799: {key: int32} & {"\(1 & 2)": 123}
}

parenExprRefParent: {
	a: (x): {}
	x: "foo"
}
parenExprRefEqual: {
	(x): {}
	x: "foo"
}
chainedResolution: x: {
	(x.baz): "bar"
	(x.foo): "baz"
	baz:     "foo"
}

// A dynamic field key that references an incomplete value (string interpolation
// using abstract string) must produce an "incomplete" error, not a spurious
// "cycle error". The error should also include path and position information.
// https://cuelang.org/issue/1407
noCycleError: {
	foo: [_]: {
		#ID: "\(string)"
	}
	foo: bar: entries: (foo.baz.#ID): true
	foo: baz: {}
}
-- constraints.cue --
constraints: {
	t1: "foo"
	t2: "bar"
	(t1)?: (t2)!: 3
}
-- issue3828.cue --
// evalv3 regression: a let inside a comprehension with a dynamic field key
// failed to produce the correct output due to a "cycle with field" error.
// https://cuelang.org/issue/3828
issue3828: reduced: {
	a: b: {}
	a: [_]: {
		if true let x = str {
			(x): true
		}
	}
	str: "foobot1"
}
issue3828: full: {
	orgs: org1: {
		repos: repo1: {}
	}
	botUser: "bot1"
	orgs: [_]: config={
		for repo_name, _ in config.repos
		let orgBotUser = botUser {
			collaborators: (repo_name): (orgBotUser): true
		}
	}
}
-- out/compile --
--- constraints.cue
{
  constraints: {
    t1: "foo"
    t2: "bar"
    〈0;t1〉?: {
      〈1;t2〉!: 3
    }
  }
}
--- in.cue
{
  a: "foo"
  "\(〈0;a〉)": {
    b: {
      c: {
        d: 〈3;e〉
      }
    }
  }
  e: 2
  b: "bar"
  "\(〈0;b〉)": {
    a: 1
  }
  c: 〈0;("\(〈0;b〉)")〉
  withError: {
    issue799: ({
      key: &(int, >=-2147483648, <=2147483647)
    } & {
      "\((1 & 2))": 123
    })
  }
  parenExprRefParent: {
    a: {
      〈1;x〉: {}
    }
    x: "foo"
  }
  parenExprRefEqual: {
    〈0;x〉: {}
    x: "foo"
  }
  chainedResolution: {
    x: {
      〈1;x〉.baz: "bar"
      〈1;x〉.foo: "baz"
      baz: "foo"
    }
  }
  noCycleError: {
    foo: {
      [_]: {
        #ID: "\(string)"
      }
    }
    foo: {
      bar: {
        entries: {
          〈3;foo〉.baz.#ID: true
        }
      }
    }
    foo: {
      baz: {}
    }
  }
}
--- issue3828.cue
{
  issue3828: {
    reduced: {
      a: {
        b: {}
      }
      a: {
        [_]: {
          if true let x = 〈2;str〉 {
            〈1;x〉: true
          }
        }
      }
      str: "foobot1"
    }
  }
  issue3828: {
    full: {
      orgs: {
        org1: {
          repos: {
            repo1: {}
          }
        }
      }
      botUser: "bot1"
      orgs: {
        [_]: {
          for repo_name, _ in 〈1〉.repos let orgBotUser = 〈3;botUser〉 {
            collaborators: {
              〈3;repo_name〉: {
                〈3;orgBotUser〉: true
              }
            }
          }
        }
      }
    }
  }
}
