Repository: dgraph-io/dgraph
File: edgraph/server.go (Go)

Code before:
			// To ensure uniqueness of values of email predicate, we will query for the value
			// to ensure that we are not adding a duplicate value.
			//
			// There are following use-cases of mutations which we need to handle:
			//   1. _:a <email> "example@email.com"  .
			//   2. _:a <email> val(queryVariable)  .
			// In the code below, we construct respective query for the above use-cases viz.
			//   __dgraph_uniquecheck_1__ as var(func: eq(email,"example@email.com"))
			//   __dgraph_uniquecheck_2__ as var(func: eq(email, val(queryVariable))){
			//       uid
			//       __dgraph_uniquecheck_val_2__ as email
			//   }
			// Each of the above query will check if there is already an existing value.
			// We can be sure that we may get at most one UID in return.
			// If the returned UID is different than the UID we have
			// in the mutation, then we reject the mutation.

			if !strings.HasPrefix(pred.ObjectId, "val(") {
				val := strconv.Quote(dql.TypeValFrom(pred.ObjectValue).Value.(string))
				query := fmt.Sprintf(`%v as var(func: eq(%v,"%v"))`, queryVar, predicateName, val[1:len(val)-1])
				if _, err := buildQuery.WriteString(query); err != nil {
					return errors.Wrapf(err, "error while writing string")
				}
				qc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{queryVar: queryVar}
			} else {
				valQueryVar := fmt.Sprintf("__dgraph_uniquecheck_val_%v__", uniqueVarMapKey)
				query := fmt.Sprintf(`%v as var(func: eq(%v,%v)){
					                             uid
					                             %v as %v
				                 }`, queryVar, predicateName, pred.ObjectId, valQueryVar, predicateName)
				if _, err := buildQuery.WriteString(query); err != nil {
					return errors.Wrapf(err, "error while building unique query")
				}
				qc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{valVar: valQueryVar, queryVar: queryVar}
			}
		}

		if buildQuery.Len() > 0 {
			if _, err := buildQuery.WriteString("}"); err != nil {
				return errors.Wrapf(err, "error while writing string")
			}
			if qc.req.Query == "" {
				qc.req.Query = "{" + buildQuery.String()
			} else {
				qc.req.Query = strings.TrimSuffix(qc.req.Query, "}")
				qc.req.Query = qc.req.Query + buildQuery.String()
			}
		}
	}
	return nil
}

Review comment (suggestion):
[nitpick] Consider using strconv.Unquote() instead of manually slicing the quoted string to remove the leading and trailing quotes. This approach could improve robustness if the quoting behavior changes in the future.
```suggestion
				quotedVal := strconv.Quote(dql.TypeValFrom(pred.ObjectValue).Value.(string))
				val, err := strconv.Unquote(quotedVal)
				if err != nil {
					return errors.Wrapf(err, "error while unquoting value")
				}
				query := fmt.Sprintf(`%v as var(func: eq(%v,"%v"))`, queryVar, predicateName, val)
```

Code after:
			uniqueVarMapKey := encodeIndex(gmuIndex, rdfIndex)
			queryVar := fmt.Sprintf("__dgraph_uniquecheck_%v__", uniqueVarMapKey)
			// Now, we add a query for a predicate to check if the value of the
			// predicate sent in the mutation already exists in the DB.
			// For example, schema => email: string @unique @index(exact) .\
			//
			// To ensure uniqueness of values of email predicate, we will query for the value
			// to ensure that we are not adding a duplicate value.
			//
			// There are following use-cases of mutations which we need to handle:
			//   1. _:a <email> "example@email.com"  .
			//   2. _:a <email> val(queryVariable)  .
			// In the code below, we construct respective query for the above use-cases viz.
			//   __dgraph_uniquecheck_1__ as var(func: eq(email,"example@email.com"))
			//   __dgraph_uniquecheck_2__ as var(func: eq(email, val(queryVariable))){
			//       uid
			//       __dgraph_uniquecheck_val_2__ as email
			//   }
			// Each of the above query will check if there is already an existing value.
			// We can be sure that we may get at most one UID in return.
			// If the returned UID is different than the UID we have
			// in the mutation, then we reject the mutation.

			if !strings.HasPrefix(pred.ObjectId, "val(") {
				val := strconv.Quote(fmt.Sprintf("%v", dql.TypeValFrom(pred.ObjectValue).Value))
				query := fmt.Sprintf(`%v as var(func: eq(%v,"%v"))`, queryVar, predicateName, val[1:len(val)-1])
				if _, err := buildQuery.WriteString(query); err != nil {
					return errors.Wrapf(err, "error while writing string")
				}
				qc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{queryVar: queryVar}
			} else {
				valQueryVar := fmt.Sprintf("__dgraph_uniquecheck_val_%v__", uniqueVarMapKey)
				query := fmt.Sprintf(`%v as var(func: eq(%v,%v)){
					                             uid
					                             %v as %v
				                 }`, queryVar, predicateName, pred.ObjectId, valQueryVar, predicateName)
				if _, err := buildQuery.WriteString(query); err != nil {
					return errors.Wrapf(err, "error while building unique query")
				}
				qc.uniqueVars[uniqueVarMapKey] = uniquePredMeta{valVar: valQueryVar, queryVar: queryVar}
			}
		}

		if buildQuery.Len() > 0 {
			if _, err := buildQuery.WriteString("}"); err != nil {
				return errors.Wrapf(err, "error while writing string")
			}
			if qc.req.Query == "" {
				qc.req.Query = "{" + buildQuery.String()
			} else {
				qc.req.Query = strings.TrimSuffix(qc.req.Query, "}")