text:
#include "tensorflow/compiler/xla/service/hlo_rematerialization.h"

#include <memory>
#include <string>

#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
#include "tensorflow/compiler/xla/service/hlo_matchers.h"
#include "tensorflow/compiler/xla/service/hlo_opcode.h"
#include "tensorflow/compiler/xla/service/hlo_ordering.h"
#include "tensorflow/compiler/xla/shape_util.h"
#include "tensorflow/compiler/xla/tests/hlo_test_base.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"
#include "tensorflow/core/lib/core/status_test_util.h"

namespace xla {
namespace {

namespace op = xla::testing::opcode_matchers;

using ::testing::_;

class HloRematerializationTest : public HloTestBase {
 protected:
  // Creates and returns a computation which can benefit from
  // rematerialization. The computation looks like:
  //
  //   F32[1] %param = {...}
  //   F32[] %reshape = reshape(F32[], param)
  //   F32[1024] %bcast = broadcast(%param)
  //   F32[1024] %negate = negate(%bcast)
  //   F32[2048] %concat_1 = concat({%negate, %negate})
  //   F32[1] %slice_1 = slice(%concat_1, {0:1})
  //   F32[1025] %concat_2 = concat({%bcast, %slice_1})
  //   F32[1] %slice_2 = slice(%concat_2, {0:1});
  //
  // The instruction %bcast can be rematerialized before its use at %concat_2
  // to reduce peak memory usage. This avoids %bcast and %concat_1 being
  // simultaneously live. Peak memory use is about 16KB before rematerialization
  // (during execution of %concat_1) and about 12KB after rematerializing %bcast
  // for its use in %concat_2.
  std::unique_ptr<HloComputation> MakeRematerializableComputation(
      const string& suffix = "") {
    auto builder = HloComputation::Builder(TestName() + suffix);
    auto param = builder.AddInstruction(
        HloInstruction::CreateParameter(0, vec1_shape_, "param"));
    auto reshape = builder.AddInstruction(
        HloInstruction::CreateReshape(scalar_shape_, param));
    auto bcast = builder.AddInstruction(
        HloInstruction::CreateBroadcast(vec1024_shape_, reshape, {}));
    auto negate = builder.AddInstruction(
        HloInstruction::CreateUnary(vec1024_shape_, HloOpcode::kNegate, bcast));
    auto concat_1 = builder.AddInstruction(HloInstruction::CreateConcatenate(
        ShapeUtil::MakeShape(xla::F32, {2048}), {negate, negate},
        /*dimension=*/0));
    auto slice_1 = builder.AddInstruction(HloInstruction::CreateSlice(
        vec1_shape_, concat_1, /*start_indices=*/{0},
        /*limit_indices=*/{1},
        /*strides=*/{1}));
    auto concat_2 = builder.AddInstruction(HloInstruction::CreateConcatenate(
        ShapeUtil::MakeShape(xla::F32, {1025}), {bcast, slice_1},
        /*dimension=*/0));
    // Add a final slice to make the parameter shape match the output shape
    // which is necessary to use this computation in a while.
    builder.AddInstruction(HloInstruction::CreateSlice(vec1_shape_, concat_2,
                                                       /*start_indices=*/{0},
                                                       /*limit_indices=*/{1},
                                                       /*strides=*/{1}));
    return builder.Build();
  }

  // Creates and returns a computation which includes a while and can benefit
  // from rematerialization. The computation looks like:
  //
  //   F32[] %param = {...}
  //   F32[1024] %bcast = broadcast(%param)
  //   F32[1] %slice_1 = slice(%bcast, {0:1})
  //   F32[1] %while = while(%slice_1, while_body, while_cond)
  //   F32[1025] %concat = concat({%bcast, %while})
  //   F32[1] %slice_2 = slice(%concat, {0:1});
  //
  // The instruction %bcast can be rematerialized before its use at %concat to
  // reduce peak memory usage. This avoids %bcast being live during execution of
  // the while. Peak memory use is maximum of 8K and 4K plus the memory use of
  // the while subcomputations.
  std::unique_ptr<HloComputation> MakeRematerializableWhileComputation(
      HloComputation* while_cond, HloComputation* while_body,
      const string& suffix = "") {
    auto builder = HloComputation::Builder(TestName() + suffix);
    auto param = builder.AddInstruction(
        HloInstruction::CreateParameter(0, vec1_shape_, "param"));
    auto reshape = builder.AddInstruction(
        HloInstruction::CreateReshape(scalar_shape_, param));
    auto bcast = builder.AddInstruction(
        HloInstruction::CreateBroadcast(vec1024_shape_, reshape, {}));
    auto slice_1 = builder.AddInstruction(
        HloInstruction::CreateSlice(vec1_shape_, bcast, /*start_indices=*/{0},
                                    /*limit_indices=*/{1},
                                    /*strides=*/{1}));
    auto while_inst = builder.AddInstruction(HloInstruction::CreateWhile(
        vec1_shape_, while_cond, while_body, slice_1));
    auto concat = builder.AddInstruction(HloInstruction::CreateConcatenate(
        ShapeUtil::MakeShape(xla::F32, {1025}), {bcast, while_inst},
        /*dimension=*/0));
    builder.AddInstruction(HloInstruction::CreateSlice(vec1_shape_, concat,
                                                       /*start_indices=*/{0},
                                                       /*limit_indices=*/{1},
                                                       /*strides=*/{1}));
    return builder.Build();
  }

  // Create and return a trivial computation appropriate for use as a while
  // condition.
  std::unique_ptr<HloComputation> MakeConditionComputation() {
    auto builder = HloComputation::Builder(TestName() + ".cond");
    builder.AddInstruction(
        HloInstruction::CreateParameter(0, vec1_shape_, "param"));
    builder.AddInstruction(
        HloInstruction::CreateConstant(LiteralUtil::CreateR0<bool>(true)));
    return builder.Build();
  }

  // Return the byte size of the top-level buffer of the given shape.
  static int64 ByteSizeOf(const Shape& shape) {
    return ShapeUtil::ByteSizeOf(shape, sizeof(void*));
  }

  StatusOr<bool> RunHloRematerialization(int64 memory_limit_bytes,
                                         HloModule* module) {
    TF_EXPECT_OK(verifier().Run(module).status());
    HloMemoryScheduler scheduler(
        [](const BufferValue& buffer) { return ByteSizeOf(buffer.shape()); },
        DefaultMemoryScheduler);
    TF_EXPECT_OK(scheduler.Run(module).status());
    HloRematerialization remat(ByteSizeOf, memory_limit_bytes,
                               /*sizes=*/nullptr);
    return remat.Run(module);
  }

  // Various shapes used in the canned computations.
  const Shape scalar_shape_ = ShapeUtil::MakeShape(xla::F32, {});
  const Shape vec1_shape_ = ShapeUtil::MakeShape(xla::F32, {1});
  const Shape vec1024_shape_ = ShapeUtil::MakeShape(xla::F32, {1024});
};

// Test rematerialization of a single computation produced by
// MakeRematerializableComputation.
TEST_F(HloRematerializationTest, SingleComputation) {
  auto module = CreateNewVerifiedModule();
  HloComputation* computation =
      module->AddEntryComputation(MakeRematerializableComputation());

  // Find and save the original broadcast instruction which should be
  // rematerialized.
  const HloInstruction* slice = computation->root_instruction();
  ASSERT_THAT(slice, op::Slice(op::Concatenate(op::Broadcast(_), _)));
  const HloInstruction* concat = slice->operand(0);
  const HloInstruction* bcast = concat->operand(0);

  // Computation requires 16KB without rematerialization, but uses only 12KB
  // with rematerialization so pick a memory limit between these values (14KB).
  TF_ASSERT_OK_AND_ASSIGN(bool changed,
                          RunHloRematerialization(
                              /*memory_limit_bytes=*/14 * 1024, module.get()));
  EXPECT_TRUE(changed);

  // Root should not have changed.
  EXPECT_EQ(computation->root_instruction(), slice);

  // The broadcast should have been rematerialized.
  const HloInstruction* remat_bcast = concat->operand(0);
  EXPECT_THAT(remat_bcast, op::Broadcast(::testing::Ne(bcast)));

  // The rematerialized broadcast should be immediate before the concat in the
  // sequence.
  EXPECT_EQ(module->schedule()
                .sequence(computation)
                .instructions()[computation->instruction_count() - 2],
            concat);
  EXPECT_EQ(module->schedule()
                .sequence(computation)
                .instructions()[computation->instruction_count() - 3],
            remat_bcast);
}

// Test rematerialization of a single computation produced by
// MakeRematerializableComputation but with a sufficiently high memory limit
// such that no instructions are rematerialized.
TEST_F(HloRematerializationTest, SingleComputationNoRematerialization) {
  auto module = CreateNewVerifiedModule();
  HloComputation* computation =
      module->AddEntryComputation(MakeRematerializableComputation());

  EXPECT_EQ(computation->instruction_count(), 8);

  TF_ASSERT_OK_AND_ASSIGN(bool changed,
                          RunHloRematerialization(
                              /*memory_limit_bytes=*/20 * 1024, module.get()));

  // No instructions should have been materialized.
  EXPECT_FALSE(changed);
  EXPECT_EQ(computation->instruction_count(), 8);
}

// Test rematerialization of a computation which calls another computation via a
// while. Both the entry computation and while body computation can have memory
// usage reduced via rematerialization however the memory limit is set such that
// only one computation needs to have an instruction rematerialized. The entry
// computation should be the one chosen because rematerialization in the while
// will presumably be more expensive.
TEST_F(HloRematerializationTest, RematerializeAroundWhile) {
  auto module = CreateNewVerifiedModule();

  auto cond_builder = HloComputation::Builder(TestName() + ".cond");
  cond_builder.AddInstruction(
      HloInstruction::CreateParameter(0, vec1_shape_, "param"));
  cond_builder.AddInstruction(
      HloInstruction::CreateConstant(LiteralUtil::CreateR0<bool>(true)));
  HloComputation* while_cond =
      module->AddEmbeddedComputation(cond_builder.Build());

  HloComputation* body_computation = module->AddEmbeddedComputation(
      MakeRematerializableComputation(/*suffix=*/".body"));
  HloComputation* entry_computation =
      module->AddEntryComputation(MakeRematerializableWhileComputation(
          while_cond, /*while_body=*/body_computation));

  EXPECT_EQ(entry_computation->instruction_count(), 7);
  EXPECT_EQ(body_computation->instruction_count(), 8);

  // The body computation uses 16KB and the entry computation uses 2KB at the
  // while so the peak memory use of the module is 18KB. Set the memory limit a
  // bit lower (17KB) to force rematerialization of the entry computation.
  TF_ASSERT_OK_AND_ASSIGN(bool changed,
                          RunHloRematerialization(
                              /*memory_limit_bytes=*/17 * 1024, module.get()));
  EXPECT_TRUE(changed);

  // Only the entry computation should have a rematerialized instruction added.
  EXPECT_EQ(entry_computation->instruction_count(), 8);
  EXPECT_EQ(body_computation->instruction_count(), 8);
}

// Test rematerialization of a computation which calls another computation via a
// while. Both the entry computation and while body computation should have
// computations rematerialized.
TEST_F(HloRematerializationTest, RematerializeEntryAndWhileBody) {
  auto module = CreateNewVerifiedModule();

  auto cond_builder = HloComputation::Builder(TestName() + ".cond");
  cond_builder.AddInstruction(
      HloInstruction::CreateParameter(0, vec1_shape_, "param"));
  cond_builder.AddInstruction(
      HloInstruction::CreateConstant(LiteralUtil::CreateR0<bool>(true)));
  HloComputation* while_cond =
      module->AddEmbeddedComputation(cond_builder.Build());

  HloComputation* body_computation = module->AddEmbeddedComputation(
      MakeRematerializableComputation(/*suffix=*/".body"));
  HloComputation* entry_computation =
      module->AddEntryComputation(MakeRematerializableWhileComputation(
          while_cond, /*while_body=*/body_computation));

  EXPECT_EQ(entry_computation->instruction_count(), 7);
  EXPECT_EQ(body_computation->instruction_count(), 8);

  TF_ASSERT_OK_AND_ASSIGN(bool changed,
                          RunHloRematerialization(
                              /*memory_limit_bytes=*/15 * 1024, module.get()));
  EXPECT_TRUE(changed);

  // Both computations should have rematerialized instructions added.
  EXPECT_EQ(entry_computation->instruction_count(), 9);
  EXPECT_EQ(body_computation->instruction_count(), 9);
}

// Test rematerialization of a doubly nested computation. All computations
// should have an instruction rematerialized.
TEST_F(HloRematerializationTest, RematerializeNestedComputations) {
  auto module = CreateNewVerifiedModule();

  auto cond_builder = HloComputation::Builder(TestName() + ".cond");
  cond_builder.AddInstruction(
      HloInstruction::CreateParameter(0, vec1_shape_, "param"));
  cond_builder.AddInstruction(
      HloInstruction::CreateConstant(LiteralUtil::CreateR0<bool>(true)));
  HloComputation* while_cond =
      module->AddEmbeddedComputation(cond_builder.Build());

  HloComputation* inner_computation = module->AddEmbeddedComputation(
      MakeRematerializableComputation(/*suffix=*/".inner"));
  HloComputation* middle_computation =
      module->AddEmbeddedComputation(MakeRematerializableWhileComputation(
          while_cond, /*while_body=*/inner_computation,
          /*suffix=*/".middle"));
  HloComputation* entry_computation =
      module->AddEntryComputation(MakeRematerializableWhileComputation(
          while_cond, /*while_body=*/middle_computation));

  EXPECT_EQ(entry_computation->instruction_count(), 7);
  EXPECT_EQ(middle_computation->instruction_count(), 7);
  EXPECT_EQ(inner_computation->instruction_count(), 8);

  // If all computations are maximally rematerialized then peak memory usage is
  // ~12K so pick something slightly larger.
  TF_ASSERT_OK_AND_ASSIGN(bool changed,
                          RunHloRematerialization(
                              /*memory_limit_bytes=*/13 * 1024, module.get()));
  EXPECT_TRUE(changed);

  // All computations should have rematerialized instructions added.
  EXPECT_EQ(entry_computation->instruction_count(), 9);
  EXPECT_EQ(middle_computation->instruction_count(), 9);
  EXPECT_EQ(inner_computation->instruction_count(), 9);
}

TEST_F(HloRematerializationTest, RngNotRematerialized) {
  // Test that a single rng is not rematerialized:
  //
  // Entry computation:
  //   F32[] %param = {...}
  //   F32[1024] rng = rng(param)
  //   F32[1024] tanh = tanh(rng)
  //   F32[1024] exp = exp(rng)
  //   F32[1024] add_0 = add(rng, tanh)              // LIVE: add_0 + rng +
  //                                                 //       tanh + exp
  //
  //   F32[1024] add_1 = add(rng, add(exp, add_0))   // LIVE: add_1 + add_0 +
  //                                                 //       rng + tanh + exp
  //
  //   F32[1024] add_2 = add(rng, add(tanh, add_1))  // LIVE: add_2 + add_1 +
  //                                                 //       rng + tanh + exp
  auto module = CreateNewVerifiedModule();

  auto builder = HloComputation::Builder(TestName());
  auto param = builder.AddInstruction(
      HloInstruction::CreateParameter(0, scalar_shape_, "param"));
  auto rng = builder.AddInstruction(HloInstruction::CreateRng(
      vec1024_shape_, RandomDistribution::RNG_UNIFORM, {param, param}));
  auto tanh = builder.AddInstruction(
      HloInstruction::CreateUnary(vec1024_shape_, HloOpcode::kTanh, rng));
  auto exp = builder.AddInstruction(
      HloInstruction::CreateUnary(vec1024_shape_, HloOpcode::kExp, rng));
  auto add_0 = builder.AddInstruction(
      HloInstruction::CreateBinary(vec1024_shape_, HloOpcode::kAdd, rng, tanh));
  auto add_1 = builder.AddInstruction(HloInstruction::CreateBinary(
      vec1024_shape_, HloOpcode::kAdd, rng,
      builder.AddInstruction(HloInstruction::CreateBinary(
          vec1024_shape_, HloOpcode::kAdd, exp, add_0))));
  builder.AddInstruction(HloInstruction::CreateBinary(
      vec1024_shape_, HloOpcode::kAdd, rng,
      builder.AddInstruction(HloInstruction::CreateBinary(
          vec1024_shape_, HloOpcode::kAdd, tanh, add_1))));
  HloComputation* entry_computation =
      module->AddEntryComputation(builder.Build());

  auto count_rngs = [](const HloComputation* computation) {
    int64 rng_count = 0;
    for (auto* instruction : computation->instructions()) {
      if (instruction->opcode() == HloOpcode::kRng) {
        ++rng_count;
      }
    }
    return rng_count;
  };
  // Before rematerialization there should be a single broadcast rng in
  // the graph.
  ASSERT_EQ(count_rngs(entry_computation), 1);
  const int64 original_instruction_count =
      entry_computation->instruction_count();
  // Pick a memory limit some where between 24KB (initial peak memory including
  // parameter and output) and 20KB (peak memory possible with
  // rematerialization).
  TF_ASSERT_OK_AND_ASSIGN(
      bool changed,
      RunHloRematerialization(
          /*memory_limit_bytes=*/4 * ByteSizeOf(vec1024_shape_), module.get()));
  EXPECT_TRUE(changed);
  // The rng should not have been rematerialized.
  EXPECT_EQ(count_rngs(entry_computation), 1);
  // There should have been rematerialization.
  EXPECT_GT(entry_computation->instruction_count(), original_instruction_count);
}

TEST_F(HloRematerializationTest, InstructionRematerializedMultipleTimes) {
  // Test that a single instruction is rematerialized several times. Module:
  //
  // Entry computation:
  //   F32[] %param = {...}
  //   F32[1024] %bcast = broadcast(%param)
  //   F32[1024] %add_1 = add(%bcast, bcast)
  //   F32[1024] %call_1 = call(Subcomputation, {%add_1})
  //   F32[1024] %add_2 = add(%bcast, call_1)
  //   F32[1024] %call_2 = call(SubComputation, {%add_2})
  //   F32[1024] %add_3 = add(%bcast, call_2)
  //   F32[1024] %call_3 = call(Subcomputation, {%add_3})
  //   F32[1024] %add_4 = add(%bcast, call_3)
  //
  // Subcomputation:
  //   F32[1024] %param = {...}
  //   F32[2048] %concat = concat({%param, %param})
  //   F32[1024] %slice = slice(%concat)
  //
  // The value %bcast is live across each call of Subcomputation (which requires
  // 8KB) though the value is not used in the calls. Rematerializing %bcast
  // across these calls reduces peak memory use from ~20KB down to ~16KB.
  auto module = CreateNewVerifiedModule();

  HloComputation* subcomputation = nullptr;
  {
    auto builder = HloComputation::Builder(TestName() + ".subcomputation");
    auto param = builder.AddInstruction(
        HloInstruction::CreateParameter(0, vec1024_shape_, "param"));
    auto concat = builder.AddInstruction(HloInstruction::CreateConcatenate(
        ShapeUtil::MakeShape(xla::F32, {2048}), {param, param},
        /*dimension=*/0));
    builder.AddInstruction(HloInstruction::CreateSlice(
        vec1024_shape_, concat, /*start_indices=*/{0},
        /*limit_indices=*/{1024}, /*strides=*/{1}));
    subcomputation = module->AddEmbeddedComputation(builder.Build());
  }

  auto builder = HloComputation::Builder(TestName());
  auto param = builder.AddInstruction(
      HloInstruction::CreateParameter(0, scalar_shape_, "param"));
  auto bcast = builder.AddInstruction(
      HloInstruction::CreateBroadcast(vec1024_shape_, param, {}));
  auto add_1 = builder.AddInstruction(HloInstruction::CreateBinary(
      vec1024_shape_, HloOpcode::kAdd, bcast, bcast));
  auto call_1 = builder.AddInstruction(
      HloInstruction::CreateCall(vec1024_shape_, {add_1}, subcomputation));
  auto add_2 = builder.AddInstruction(HloInstruction::CreateBinary(
      vec1024_shape_, HloOpcode::kAdd, bcast, call_1));
  auto call_2 = builder.AddInstruction(
      HloInstruction::CreateCall(vec1024_shape_, {add_2}, subcomputation));
  auto add_3 = builder.AddInstruction(HloInstruction::CreateBinary(
      vec1024_shape_, HloOpcode::kAdd, bcast, call_2));
  auto call_3 = builder.AddInstruction(
      HloInstruction::CreateCall(vec1024_shape_, {add_3}, subcomputation));
  auto add_4 = builder.AddInstruction(HloInstruction::CreateBinary(
      vec1024_shape_, HloOpcode::kAdd, bcast, call_3));
  HloComputation* entry_computation =
      module->AddEntryComputation(builder.Build());

  auto count_broadcasts = [](const HloComputation* computation) {
    int64 bcast_count = 0;
    for (auto* instruction : computation->instructions()) {
      if (instruction->opcode() == HloOpcode::kBroadcast) {
        bcast_count++;
      }
    }
    return bcast_count;
  };

  // Before rematerialization there should be a single broadcast instruction in
  // the graph.
  EXPECT_EQ(count_broadcasts(entry_computation), 1);
  EXPECT_EQ(entry_computation->instruction_count(), 9);

  EXPECT_EQ(add_2->operand(0), bcast);
  EXPECT_EQ(add_3->operand(0), bcast);
  EXPECT_EQ(add_4->operand(0), bcast);

  // Pick a memory limit some where between 24KB (initial peak memory including
  // parameter and output) and 20KB (peak memory possible with
  // rematerialization).
  TF_ASSERT_OK_AND_ASSIGN(bool changed,
                          RunHloRematerialization(
                              /*memory_limit_bytes=*/22 * 1024, module.get()));
  EXPECT_TRUE(changed);

  // The broadcast should have been rematerialized 3 times.
  EXPECT_EQ(count_broadcasts(entry_computation), 4);
  EXPECT_EQ(entry_computation->instruction_count(), 12);

  // The operands of add_2, add_3, and add_4 should all be rematerialized
  // broadcasts.
  EXPECT_NE(add_2->operand(0), bcast);
  EXPECT_THAT(add_2->operand(0), op::Broadcast(param));
  EXPECT_NE(add_3->operand(0), bcast);
  EXPECT_THAT(add_3->operand(0), op::Broadcast(param));
  EXPECT_NE(add_4->operand(0), bcast);
  EXPECT_THAT(add_4->operand(0), op::Broadcast(param));
}

TEST_F(HloRematerializationTest, CopyNotRematerialized) {
  // Test that copies are not rematerialized.
  auto module = CreateNewVerifiedModule();

  auto builder = HloComputation::Builder(TestName());
  auto param = builder.AddInstruction(
      HloInstruction::CreateParameter(0, vec1024_shape_, "param"));

  auto copy = builder.AddInstruction(
      HloInstruction::CreateUnary(vec1024_shape_, HloOpcode::kCopy, param));

  auto negate_a_1 = builder.AddInstruction(
      HloInstruction::CreateUnary(vec1024_shape_, HloOpcode::kNegate, copy));

  auto negate_a_2 = builder.AddInstruction(HloInstruction::CreateUnary(
      vec1024_shape_, HloOpcode::kNegate, negate_a_1));

  auto negate_b_1 = builder.AddInstruction(
      HloInstruction::CreateUnary(vec1024_shape_, HloOpcode::kNegate, copy));

  auto negate_b_2 = builder.AddInstruction(HloInstruction::CreateUnary(
      vec1024_shape_, HloOpcode::kNegate, negate_b_1));

  builder.AddInstruction(HloInstruction::CreateTuple({negate_a_2, negate_b_2}));

  HloComputation* entry_computation =
      module->AddEntryComputation(builder.Build());

  TF_ASSERT_OK_AND_ASSIGN(bool changed,
                          RunHloRematerialization(
                              /*memory_limit_bytes=*/1 * 1024, module.get()));

  auto count_copies = [](const HloComputation* computation) {
    int64 copy_count = 0;
    for (auto* instruction : computation->instructions()) {
      if (instruction->opcode() == HloOpcode::kCopy) {
        copy_count++;
      }
    }
    return copy_count;
  };
  EXPECT_TRUE(changed);

  EXPECT_EQ(count_copies(entry_computation), 1);
}

class IndirectUseTest : public HloRematerializationTest,
                        public ::testing::WithParamInterface<bool> {};

TEST_P(IndirectUseTest, IndirectUseNotRematerialized) {
  // Test that an rematerializable instruction is not rematerialized if it has
  // an indirect use. Test is parameterized on whether the value has an indirect
  // use, and the instruction should be rematerialized iff the value has no
  // indirect use. Module:
  //
  // Entry computation:
  //   F32[] %param = {...}
  //   F32[1024] %bcast = broadcast(%param)
  //   F32[1024] %add_1 = add(%bcast, bcast)
  //   F32[1024] %call = call(Subcomputation, {%add_1})
  //   F32[1024] %add_2 = add(%bcast, call)
  //   {F32[1024], F32[1024]} %tuple = tuple(%bcast, %add_2)
  //   F32[1024] %gte = GetTupleElememt(%tuple, 0)
  //   F32[1024] %negate = negate(%gte)
  //
  // Subcomputation:
  //   F32[1024] %param = {...}
  //   F32[2048] %concat = concat({%param, %param})
  //   F32[1024] %slice = slice(%concat)
  //
  // The value %bcast is live across the call and rematerialization of %bcast
  // across that point would reduce peak memory use by 4KB. However, %bcast is
  // used indirectly in the %negate so rematerialization should not happen.
  //
  // This test is parameterized on whether the broadcast has an indirect use or
  // not. The indirect use is controlled by the index of the GetTupleElement
  // instruction. If the element is 0, then the %negate operand aliases %bcast
  // (ie %bcast is used indirectly by %negate), otherwise the %negate operand
  // aliases %add_2.
  const bool indirectly_used = GetParam();
  auto module = CreateNewVerifiedModule();

  HloComputation* subcomputation = nullptr;
  {
    auto builder = HloComputation::Builder(TestName() + ".subcomputation");
    auto param = builder.AddInstruction(
        HloInstruction::CreateParameter(0, vec1024_shape_, "param"));
    auto concat = builder.AddInstruction(HloInstruction::CreateConcatenate(
        ShapeUtil::MakeShape(xla::F32, {2048}), {param, param},
        /*dimension=*/0));
    builder.AddInstruction(HloInstruction::CreateSlice(
        vec1024_shape_, concat, /*start_indices=*/{0},
        /*limit_indices=*/{1024}, /*strides=*/{1}));
    subcomputation = module->AddEmbeddedComputation(builder.Build());
  }

  auto builder = HloComputation::Builder(TestName());
  auto param = builder.AddInstruction(
      HloInstruction::CreateParameter(0, scalar_shape_, "param"));
  auto bcast = builder.AddInstruction(
      HloInstruction::CreateBroadcast(vec1024_shape_, param, {}));
  auto add_1 = builder.AddInstruction(HloInstruction::CreateBinary(
      vec1024_shape_, HloOpcode::kAdd, bcast, bcast));
  auto call_1 = builder.AddInstruction(
      HloInstruction::CreateCall(vec1024_shape_, {add_1}, subcomputation));
  auto add_2 = builder.AddInstruction(HloInstruction::CreateBinary(
      vec1024_shape_, HloOpcode::kAdd, bcast, call_1));
  auto tuple =
      builder.AddInstruction(HloInstruction::CreateTuple({bcast, add_2}));
  auto gte = builder.AddInstruction(HloInstruction::CreateGetTupleElement(
      vec1024_shape_, tuple, indirectly_used ? 0 : 1));
  builder.AddInstruction(
      HloInstruction::CreateUnary(vec1024_shape_, HloOpcode::kNegate, gte));
  HloComputation* entry_computation =
      module->AddEntryComputation(builder.Build());

  EXPECT_EQ(entry_computation->instruction_count(), 8);

  // Pick a memory limit some where between 24KB (initial peak memory including
  // parameter and output) and 20KB (peak memory possible with
  // rematerialization).
  TF_ASSERT_OK_AND_ASSIGN(bool changed,
                          RunHloRematerialization(
                              /*memory_limit_bytes=*/22 * 1024, module.get()));
  // Rematerialization should only occur if the rematerializable instruction has
  // no indirect uses.
  if (indirectly_used) {
    EXPECT_FALSE(changed);
    EXPECT_EQ(entry_computation->instruction_count(), 8);
  } else {
    EXPECT_TRUE(changed);
    EXPECT_EQ(entry_computation->instruction_count(), 9);
  }
}

INSTANTIATE_TEST_SUITE_P(IndirectUseTestInstantiation, IndirectUseTest,
                         ::testing::Values(true, false));

}  // namespace

}  // namespace xla


text: return require("ui/border")

redpajama_set_name: RedPajamaGithub


text:
<?php
/**
 *
 * @author nblackwe
 *
 */
class FormTest extends PHPUnit_Framework_TestCase
{

    protected function _includeCore()
    {
        if (!defined('_JEXEC')) {
            define('_JOOMLA', 1);
        }

/**
 * use php-core-app a web-app framework for php
 * and joomla.
 */
        include_once dirname(__DIR__) . '/forms/php-core-app/core.php';
        Core::Parameters()->disableCaching();
        Core::Parameters()->disableCompression();

        Core::HTML()->setDomain('example.com')->setScriptPath('index.php');

    }

    /**
     * @runInSeparateProcess
     */
    public function testFormsMetadata()
    {

        $this->_includeCore();

        include_once Core::LibDir() . DS . 'easycsv' . DS . 'EasyCsv.php';
        include_once dirname(__DIR__) . DS . 'forms/database' . DS . 'ScheduleDatabase.php';
        include_once dirname(__DIR__) . DS . 'forms/lib' . DS . 'RWAForm.php';
        $db = ScheduleDatabase::GetInstance();

        $fields = RWAForm::GetFieldNames('scheduled');
        $db->iterateAllSchedules(
            function ($record) use ($fields) {

                $formdata = get_object_vars(json_decode($record->formData));

                echo 'Missing Fields: ' . print_r(array_diff($fields, array_keys($formdata)), true) . "\n";
                echo 'Extra Fields: ' . print_r(array_diff(array_keys($formdata), $fields), true) . "\n";

            });

        $fields = RWAForm::GetFieldNames('addendum');
        $db->iterateAllAddendums(
            function ($record) use ($fields) {

                $formdata = get_object_vars(json_decode($record->formData));

                echo 'Missing Fields: ' . print_r(array_diff($fields, array_keys($formdata)), true) . "\n";
                echo 'Extra Fields: ' . print_r(array_diff(array_keys($formdata), $fields), true) . "\n";

            });

        $fields = RWAForm::GetFieldNames('quarterly');
        $db->iterateAllQuarterlys(
            function ($record) use ($fields) {

                $formdata = get_object_vars(json_decode($record->formData));

                echo 'Missing Fields: ' . print_r(array_diff($fields, array_keys($formdata)), true) . "\n";
                echo 'Extra Fields: ' . print_r(array_diff(array_keys($formdata), $fields), true) . "\n";

            });

    }

    /**
     * @runInSeparateProcess
     */
    public function testExportFormsMetadata()
    {

        $this->_includeCore();

        include_once Core::LibDir() . DS . 'easycsv' . DS . 'EasyCsv.php';
        include_once dirname(__DIR__) . DS . 'forms/database' . DS . 'ScheduleDatabase.php';
        include_once dirname(__DIR__) . DS . 'forms/lib' . DS . 'RWAForm.php';
        $db = ScheduleDatabase::GetInstance();

        $fieldsNames = RWAForm::GetFieldNames('scheduled');
        $fieldValuesArray = array();

        $db->iterateAllSchedules(
            function ($record) use ($fieldsNames, &$fieldValuesArray) {

                $form = get_object_vars(json_decode($record->formData));
                $values = array();
                foreach ($fieldsNames as $field) {
                    if (key_exists($field, $form)) {
                        $values[] = $form[$field];
                    } else {
                        $values[] = "";
                    }
                }

                $fieldValuesArray[] = $values;

            });

        echo print_r($fieldsNames, true) . print_r($fieldValuesArray, true) . "\n";

    }

}


text:
<?php

namespace Assetic\Test\Filter;

use Assetic\Asset\StringAsset;
use Assetic\Filter\GssFilter;

/**
 * @group integration
 */
class GssFilterTest extends FilterTestCase
{
    protected function setUp()
    {
        if (!$javaBin = $this->findExecutable('java', 'JAVA_BIN')) {
            $this->markTestSkipped('Unable to find `java` executable.');
        }

        if (!isset($_SERVER['GSS_JAR'])) {
            $this->markTestSkipped('There is no GSS_JAR environment variable.');
        }

        $this->filter = new GssFilter($_SERVER['GSS_JAR'], $javaBin);
    }

    public function testCompile()
    {
        $input = <<<EOF
@def BG_COLOR rgb(235, 239, 249);
body {background-color: BG_COLOR;}
EOF;

        $expected = <<<EOF
body{background-color:#ebeff9}
EOF;

        $asset = new StringAsset($input);
        $asset->load();

        $this->filter->filterLoad($asset);

        $this->assertEquals($expected, $asset->getContent());
    }
}


text:
class SlovarKeyError(KeyError):
    pass

class SlovarValueError(ValueError):
    pass

def set_exceptions(key_error, value_error):
    global SlovarKeyError, SlovarValueError
    SlovarKeyError = key_error
    SlovarValueError = value_error


text:
<META HTTP-EQUIV='Content-Security-Policy' CONTENT="base-uri 'self'; default-src 'self' https://www.google-analytics.com ; script-src 'self' 'unsafe-inline' 'nonce-77AABBCC' 'strict-dynamic' https: http: ; style-src 'self' 'unsafe-inline' https://netdna.bootstrapcdn.com ; font-src 'self' https://netdna.bootstrapcdn.com; ">
{% for mainpage in site.pages reversed %}
{% assign pagecat=page.categories[1] %}
{% if mainpage.category %}
    {% if mainpage.lang==page.lang and mainpage.category==pagecat %}
        {% assign current_title=page.title | append: ' | ' | append: mainpage.title %}
        {% assign mainpage_title=mainpage.title %}
        {% assign current_github_url=mainpage.github_url %}
        {% assign full_article = mainpage.url %}
        {% assign maincover_image = mainpage.cover_image %}
        {% break %}
    {% else %}
        {% assign current_title=page.title %}
        {% assign current_github_url=page.github_url %}
    {% endif %}
{% endif %}
{% endfor %}
{% if page.cover_image %}
{% assign main_image = page.cover_image %}
{% else %}
{% assign main_image = maincover_image %}
{% endif %}
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--[if lte IE 8]><script src="js/ie/html5shiv.js"></script><![endif]-->
<link href="/css/flex-layout-attribute.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/css/fbmain.css">
<link rel="stylesheet" href="/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/main.css" />
<link rel="stylesheet" href="/css/friendly.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="/css/ie8.css" /><![endif]-->
<!--[if lte IE 9]><link rel="stylesheet" href="/css/ie9.css" /><![endif]-->
<link rel="apple-touch-icon" sizes="180x180" href="/img/icon/apple-touch-icon.png">
<link rel="icon" type="image/png" href="/img/icon/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/img/icon/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/img/icon/manifest.json">
<link rel="mask-icon" href="/img/icon/safari-pinned-tab.svg">
<link rel="shortcut icon" href="/img/icon/favicon.ico">
<meta name="msapplication-config" content="/img/icon/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<meta property="og:url" content="{{ site.url }}{{ page.url }}" />
<meta property="og:type" content="article" />
<meta property="og:title" content="{{ current_title | replace: site.stmb_key , site.stmb }}" />
<meta property="og:description" content="{{ page.description | replace: site.stmb_key , site.stmb }}" />
{% if page.category %}
<meta property="og:image" content="{{ site.url }}/{{ page.lang }}/{{ page.category }}/img/{{ main_image }}" />
{% else %}
<meta property="og:image" content="{{ site.url }}/{{ page.categories | join: '/' }}/img/{{ main_image }}" />
{% endif %}
<meta property="fb:app_id" content="{{ site.facebook_app_id }}"/>
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@{{ site.twitter_username }}">
<meta name="twitter:title" content="{{ current_title | replace: site.stmb_key , site.stmb }}">
<meta name="twitter:description" content="{{ page.description | replace: site.stmb_key , site.stmb }}">
<meta name="twitter:creator" content="@{{ site.data.people[page.author].twitter }}">
{% if page.category %}
<meta name="twitter:image" content="{{ site.url }}/{{ page.lang }}/{{ page.category }}/img/{{ main_image }}">
{% else %}
<meta name="twitter:image" content="{{ site.url }}/{{ page.categories | join: '/' }}/img/{{ main_image }}">
{% endif %}
<meta name="twitter:domain" content="{{ site.domain }}">
<meta name="description" content="{{ page.description | replace: site.stmb_key , site.stmb }}">
<title>{{ current_title | replace: site.stmb_key , site.stmb }}</title>


text:
package org.auraframework.test.source;

import java.util.Set;

import javax.inject.Inject;

import org.auraframework.annotations.Annotations.ServiceComponent;
import org.auraframework.def.DefDescriptor;
import org.auraframework.def.DefDescriptor.DefType;
import org.auraframework.def.Definition;
import org.auraframework.def.DescriptorFilter;
import org.auraframework.ds.servicecomponent.Controller;
import org.auraframework.service.DefinitionService;
import org.auraframework.system.Annotations.AuraEnabled;
import org.auraframework.system.Annotations.Key;

@ServiceComponent
public class StringSourceLoaderController implements Controller {
    @Inject
    private DefinitionService definitionService;

    @Inject
    private StringSourceLoader stringSourceLoader;

    @SuppressWarnings("unchecked")
    @AuraEnabled
    public <D extends Definition, B extends Definition> DefDescriptor<D> createStringSourceDescriptor(
            @Key("namePrefix") String namePrefix,
            @Key("defClass") String defClass,
            @Key("bundleName") String bundleName,
            @Key("bundleDefClass") String bundleDefClass)
            throws ClassNotFoundException {

        DefDescriptor<B> bundleDescriptor = null;
        if (bundleName != null) {
            bundleDescriptor = definitionService.getDefDescriptor(bundleName,
                    (Class<B>) Class.forName(bundleDefClass));
        }
        return stringSourceLoader.createStringSourceDescriptor(namePrefix,
                (Class<D>) Class.forName(defClass), bundleDescriptor);
    }

    @SuppressWarnings("unchecked")
    @AuraEnabled
    public <D extends Definition, B extends Definition> void putSource(
            @Key("name") String name, @Key("defClass") String defClass,
            @Key("bundleName") String bundleName,
            @Key("bundleDefClass") String bundleDefClass,
            @Key("contents") String contents,
            @Key("overwrite") boolean overwrite,
            @Key("access") String access)
            throws ClassNotFoundException {

        DefDescriptor<B> bundleDescriptor = null;
        if (bundleName != null) {
            bundleDescriptor = definitionService.getDefDescriptor(bundleName,
                    (Class<B>) Class.forName(bundleDefClass));
        }
        DefDescriptor<D> descriptor = definitionService.getDefDescriptor(name,
                (Class<D>) Class.forName(defClass), bundleDescriptor);
        stringSourceLoader.putSource(descriptor, contents, overwrite, StringSourceLoader.NamespaceAccess.valueOf(access));
    }

    @SuppressWarnings("unchecked")
    @AuraEnabled
    public <D extends Definition, B extends Definition> void removeSource(
            @Key("name") String name, @Key("defClass") String defClass,
            @Key("bundleName") String bundleName,
            @Key("bundleDefClass") String bundleDefClass) throws ClassNotFoundException {
        DefDescriptor<B> bundleDescriptor = null;
        if (bundleName != null) {
            bundleDescriptor = definitionService.getDefDescriptor(bundleName,
                    (Class<B>) Class.forName(bundleDefClass));
        }
        DefDescriptor<D> descriptor = definitionService.getDefDescriptor(name,
                (Class<D>) Class.forName(defClass), bundleDescriptor);
        stringSourceLoader.removeSource(descriptor);
    }

    @AuraEnabled
    public Set<String> getNamespaces() {
        return stringSourceLoader.getNamespaces();
    }

    @AuraEnabled
    public Set<DefType> getDefTypes() {
        return stringSourceLoader.getDefTypes();
    }

    @AuraEnabled
    public Set<DefDescriptor<?>> findForDescriptorFilter(
            @Key("descriptorFilter") DescriptorFilter descriptorFilter) {
        return stringSourceLoader.find(descriptorFilter);
    }

    @AuraEnabled
    boolean isInternalNamespace(@Key("namespace") String namespace) {
        return stringSourceLoader.isInternalNamespace(namespace);
    }
}


text:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(OVROverlay))]
public class OVROverlayEditor : Editor
{

	/// <summary>
	/// Common Video Types, to ease source and dest rect creation
	/// </summary>
	public enum StereoType
	{
		Custom = 0,
		Mono = 1,
		Stereo = 2,
		StereoLeftRight = 3,
		StereoTopBottom = 4,
	}

	public enum DisplayType
	{
		Custom = 0,
		Full = 1,
		Half = 2
	}

	private bool sourceRectsVisible = false;
	private bool destRectsVisible = false;

	private Material _SrcRectMaterial;
	protected Material SrcRectMaterial
	{
		get
		{
			if (_SrcRectMaterial == null)
			{
				string[] shaders = AssetDatabase.FindAssets("OVROverlaySrcRectEditor");

				if (shaders.Length > 0)
				{
					Shader shader = (Shader)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(shaders[0]), typeof(Shader));

					if (shader != null)
					{
						_SrcRectMaterial = new Material(shader);
					}
				}
			}
			return _SrcRectMaterial;
		}
	}

	private Material _DestRectMaterial;
	protected Material DestRectMaterial
	{
		get
		{
			if (_DestRectMaterial == null)
			{
				string[] shaders = AssetDatabase.FindAssets("OVROverlayDestRectEditor");

				if (shaders.Length > 0)
				{
					Shader shader = (Shader)AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(shaders[0]), typeof(Shader));

					if (shader != null)
					{
						_DestRectMaterial = new Material(shader);
					}
				}
			}
			return _DestRectMaterial;
		}
	}

	private TextureRect _DraggingRect;
	private Side _DraggingSide;

	enum TextureRect
	{
		None,
		SrcLeft,
		SrcRight,
		DestLeft,
		DestRight
	}

	enum Side
	{
		Left,
		Right,
		Top,
		Bottom
	}

	public override void OnInspectorGUI()
	{
		OVROverlay overlay = (OVROverlay)target;
		if (overlay == null)
		{
			return;
		}

		EditorGUILayout.LabelField("Display Order", EditorStyles.boldLabel);
		overlay.currentOverlayType = (OVROverlay.OverlayType)EditorGUILayout.EnumPopup(new GUIContent("Current Overlay Type", "Whether this overlay should layer behind the scene or in front of it"), overlay.currentOverlayType);
		overlay.compositionDepth = EditorGUILayout.IntField(new GUIContent("Composition Depth", "Depth value used to sort OVROverlays in the scene, smaller value appears in front"), overlay.compositionDepth);
		overlay.noDepthBufferTesting = EditorGUILayout.Toggle(new GUIContent("No Depth Buffer Testing", "The noDepthBufferTesting will stop layer's depth buffer compositing even if the engine has \"Shared Depth Buffer\" enabled"), overlay.noDepthBufferTesting);
		EditorGUILayout.Space();

		EditorGUILayout.LabelField(new GUIContent("Overlay Shape", "The shape of this overlay"), EditorStyles.boldLabel);
		overlay.currentOverlayShape = (OVROverlay.OverlayShape)EditorGUILayout.EnumPopup(new GUIContent("Overlay Shape", "The shape of this overlay"), overlay.currentOverlayShape);
		EditorGUILayout.Space();

		EditorGUILayout.Separator();
		EditorGUILayout.LabelField("Textures", EditorStyles.boldLabel);

#if UNITY_ANDROID
		bool lastIsExternalSurface = overlay.isExternalSurface;
		overlay.isExternalSurface = EditorGUILayout.Toggle(new GUIContent("Is External Surface", "On Android, retrieve an Android Surface object to render to (e.g., video playback)"), overlay.isExternalSurface);

		if (lastIsExternalSurface)
		{
			overlay.externalSurfaceWidth = EditorGUILayout.IntField("External Surface Width", overlay.externalSurfaceWidth);
			overlay.externalSurfaceHeight = EditorGUILayout.IntField("External Surface Height", overlay.externalSurfaceHeight);
			overlay.isProtectedContent = EditorGUILayout.Toggle(new GUIContent("Is Protected Content", "The external surface has L1 widevine protection."), overlay.isProtectedContent);
		}
		else
#endif
		{
			if (overlay.textures == null)
			{
				overlay.textures = new Texture[2];
			}
			if (overlay.textures.Length < 2)
			{
				Texture[] tmp = new Texture[2];
				for (int i = 0; i < overlay.textures.Length; i++)
				{
					tmp[i] = overlay.textures[i];
				}
				overlay.textures = tmp;
			}

			var labelControlRect = EditorGUILayout.GetControlRect();
			EditorGUI.LabelField(new Rect(labelControlRect.x, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Left Texture", "Texture used for the left eye"));
			EditorGUI.LabelField(new Rect(labelControlRect.x + labelControlRect.width / 2, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Right Texture", "Texture used for the right eye"));

			var textureControlRect = EditorGUILayout.GetControlRect(GUILayout.Height(64));

			overlay.textures[0] = (Texture)EditorGUI.ObjectField(new Rect(textureControlRect.x, textureControlRect.y, 64, textureControlRect.height), overlay.textures[0], typeof(Texture), true);
			Texture right = (Texture)EditorGUI.ObjectField(new Rect(textureControlRect.x + textureControlRect.width / 2, textureControlRect.y, 64, textureControlRect.height), overlay.textures[1] != null ? overlay.textures[1] : overlay.textures[0], typeof(Texture), true);
			if (right == overlay.textures[0])
			{
				overlay.textures[1] = null;
			}
			else
			{
				overlay.textures[1] = right;
			}

			overlay.isDynamic = EditorGUILayout.Toggle(new GUIContent("Dynamic Texture", "This texture will be updated dynamically at runtime (e.g., Video)"), overlay.isDynamic);
#if !UNITY_ANDROID
            overlay.isProtectedContent = EditorGUILayout.Toggle(new GUIContent("Is Protected Content", "The texture has copy protection, e.g., HDCP"), overlay.isProtectedContent);
#endif
		}
		if (overlay.currentOverlayShape == OVROverlay.OverlayShape.Cylinder || overlay.currentOverlayShape == OVROverlay.OverlayShape.Equirect || overlay.currentOverlayShape == OVROverlay.OverlayShape.Quad) {

			EditorGUILayout.Separator();
			EditorGUILayout.Space();
			EditorGUILayout.LabelField("Texture Rects", EditorStyles.boldLabel);

			bool lastOverrideTextureRectMatrix = overlay.overrideTextureRectMatrix;
			overlay.overrideTextureRectMatrix = !EditorGUILayout.Toggle(new GUIContent("Use Default Rects", overlay.textures[1] == null ? "If you need to use a single texture as a stereo image, uncheck this box" : "Uncheck this box if you need to clip you textures or layer"), !overlay.overrideTextureRectMatrix);

			if (lastOverrideTextureRectMatrix)
			{
				sourceRectsVisible = EditorGUILayout.Foldout(sourceRectsVisible, new GUIContent("Source Rects", "What portion of the source texture will ultimately be shown in each eye."));

				if (sourceRectsVisible)
				{
					var mat = SrcRectMaterial;

					if (mat != null)
					{
						Rect drawRect = EditorGUILayout.GetControlRect(GUILayout.Height(128 + 8));
						mat.SetVector("_SrcRectLeft", new Vector4(Mathf.Max(0.0f, overlay.srcRectLeft.x), Mathf.Max(0.0f, overlay.srcRectLeft.y), Mathf.Min(1.0f - overlay.srcRectLeft.x, overlay.srcRectLeft.width), Mathf.Min(1.0f - overlay.srcRectLeft.y, overlay.srcRectLeft.height)));
						mat.SetVector("_SrcRectRight", new Vector4(Mathf.Max(0.0f, overlay.srcRectRight.x), Mathf.Max(0.0f, overlay.srcRectRight.y), Mathf.Min(1.0f - overlay.srcRectRight.x, overlay.srcRectRight.width), Mathf.Min(1.0f - overlay.srcRectRight.y, overlay.srcRectRight.height)));
						// center our draw rect
						var drawRectCentered = new Rect(drawRect.x + drawRect.width / 2 - 128 - 4, drawRect.y, 256 + 8, drawRect.height);
						EditorGUI.DrawPreviewTexture(drawRectCentered, overlay.textures[0] ?? Texture2D.blackTexture, mat);

						var drawRectInset = new Rect(drawRectCentered.x + 4, drawRectCentered.y + 4, drawRectCentered.width - 8, drawRectCentered.height - 8);
						UpdateRectDragging(drawRectInset, drawRectInset, TextureRect.SrcLeft, TextureRect.SrcRight, ref overlay.srcRectLeft, ref overlay.srcRectRight);
						CreateCursorRects(drawRectInset, overlay.srcRectLeft);
						CreateCursorRects(drawRectInset, overlay.srcRectRight);
					}

					var labelControlRect = EditorGUILayout.GetControlRect();
					EditorGUI.LabelField(new Rect(labelControlRect.x, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Left Source Rect", "The rect in the source image that will be displayed on the left eye layer"));
					EditorGUI.LabelField(new Rect(labelControlRect.x + labelControlRect.width / 2, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Right Source Rect", "The rect in the source image that will be displayed on the right eye layer"));

					var rectControlRect = EditorGUILayout.GetControlRect(GUILayout.Height(34));

					overlay.srcRectLeft = Clamp01(EditorGUI.RectField(new Rect(rectControlRect.x, rectControlRect.y, rectControlRect.width / 2 - 20, rectControlRect.height), overlay.srcRectLeft));
					overlay.srcRectRight = Clamp01(EditorGUI.RectField(new Rect(rectControlRect.x + rectControlRect.width / 2, rectControlRect.y, rectControlRect.width / 2 - 20, rectControlRect.height), overlay.srcRectRight));

					EditorGUILayout.BeginHorizontal();
					if (overlay.textures[1] != null)
					{
						if (GUILayout.Button(new GUIContent("Reset To Default", "Reset Source Rects to default")))
						{
							SetRectsByVideoType(overlay, StereoType.Stereo, DisplayType.Custom);
						}
					}
					else
					{
						if (GUILayout.Button(new GUIContent("Monoscopic", "Display the full Texture in both eyes")))
						{
							SetRectsByVideoType(overlay, StereoType.Mono, DisplayType.Custom);
						}
						if (GUILayout.Button(new GUIContent("Stereo Left/Right", "The left half of the texture is displayed in the left eye, and the right half in the right eye")))
						{
							SetRectsByVideoType(overlay, StereoType.StereoLeftRight, DisplayType.Custom);
						}
						if (GUILayout.Button(new GUIContent("Stereo Top/Bottom", "The top half of the texture is displayed in the left eye, and the bottom half in the right eye")))
						{
							SetRectsByVideoType(overlay, StereoType.StereoTopBottom, DisplayType.Custom);
						}
					}
					EditorGUILayout.EndHorizontal();

				}
				destRectsVisible = EditorGUILayout.Foldout(destRectsVisible, new GUIContent("Destination Rects", "What portion of the destination texture that the source will be rendered into."));
				if (destRectsVisible)
				{

					var mat = DestRectMaterial;

					if (mat != null)
					{
						Rect drawRect = EditorGUILayout.GetControlRect(GUILayout.Height(128 + 8));
						mat.SetVector("_SrcRectLeft", new Vector4(Mathf.Max(0.0f, overlay.srcRectLeft.x), Mathf.Max(0.0f, overlay.srcRectLeft.y), Mathf.Min(1.0f - overlay.srcRectLeft.x, overlay.srcRectLeft.width), Mathf.Min(1.0f - overlay.srcRectLeft.y, overlay.srcRectLeft.height)));
						mat.SetVector("_SrcRectRight", new Vector4(Mathf.Max(0.0f, overlay.srcRectRight.x), Mathf.Max(0.0f, overlay.srcRectRight.y), Mathf.Min(1.0f - overlay.srcRectRight.x, overlay.srcRectRight.width), Mathf.Min(1.0f - overlay.srcRectRight.y, overlay.srcRectRight.height)));
						mat.SetVector("_DestRectLeft", new Vector4(Mathf.Max(0.0f, overlay.destRectLeft.x), Mathf.Max(0.0f, overlay.destRectLeft.y), Mathf.Min(1.0f - overlay.destRectLeft.x, overlay.destRectLeft.width), Mathf.Min(1.0f - overlay.destRectLeft.y, overlay.destRectLeft.height)));
						mat.SetVector("_DestRectRight", new Vector4(Mathf.Max(0.0f, overlay.destRectRight.x), Mathf.Max(0.0f, overlay.destRectRight.y), Mathf.Min(1.0f - overlay.destRectRight.x, overlay.destRectRight.width), Mathf.Min(1.0f - overlay.destRectRight.y, overlay.destRectRight.height)));
						mat.SetColor("_BackgroundColor", EditorGUIUtility.isProSkin ? (Color)new Color32(56, 56, 56, 255) : (Color)new Color32(194, 194, 194, 255));

						var drawRectCentered = new Rect(drawRect.x + drawRect.width / 2 - 128 - 16 - 4, drawRect.y, 256 + 32 + 8, drawRect.height);
						// center our draw rect
						EditorGUI.DrawPreviewTexture(drawRectCentered, overlay.textures[0] ?? Texture2D.blackTexture, mat);

						var drawRectInsetLeft = new Rect(drawRectCentered.x + 4, drawRectCentered.y + 4, drawRectCentered.width / 2 - 20, drawRectCentered.height - 8);
						var drawRectInsetRight = new Rect(drawRectCentered.x + drawRectCentered.width / 2 + 16, drawRectCentered.y + 4, drawRectCentered.width / 2 - 20, drawRectCentered.height - 8);
						UpdateRectDragging(drawRectInsetLeft, drawRectInsetRight, TextureRect.DestLeft, TextureRect.DestRight, ref overlay.destRectLeft, ref overlay.destRectRight);

						CreateCursorRects(drawRectInsetLeft, overlay.destRectLeft);
						CreateCursorRects(drawRectInsetRight, overlay.destRectRight);

					}

					var labelControlRect = EditorGUILayout.GetControlRect();
					EditorGUI.LabelField(new Rect(labelControlRect.x, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Left Destination Rect", "The rect in the destination layer the left eye will display to"));
					EditorGUI.LabelField(new Rect(labelControlRect.x + labelControlRect.width / 2, labelControlRect.y, labelControlRect.width / 2, labelControlRect.height), new GUIContent("Right Destination Rect", "The rect in the destination layer the right eye will display to"));

					var rectControlRect = EditorGUILayout.GetControlRect(GUILayout.Height(34));

					overlay.destRectLeft = Clamp01(EditorGUI.RectField(new Rect(rectControlRect.x, rectControlRect.y, rectControlRect.width / 2 - 20, rectControlRect.height), overlay.destRectLeft));
					overlay.destRectRight = Clamp01(EditorGUI.RectField(new Rect(rectControlRect.x + rectControlRect.width / 2, rectControlRect.y, rectControlRect.width / 2 - 20, rectControlRect.height), overlay.destRectRight));

					if (overlay.currentOverlayShape == OVROverlay.OverlayShape.Equirect)
					{
						EditorGUILayout.BeginHorizontal();
						if (GUILayout.Button(new GUIContent("360 Video", "Display the full 360 layer")))
						{
							SetRectsByVideoType(overlay, StereoType.Custom, DisplayType.Full);
						}
						if (GUILayout.Button(new GUIContent("180 Video", "Display the front 180 layer")))
						{
							SetRectsByVideoType(overlay, StereoType.Custom, DisplayType.Half);
						}
						EditorGUILayout.EndHorizontal();
					}
					else
					{
						if (GUILayout.Button(new GUIContent("Reset To Default", "Reset Source Rects to default")))
						{
							SetRectsByVideoType(overlay, StereoType.Custom, DisplayType.Full);
						}
					}
				}
			}
		}
		EditorGUILayout.Separator();
		EditorGUILayout.LabelField("Color Scale", EditorStyles.boldLabel);
		EditorGUILayout.Space();
		overlay.overridePerLayerColorScaleAndOffset = EditorGUILayout.Toggle(new GUIContent("Override Color Scale", "Manually set color scale and offset of this layer, regardless of what the global values are from OVRManager.SetColorScaleAndOffset()."), overlay.overridePerLayerColorScaleAndOffset);
		if (overlay.overridePerLayerColorScaleAndOffset)
		{
			Vector4 colorScale = EditorGUILayout.Vector4Field(new GUIContent("Color Scale", "Scale that the color values for this overlay will be multiplied by."), overlay.colorScale);
			Vector4 colorOffset = EditorGUILayout.Vector4Field(new GUIContent("Color Offset", "Offset that the color values for this overlay will be added to."), overlay.colorOffset);
			overlay.SetPerLayerColorScaleAndOffset(colorScale, colorOffset);
		}

		EditorUtility.SetDirty(overlay);
	}

	private Rect Clamp01(Rect rect)
	{
		rect.x = Mathf.Clamp01(rect.x);
		rect.y = Mathf.Clamp01(rect.y);
		rect.width = Mathf.Clamp01(rect.width);
		rect.height = Mathf.Clamp01(rect.height);
		return rect;
	}

	private bool IsUnitRect(Rect rect)
	{
		return IsRect(rect, 0, 0, 1, 1);
	}

	private bool IsRect(Rect rect, float x, float y, float w, float h)
	{
		return rect.x == x && rect.y == y && rect.width == w && rect.height == h;
	}

	private StereoType GetStereoType(OVROverlay overlay)
	{
		if (overlay.textures[0] != null && overlay.textures[1] != null)
		{
			if (IsUnitRect(overlay.srcRectLeft) && IsUnitRect(overlay.srcRectRight))
			{
				return StereoType.Stereo;
			}
			else
			{
				return StereoType.Custom;
			}
		}
		else if (overlay.textures[0] != null)
		{
			if (IsUnitRect(overlay.srcRectLeft) && IsUnitRect(overlay.srcRectRight))
			{
				return StereoType.Mono;
			}
			else if (IsRect(overlay.srcRectLeft, 0, 0, 0.5f, 1f) && IsRect(overlay.srcRectRight, 0.5f, 0, 0.5f, 1f))
			{
				return StereoType.StereoLeftRight;
			}
			else if (IsRect(overlay.srcRectLeft, 0, 0, 1f, 0.5f) && IsRect(overlay.srcRectRight, 0f, 0.5f, 1f, 0.5f))
			{
				return StereoType.StereoTopBottom;
			}
			else
			{
				return StereoType.Custom;
			}
		}
		else
		{
			return StereoType.Mono;
		}
	}

	private void SetRectsByVideoType(OVROverlay overlay, StereoType stereoType, DisplayType displayType)
	{
		Rect srcRectLeft, srcRectRight, destRectLeft, destRectRight;

		switch (displayType)
		{
			case DisplayType.Full:
				destRectLeft = destRectRight = new Rect(0, 0, 1, 1);
				break;

			case DisplayType.Half:
				destRectLeft = destRectRight = new Rect(0.25f, 0, 0.5f, 1);
				break;
			default:
				destRectLeft = overlay.destRectLeft;
				destRectRight = overlay.destRectRight;
				break;
		}

		switch (stereoType)
		{
			case StereoType.Mono:
			case StereoType.Stereo:
				srcRectLeft = srcRectRight = new Rect(0, 0, 1, 1);
				break;

			case StereoType.StereoTopBottom:
				srcRectLeft = new Rect(0, 0, 1, 0.5f);
				srcRectRight = new Rect(0, 0.5f, 1, 0.5f);
				break;

			case StereoType.StereoLeftRight:
				srcRectLeft = new Rect(0, 0, 0.5f, 1);
				srcRectRight = new Rect(0.5f, 0, 0.5f, 1);
				break;
			default:
				srcRectLeft = overlay.srcRectLeft;
				srcRectRight = overlay.srcRectRight;
				break;
		}
		overlay.SetSrcDestRects(srcRectLeft, srcRectRight, destRectLeft, destRectRight);
	}

	private void GetCursorPoints(Rect drawRect, Rect selectRect, out Vector2 leftPos, out Vector2 rightPos, out Vector2 topPos, out Vector2 bottomPos)
	{
		leftPos = new Vector2(drawRect.x + selectRect.x * drawRect.width, drawRect.y + (selectRect.y + selectRect.height / 2) * drawRect.height);
		rightPos = new Vector2(drawRect.x + (selectRect.x + selectRect.width) * drawRect.width, drawRect.y + (selectRect.y + selectRect.height / 2) * drawRect.height);
		topPos = new Vector2(drawRect.x + (selectRect.x + selectRect.width / 2) * drawRect.width, drawRect.y + (selectRect.y) * drawRect.height);
		bottomPos = new Vector2(drawRect.x + (selectRect.x + selectRect.width / 2) * drawRect.width, drawRect.y + (selectRect.y + selectRect.height) * drawRect.height);
	}

	private void CreateCursorRects(Rect drawRect, Rect selectRect)
	{
		Vector2 leftPos, rightPos, topPos, bottomPos;
		GetCursorPoints(drawRect, selectRect, out leftPos, out rightPos, out topPos, out bottomPos);

		EditorGUIUtility.AddCursorRect(new Rect(leftPos - 5 * Vector2.one, 10 * Vector2.one), MouseCursor.ResizeHorizontal);
		EditorGUIUtility.AddCursorRect(new Rect(rightPos - 5 * Vector2.one, 10 * Vector2.one), MouseCursor.ResizeHorizontal);
		EditorGUIUtility.AddCursorRect(new Rect(topPos - 5 * Vector2.one, 10 * Vector2.one), MouseCursor.ResizeVertical);
		EditorGUIUtility.AddCursorRect(new Rect(bottomPos - 5 * Vector2.one, 10 * Vector2.one), MouseCursor.ResizeVertical);
	}

	private bool IsOverRectControls(Rect drawRect, Vector2 mousePos, Rect selectRect, ref Side side)
	{
		Vector2 leftPos, rightPos, topPos, bottomPos;
		GetCursorPoints(drawRect, selectRect, out leftPos, out rightPos, out topPos, out bottomPos);

		if ((leftPos - mousePos).sqrMagnitude <= 25)
		{
			side = Side.Left;
			return true;
		}
		if ((rightPos - mousePos).sqrMagnitude <= 25)
		{
			side = Side.Right;
			return true;
		}
		if ((topPos - mousePos).sqrMagnitude <= 25)
		{
			side = Side.Top;
			return true;
		}
		if ((bottomPos - mousePos).sqrMagnitude <= 25)
		{
			side = Side.Bottom;
			return true;
		}
		return false;
	}

	private void UpdateRectDragging(Rect drawingRectLeft, Rect drawingRectRight, TextureRect rectLeftType, TextureRect rectRightType, ref Rect rectLeft, ref Rect rectRight)
	{
		if (!Event.current.isMouse || Event.current.button != 0)
		{
			return;
		}

		if (Event.current.type == EventType.MouseUp)
		{
			_DraggingRect = TextureRect.None;
			return;
		}

		Vector2 mousePos = Event.current.mousePosition;
		if (_DraggingRect == TextureRect.None && Event.current.type == EventType.MouseDown)
		{
			if (IsOverRectControls(drawingRectLeft, mousePos, rectLeft, ref _DraggingSide))
			{
				_DraggingRect = rectLeftType;
			}
			if (_DraggingRect == TextureRect.None || Event.current.shift)
			{
				if (IsOverRectControls(drawingRectRight, mousePos, rectRight, ref _DraggingSide))
				{
					_DraggingRect = rectRightType;
				}
			}
		}

		if (_DraggingRect == rectLeftType)
		{
			SetRectSideValue(drawingRectLeft, mousePos, _DraggingSide, ref rectLeft);
		}
		if (_DraggingRect == rectRightType)
		{
			SetRectSideValue(drawingRectRight, mousePos, _DraggingSide, ref rectRight);
		}
	}

	private void SetRectSideValue(Rect drawingRect, Vector2 mousePos, Side side, ref Rect rect)
	{
		// quantize to 1/32
		float x = Mathf.Clamp01(Mathf.Round(((mousePos.x - drawingRect.x) / drawingRect.width) * 32) / 32.0f);
		float y = Mathf.Clamp01(Mathf.Round(((mousePos.y - drawingRect.y) / drawingRect.height) * 32) / 32.0f);

		switch (side)
		{
			case Side.Left:
				float xMax = rect.xMax;
				rect.x = Mathf.Min(x, xMax);
				rect.width = xMax - rect.x;
				break;
			case Side.Right:
				rect.width = Mathf.Max(0, x - rect.x);
				break;
			case Side.Top:
				float yMax = rect.yMax;
				rect.y = Mathf.Min(y, yMax);
				rect.height = yMax - rect.y;
				break;
			case Side.Bottom:
				rect.height = Mathf.Max(0, y - rect.y);
				break;
		}
	}
}


text:
/*
 * The Plaid API
 *
 * The Plaid REST API. Please see https://plaid.com/docs/api for more details.
 *
 * API version: 2020-09-14_1.205.3
 */

// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.

package plaid

import (
	"encoding/json"
)

// IncomeVerificationDocumentsDownloadRequest IncomeVerificationDocumentsDownloadRequest defines the request schema for `/income/verification/documents/download`.
type IncomeVerificationDocumentsDownloadRequest struct {
	// Your Plaid API `client_id`. The `client_id` is required and may be provided either in the `PLAID-CLIENT-ID` header or as part of a request body.
	ClientId *string `json:"client_id,omitempty"`
	// Your Plaid API `secret`. The `secret` is required and may be provided either in the `PLAID-SECRET` header or as part of a request body.
	Secret *string `json:"secret,omitempty"`
	// The ID of the verification.
	IncomeVerificationId NullableString `json:"income_verification_id,omitempty"`
	// The access token associated with the Item data is being requested for.
	AccessToken NullableString `json:"access_token,omitempty"`
	// The document ID to download. If passed, a single document will be returned in the resulting zip file, rather than all document
	DocumentId NullableString `json:"document_id,omitempty"`
}

// NewIncomeVerificationDocumentsDownloadRequest instantiates a new IncomeVerificationDocumentsDownloadRequest object
// This constructor will assign default values to properties that have it defined,
// and makes sure properties required by API are set, but the set of arguments
// will change when the set of required properties is changed
func NewIncomeVerificationDocumentsDownloadRequest() *IncomeVerificationDocumentsDownloadRequest {
	this := IncomeVerificationDocumentsDownloadRequest{}
	return &this
}

// NewIncomeVerificationDocumentsDownloadRequestWithDefaults instantiates a new IncomeVerificationDocumentsDownloadRequest object
// This constructor will only assign default values to properties that have it defined,
// but it doesn't guarantee that properties required by API are set
func NewIncomeVerificationDocumentsDownloadRequestWithDefaults() *IncomeVerificationDocumentsDownloadRequest {
	this := IncomeVerificationDocumentsDownloadRequest{}
	return &this
}

// GetClientId returns the ClientId field value if set, zero value otherwise.
func (o *IncomeVerificationDocumentsDownloadRequest) GetClientId() string {
	if o == nil || o.ClientId == nil {
		var ret string
		return ret
	}
	return *o.ClientId
}

// GetClientIdOk returns a tuple with the ClientId field value if set, nil otherwise
// and a boolean to check if the value has been set.
func (o *IncomeVerificationDocumentsDownloadRequest) GetClientIdOk() (*string, bool) {
	if o == nil || o.ClientId == nil {
		return nil, false
	}
	return o.ClientId, true
}

// HasClientId returns a boolean if a field has been set.
func (o *IncomeVerificationDocumentsDownloadRequest) HasClientId() bool {
	if o != nil && o.ClientId != nil {
		return true
	}

	return false
}

// SetClientId gets a reference to the given string and assigns it to the ClientId field.
func (o *IncomeVerificationDocumentsDownloadRequest) SetClientId(v string) {
	o.ClientId = &v
}

// GetSecret returns the Secret field value if set, zero value otherwise.
func (o *IncomeVerificationDocumentsDownloadRequest) GetSecret() string {
	if o == nil || o.Secret == nil {
		var ret string
		return ret
	}
	return *o.Secret
}

// GetSecretOk returns a tuple with the Secret field value if set, nil otherwise
// and a boolean to check if the value has been set.
func (o *IncomeVerificationDocumentsDownloadRequest) GetSecretOk() (*string, bool) {
	if o == nil || o.Secret == nil {
		return nil, false
	}
	return o.Secret, true
}

// HasSecret returns a boolean if a field has been set.
func (o *IncomeVerificationDocumentsDownloadRequest) HasSecret() bool {
	if o != nil && o.Secret != nil {
		return true
	}

	return false
}

// SetSecret gets a reference to the given string and assigns it to the Secret field.
func (o *IncomeVerificationDocumentsDownloadRequest) SetSecret(v string) {
	o.Secret = &v
}

// GetIncomeVerificationId returns the IncomeVerificationId field value if set, zero value otherwise (both if not set or set to explicit null).
func (o *IncomeVerificationDocumentsDownloadRequest) GetIncomeVerificationId() string {
	if o == nil || o.IncomeVerificationId.Get() == nil {
		var ret string
		return ret
	}
	return *o.IncomeVerificationId.Get()
}

// GetIncomeVerificationIdOk returns a tuple with the IncomeVerificationId field value if set, nil otherwise
// and a boolean to check if the value has been set.
// NOTE: If the value is an explicit nil, `nil, true` will be returned
func (o *IncomeVerificationDocumentsDownloadRequest) GetIncomeVerificationIdOk() (*string, bool) {
	if o == nil  {
		return nil, false
	}
	return o.IncomeVerificationId.Get(), o.IncomeVerificationId.IsSet()
}

// HasIncomeVerificationId returns a boolean if a field has been set.
func (o *IncomeVerificationDocumentsDownloadRequest) HasIncomeVerificationId() bool {
	if o != nil && o.IncomeVerificationId.IsSet() {
		return true
	}

	return false
}

// SetIncomeVerificationId gets a reference to the given NullableString and assigns it to the IncomeVerificationId field.
func (o *IncomeVerificationDocumentsDownloadRequest) SetIncomeVerificationId(v string) {
	o.IncomeVerificationId.Set(&v)
}
// SetIncomeVerificationIdNil sets the value for IncomeVerificationId to be an explicit nil
func (o *IncomeVerificationDocumentsDownloadRequest) SetIncomeVerificationIdNil() {
	o.IncomeVerificationId.Set(nil)
}

// UnsetIncomeVerificationId ensures that no value is present for IncomeVerificationId, not even an explicit nil
func (o *IncomeVerificationDocumentsDownloadRequest) UnsetIncomeVerificationId() {
	o.IncomeVerificationId.Unset()
}

// GetAccessToken returns the AccessToken field value if set, zero value otherwise (both if not set or set to explicit null).
func (o *IncomeVerificationDocumentsDownloadRequest) GetAccessToken() string {
	if o == nil || o.AccessToken.Get() == nil {
		var ret string
		return ret
	}
	return *o.AccessToken.Get()
}

// GetAccessTokenOk returns a tuple with the AccessToken field value if set, nil otherwise
// and a boolean to check if the value has been set.
// NOTE: If the value is an explicit nil, `nil, true` will be returned
func (o *IncomeVerificationDocumentsDownloadRequest) GetAccessTokenOk() (*string, bool) {
	if o == nil  {
		return nil, false
	}
	return o.AccessToken.Get(), o.AccessToken.IsSet()
}

// HasAccessToken returns a boolean if a field has been set.
func (o *IncomeVerificationDocumentsDownloadRequest) HasAccessToken() bool {
	if o != nil && o.AccessToken.IsSet() {
		return true
	}

	return false
}

// SetAccessToken gets a reference to the given NullableString and assigns it to the AccessToken field.
func (o *IncomeVerificationDocumentsDownloadRequest) SetAccessToken(v string) {
	o.AccessToken.Set(&v)
}
// SetAccessTokenNil sets the value for AccessToken to be an explicit nil
func (o *IncomeVerificationDocumentsDownloadRequest) SetAccessTokenNil() {
	o.AccessToken.Set(nil)
}

// UnsetAccessToken ensures that no value is present for AccessToken, not even an explicit nil
func (o *IncomeVerificationDocumentsDownloadRequest) UnsetAccessToken() {
	o.AccessToken.Unset()
}

// GetDocumentId returns the DocumentId field value if set, zero value otherwise (both if not set or set to explicit null).
func (o *IncomeVerificationDocumentsDownloadRequest) GetDocumentId() string {
	if o == nil || o.DocumentId.Get() == nil {
		var ret string
		return ret
	}
	return *o.DocumentId.Get()
}

// GetDocumentIdOk returns a tuple with the DocumentId field value if set, nil otherwise
// and a boolean to check if the value has been set.
// NOTE: If the value is an explicit nil, `nil, true` will be returned
func (o *IncomeVerificationDocumentsDownloadRequest) GetDocumentIdOk() (*string, bool) {
	if o == nil  {
		return nil, false
	}
	return o.DocumentId.Get(), o.DocumentId.IsSet()
}

// HasDocumentId returns a boolean if a field has been set.
func (o *IncomeVerificationDocumentsDownloadRequest) HasDocumentId() bool {
	if o != nil && o.DocumentId.IsSet() {
		return true
	}

	return false
}

// SetDocumentId gets a reference to the given NullableString and assigns it to the DocumentId field.
func (o *IncomeVerificationDocumentsDownloadRequest) SetDocumentId(v string) {
	o.DocumentId.Set(&v)
}
// SetDocumentIdNil sets the value for DocumentId to be an explicit nil
func (o *IncomeVerificationDocumentsDownloadRequest) SetDocumentIdNil() {
	o.DocumentId.Set(nil)
}

// UnsetDocumentId ensures that no value is present for DocumentId, not even an explicit nil
func (o *IncomeVerificationDocumentsDownloadRequest) UnsetDocumentId() {
	o.DocumentId.Unset()
}

func (o IncomeVerificationDocumentsDownloadRequest) MarshalJSON() ([]byte, error) {
	toSerialize := map[string]interface{}{}
	if o.ClientId != nil {
		toSerialize["client_id"] = o.ClientId
	}
	if o.Secret != nil {
		toSerialize["secret"] = o.Secret
	}
	if o.IncomeVerificationId.IsSet() {
		toSerialize["income_verification_id"] = o.IncomeVerificationId.Get()
	}
	if o.AccessToken.IsSet() {
		toSerialize["access_token"] = o.AccessToken.Get()
	}
	if o.DocumentId.IsSet() {
		toSerialize["document_id"] = o.DocumentId.Get()
	}
	return json.Marshal(toSerialize)
}

type NullableIncomeVerificationDocumentsDownloadRequest struct {
	value *IncomeVerificationDocumentsDownloadRequest
	isSet bool
}

func (v NullableIncomeVerificationDocumentsDownloadRequest) Get() *IncomeVerificationDocumentsDownloadRequest {
	return v.value
}

func (v *NullableIncomeVerificationDocumentsDownloadRequest) Set(val *IncomeVerificationDocumentsDownloadRequest) {
	v.value = val
	v.isSet = true
}

func (v NullableIncomeVerificationDocumentsDownloadRequest) IsSet() bool {
	return v.isSet
}

func (v *NullableIncomeVerificationDocumentsDownloadRequest) Unset() {
	v.value = nil
	v.isSet = false
}

func NewNullableIncomeVerificationDocumentsDownloadRequest(val *IncomeVerificationDocumentsDownloadRequest) *NullableIncomeVerificationDocumentsDownloadRequest {
	return &NullableIncomeVerificationDocumentsDownloadRequest{value: val, isSet: true}
}

func (v NullableIncomeVerificationDocumentsDownloadRequest) MarshalJSON() ([]byte, error) {
	return json.Marshal(v.value)
}

func (v *NullableIncomeVerificationDocumentsDownloadRequest) UnmarshalJSON(src []byte) error {
	v.isSet = true
	return json.Unmarshal(src, &v.value)
}


text:
"""

Shane Bussmann

2014 August 15

Plot total flux of an object vs. the individual component fluxes of that
object.  Like Figure 5 in Hodge+13.

"""

from astropy.table import Table
import matplotlib
import matplotlib.pyplot as plt
from pylab import savefig
import numpy

# set font properties
font = {'family' : 'Arial',
        'weight' : 'normal',
        'size'   : 12}
matplotlib.rc('font', **font)
matplotlib.rcParams['axes.linewidth'] = 1.5

# read in the plotting color scheme
colorschemeloc = 'colorscheme.txt'
colorscheme = Table.read(colorschemeloc, format='ascii')
ecolor = str(colorscheme['ecolor'][0])
color3 = colorscheme['color3'][0]
color4 = colorscheme['color4'][0]
gray = colorscheme['color6'][0]

# *****************
# Hodge+2013 sample
# *****************

# Plotting parameters
hodgecolor = 'LightPink'
hodgems = 3
hodgefmt = 'D'

# Load the data
fluxcomponent_file = '../Data/hodge2013.dat'
fluxcomponent = Table.read(fluxcomponent_file, format='ascii')

nindiv = len(fluxcomponent)

superfluxtotal = []
e_superfluxtotal = []
for icomp in range(nindiv):
    target = fluxcomponent['lessid'][icomp]

    match = fluxcomponent['lessid'] == target
    fluxsources = fluxcomponent['best880'][match]
    e_fluxsources = fluxcomponent['e_best880'][match]
    ftot = fluxsources.sum()
    e_ftot = e_fluxsources.sum()

    superfluxtotal.append(ftot)
    e_superfluxtotal.append(e_ftot)

fluxcomponent_hodge = fluxcomponent['best880']
fluxtotal_hodge = numpy.array(superfluxtotal)

xxxhodge = numpy.array(superfluxtotal)
xxxerrhodge = numpy.array(e_superfluxtotal)
yyyhodge = fluxcomponent['best880']
yyyerrhodge = fluxcomponent['e_best880']

# **********
# Cowley simulations
# **********

# Plotting parameters
bargcolor = 'LightBlue'
bargms = 4
bargfmt = '*'

# Load the data
fluxcomponent_file = '../Data/SPIRE_ALMA_Cat_v2.txt'
fluxcomponent = Table.read(fluxcomponent_file, format='ascii')
hiz = fluxcomponent['z'] > 1.5
fluxcomponent = fluxcomponent[hiz]
hiflux = fluxcomponent['S500'] > 50
fluxcomponent = fluxcomponent[hiflux]

nindiv = len(fluxcomponent)

superfluxtotal = []
e_superfluxtotal = []
for icomp in range(nindiv):
    target = fluxcomponent['TargetID'][icomp]

    match = fluxcomponent['TargetID'] == target
    fluxsources = fluxcomponent['S850'][match]
    e_fluxsources = fluxcomponent['S850'][match] / 10.
    ftot = fluxsources.sum()
    e_ftot = e_fluxsources.sum()

    superfluxtotal.append(ftot)
    e_superfluxtotal.append(e_ftot)

fluxcomponent_barger = fluxcomponent['S850']
fluxtotal_barger = numpy.array(superfluxtotal)

xxxbarger = numpy.array(superfluxtotal)
xxxerrbarger = numpy.array(e_superfluxtotal)
yyybarger = fluxcomponent['S850']
yyyerrbarger = fluxcomponent['S850'] / 10.

# **********
# Barger sample
# **********

# Plotting parameters
bargcolor = 'orange'
bargms = 6
bargfmt = 'x'

# Load the data
#fluxcomponent_file = '../Data/barger2012.dat'
#fluxcomponent = Table.read(fluxcomponent_file, format='ascii')
#
#nindiv = len(fluxcomponent)
#
#superfluxtotal = []
#e_superfluxtotal = []
#for icomp in range(nindiv):
#    target = fluxcomponent['name'][icomp]
#
#    match = fluxcomponent['name'] == target
#    fluxsources = fluxcomponent['f860'][match]
#    e_fluxsources = fluxcomponent['e_f860'][match]
#    ftot = fluxsources.sum()
#    e_ftot = e_fluxsources.sum()
#
#    superfluxtotal.append(ftot)
#    e_superfluxtotal.append(e_ftot)
#
#fluxcomponent_barger = fluxcomponent['f860']
#fluxtotal_barger = numpy.array(superfluxtotal)
#
#xxxbarger = numpy.array(superfluxtotal)
#xxxerrbarger = numpy.array(e_superfluxtotal)
#yyybarger = fluxcomponent['f860']
#yyyerrbarger = fluxcomponent['e_f860']

# ***********
# ALMA sample
# ***********

# plotting parameters
acolor = colorscheme['color5'][0]
ams = 4
afmt = 's'

fluxcomponent_file = '../Data/table_intrinsic.dat'
fluxcomponent = Table.read(fluxcomponent_file, format='ascii')

nindiv = len(fluxcomponent)

superfluxtotal = []
e_superfluxtotal = []
for icomp in range(nindiv):
    target = fluxcomponent['target'][icomp]

    match = fluxcomponent['target'] == target
    fluxsources = fluxcomponent['f870'][match]
    e_fluxsources = fluxcomponent['e_f870'][match]
    ftot = fluxsources.sum()
    e_ftot = e_fluxsources.sum()

    superfluxtotal.append(ftot)
    e_superfluxtotal.append(e_ftot)

fluxcomponent_alma = fluxcomponent['f870']
fluxtotal_alma = numpy.array(superfluxtotal)

xxxalma = superfluxtotal
xxxerralma = e_superfluxtotal
yyyalma = fluxcomponent['f870']
yyyerralma = fluxcomponent['e_f870']

# **********
# SMA sample
# **********

# Plotting parameters
bcolor = colorscheme['color1'][0]
bms = 3
bfmt = 'o'

# Load the data
fluxcomponent_file = '../Data/bussmann2013_muflux.dat'
fluxcomponent = Table.read(fluxcomponent_file, format='ascii')

nindiv = len(fluxcomponent)

superfluxtotal = []
e_superfluxtotal = []
for icomp in range(nindiv):
    target = fluxcomponent['dataname'][icomp]

    match = fluxcomponent['dataname'] == target
    fluxsources = fluxcomponent['fnu'][match] / fluxcomponent['mu'][match]
    e_fluxsources = fluxcomponent['e_fnu'][match] / fluxcomponent['mu'][match]
    ftot = fluxsources.sum()
    e_ftot = e_fluxsources.sum()

    superfluxtotal.append(ftot)
    e_superfluxtotal.append(e_ftot)

goodmodel = fluxcomponent['mu'] > fluxcomponent['e_mu']

fluxcomponent_sma = fluxcomponent['fnu'][goodmodel] / fluxcomponent['mu'][goodmodel]
fluxtotal_sma = numpy.array(superfluxtotal)[goodmodel]

xxxsma = numpy.array(superfluxtotal)[goodmodel]
xxxerrsma = numpy.array(e_superfluxtotal)[goodmodel]
yyysma = fluxcomponent['fnu'][goodmodel] / fluxcomponent['mu'][goodmodel]
yyyerrsma = fluxcomponent['e_fnu'][goodmodel] / fluxcomponent['mu'][goodmodel]

# set the plotting window size
#fig = plt.figure(figsize=(5.0, 4.5))
fig, ax1 = plt.subplots(figsize=(5,4.5))
#ax2 = ax1.twinx()

# plot the average S_component/S_total vs. S_total
#plt.clf()

def gethist(xflux, componentflux, totalflux):
    nflux = xflux.size
    avgratio = numpy.zeros(nflux)
    for i in range(nflux - 1):
        iflux = xflux[i]
        iflux1 = xflux[i + 1]
        match = (totalflux > iflux) & (totalflux < iflux1)
        if totalflux[match].size > 0:
            ratioflux = componentflux[match] / totalflux[match]
            avgratio[i] = numpy.mean(ratioflux)

    return avgratio

maxflux = 45
minflux = 0

xflux = numpy.arange(minflux, maxflux, 1)

ratio_hodge = gethist(xflux, fluxcomponent_hodge, fluxtotal_hodge)
ratio_barger = gethist(xflux, fluxcomponent_barger, fluxtotal_barger)
ratio_sma = gethist(xflux, fluxcomponent_sma, fluxtotal_sma)
ratio_alma = gethist(xflux, fluxcomponent_alma, fluxtotal_alma)
ratio_alma[0] = 0

#ax2.plot(xflux, ratio_hodge, color=hodgecolor, linewidth=2, zorder=-1,
#        drawstyle='steps-post')
#plt.plot(xflux, ratio_barger, ls='steps', color=bargcolor)
#plt.plot(xflux, ratio_sma, ls='steps', color=bcolor)
#ax2.plot(xflux, ratio_alma, color=acolor, linewidth=2, zorder=0,
#        drawstyle='steps-post')

#ax2.fill_between(xflux, ratio_hodge, hatch='//', edgecolor=hodgecolor,
#        facecolor='none', zorder=0)
#ax2.fill_between(xflux, ratio_alma, hatch='\\', edgecolor=acolor,
#        facecolor='none', zorder=1)
#ax2.fill_between(xflux, ratio_barger, facecolor='gray', alpha=0.5)

xmin = 0
xmax = 20
ymin = 0
ymax = 1.1
#ax2.axis([xmin, xmax, ymin, ymax])

# customize plot
#ax2.set_xlabel(r'$S_{\rm total} \,({\rm mJy}$)', fontsize='x-large')
#ax2.set_ylabel(r'$<S_{\rm component}/S_{\rm total}>$', fontsize='large')
#ax2.minorticks_on()
#ax2.tick_params(width=1.5, which='both')
#ax2.tick_params(length=2, which='minor')
#ax2.tick_params(length=4, which='major')
#plt.subplots_adjust(left=0.14, right=0.96, top=0.97, bottom=0.13, wspace=0.39)

#plt.legend(loc='upper right', numpoints=1, handletextpad=0.35, borderpad=0.4,
#        labelspacing=0.18, handlelength=1.0)
#leg = plt.gca().get_legend()
#ltext  = leg.get_texts()
#plt.setp(ltext, fontsize='small')

# plot the 1:1 line
ax1.plot([0,45], [0,45], color='gray')
#plt.errorbar(xxx, yyy, yerr=yyyerr, xerr=xxxerr, fmt=',', ecolor='gray',
#        capsize=0)
ax1.plot(xxxhodge, yyyhodge, hodgefmt, ms=hodgems, mfc=hodgecolor,
        mec='black', label='ALESS')
#ax1.plot(xxxhodge, yyyhodge, hodgefmt, ms=hodgems, mfc=hodgecolor,
#        mec='none', label='Hodge+13')

#ax1.errorbar(xxx, yyy, yerr=yyyerr, xerr=xxxerr, fmt=',', ecolor='gray',
#        capsize=0)
#ax1.plot(xxxbarger, yyybarger, bargfmt, ms=bargms, color=bargcolor, label='Barger+12')

#ax1.plot(xxxbarger, yyybarger, bargfmt, ms=bargms, color=bargcolor,
#        label='C15 Simulation', mew=1.2)
#ax1.plot(xxxbarger, yyybarger, bargfmt, ms=bargms, color=bargcolor,
#        mec='none', label='Cowley+2014')

#ax1.errorbar(xxx, yyy, yerr=yyyerr, xerr=xxxerr, fmt=',', ecolor='gray',
#        capsize=0)
#ax1.plot(xxxsma, yyysma, bfmt, ms=bms, color=bcolor, label='SMA Sample')

ax1.errorbar(xxxalma, yyyalma, yerr=yyyerralma, xerr=xxxerralma, fmt=',',
        ecolor='gray', capsize=0)
ax1.plot(xxxalma, yyyalma, afmt, ms=ams, color=acolor, label='Herschel-ALMA')

# customize plot
#plt.xlabel(r'${\rm Total} \, S_{870}\,({\rm mJy}$)', fontsize='x-large')
#plt.ylabel(r'${\rm Component} \, S_{870}\,({\rm mJy}$)', fontsize='x-large')
ax1.set_xlabel(r'$S_{\rm total}\,({\rm mJy}$)', fontsize='large')
ax1.set_ylabel(r'$S_{\rm component}\,({\rm mJy}$)', fontsize='large')
ax1.minorticks_on()
ax1.tick_params(width=1.5, which='both')
ax1.tick_params(length=2, which='minor')
ax1.tick_params(length=4, which='major')
plt.subplots_adjust(left=0.12, right=0.87, top=0.97, bottom=0.12, wspace=0.39)

xmin = 0
xmax = 25
ymin = 0
ymax = 25
ax1.axis([xmin, xmax, ymin, ymax])

ax1.legend(loc='upper left', numpoints=1, handletextpad=0.00, borderpad=0.4,
        labelspacing=0.35, handlelength=1.0)
#leg = ax1.gca().get_legend()
#ltext  = leg.get_texts()
#plt.setp(ltext, fontsize='small')
#ax2.set_zorder(0)
ax1.set_zorder(1)
#ax1.set_frame_on(False)
#ax2.set_frame_on(True)

outfile = '../Figures/fluxtotalcomponent.pdf'
savefig(outfile)

#savefig('avgratio')
import pdb; pdb.set_trace()


text:
require_relative './base_service'

# encoding: utf-8
#
# This client is automatically generated from a template and JSON schema definition.
# See https://github.com/gocardless/gocardless-pro-ruby#contributing before editing.
#

module GoCardlessPro
  module Services
    # Service for making requests to the Creditor endpoints
    class CreditorsService < BaseService
      # Creates a new creditor.
      # Example URL: /creditors
      # @param options [Hash] parameters as a hash, under a params key.
      def create(options = {})
        path = '/creditors'

        params = options.delete(:params) || {}
        options[:params] = {}
        options[:params][envelope_key] = params

        options[:retry_failures] = true

        begin
          response = make_request(:post, path, options)

          # Response doesn't raise any errors until #body is called
          response.tap(&:body)
        rescue InvalidStateError => e
          if e.idempotent_creation_conflict?
            case @api_service.on_idempotency_conflict
            when :raise
              raise IdempotencyConflict, e.error
            when :fetch
              return get(e.conflicting_resource_id)
            end
          end

          raise e
        end

        return if response.body.nil?

        Resources::Creditor.new(unenvelope_body(response.body), response)
      end

      # Returns a [cursor-paginated](#api-usage-cursor-pagination) list of your
      # creditors.
      # Example URL: /creditors
      # @param options [Hash] parameters as a hash, under a params key.
      def list(options = {})
        path = '/creditors'

        options[:retry_failures] = true

        response = make_request(:get, path, options)

        ListResponse.new(
          response: response,
          unenveloped_body: unenvelope_body(response.body),
          resource_class: Resources::Creditor
        )
      end

      # Get a lazily enumerated list of all the items returned. This is similar to the `list` method but will paginate for you automatically.
      #
      # @param options [Hash] parameters as a hash. If the request is a GET, these will be converted to query parameters.
      # Otherwise they will be the body of the request.
      def all(options = {})
        Paginator.new(
          service: self,
          options: options
        ).enumerator
      end

      # Retrieves the details of an existing creditor.
      # Example URL: /creditors/:identity
      #
      # @param identity       # Unique identifier, beginning with "CR".
      # @param options [Hash] parameters as a hash, under a params key.
      def get(identity, options = {})
        path = sub_url('/creditors/:identity', 'identity' => identity)

        options[:retry_failures] = true

        response = make_request(:get, path, options)

        return if response.body.nil?

        Resources::Creditor.new(unenvelope_body(response.body), response)
      end

      # Updates a creditor object. Supports all of the fields supported when creating
      # a creditor.
      # Example URL: /creditors/:identity
      #
      # @param identity       # Unique identifier, beginning with "CR".
      # @param options [Hash] parameters as a hash, under a params key.
      def update(identity, options = {})
        path = sub_url('/creditors/:identity', 'identity' => identity)

        params = options.delete(:params) || {}
        options[:params] = {}
        options[:params][envelope_key] = params

        options[:retry_failures] = true

        response = make_request(:put, path, options)

        return if response.body.nil?

        Resources::Creditor.new(unenvelope_body(response.body), response)
      end

      private

      # Unenvelope the response of the body using the service's `envelope_key`
      #
      # @param body [Hash]
      def unenvelope_body(body)
        body[envelope_key] || body['data']
      end

      # return the key which API responses will envelope data under
      def envelope_key
        'creditors'
      end
    end
  end
end


text:
Quick Donate

		  	{% include 'campaign/components/payfast_button.html' with campaign=campaign amount='100' editable=False btn_classes='btn btn-block' btn_text='R100' %}
		    {% include 'campaign/components/payfast_button.html' with campaign=campaign amount='500' editable=False btn_classes='btn btn-block' btn_text='R500' %}
		    {% include 'campaign/components/payfast_button.html' with campaign=campaign amount='1000' editable=False btn_classes='btn-success btn-block' btn_text='R1000' %}

Custom Donate

		      {% include 'campaign/components/payfast_button.html' with campaign=campaign amount='1000' editable=True btn_classes='btn-success btn-block' btn_text='Donate' %}

Buy 67 minutes

		    <form
		    	id="salaryDonation"
				data-campaign="{{campaign.pk}}" class="donate-form"
				action="{{PAYFAST_URL}}" method="post" name="salaryDonation" >
			{% csrf_token %}
			<input type="hidden" name="cmd" value="_paynow">
			<input type="hidden" name="receiver" value="{{campaign.charity.gateway_properties.recipient_email}}">
			<input type="hidden" name="item_name" value="FlashGive: {{campaign.name}}">
			<input type="hidden" name="amount" value="{{amount}}">
			<input type="hidden" name="return_url" value="http://{{SITE.domain}}{% url 'campaign_detail' campaign.slug %}?tid=">
			<input type="hidden" name="cancel_url" value="http://{{SITE.domain}}{% url 'campaign_detail' campaign.slug %}">

			    Enter your monthly salary
			    <input type="number" class="form-control" id="donation-amount"
			    min="1000"
			    value="20000" name="monthlySalary" placeholder="Your monthly salary" onkeyup="calculate67MinSalary(this.form, '{{campaign.id}}')">

			    	You earn R in 67 mins

			  <button class="btn btn-block btn-success" data-loading-text='loading..' >Donate R</button>
			</form>


text:
package org.apache.lens.cube.parse;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.parse.SemanticException;
import org.apache.lens.cube.metadata.Dimension;

import static org.apache.lens.cube.parse.StorageUtil.joinWithAnd;

/**
 * Dimension HQLContext.
 *
 * Contains all the dimensions queried and their candidate dim tables Update
 * where string with storage filters added dimensions queried.
 */
abstract class DimHQLContext extends SimpleHQLContext {

  public static Log LOG = LogFactory.getLog(DimHQLContext.class.getName());

  private final Map<Dimension, CandidateDim> dimsToQuery;
  private final Set<Dimension> queriedDims;
  private String where;
  protected final CubeQueryContext query;

  public CubeQueryContext getQuery() {
    return query;
  }

  DimHQLContext(CubeQueryContext query, Map<Dimension, CandidateDim> dimsToQuery, Set<Dimension> queriedDims, String select, String where,
      String groupby, String orderby, String having, Integer limit) throws SemanticException {
    super(select, groupby, orderby, having, limit);
    this.query = query;
    this.dimsToQuery = dimsToQuery;
    this.where = where;
    this.queriedDims = queriedDims;
  }

  protected void setMissingExpressions() throws SemanticException {
    setFrom(getFromString());
    setWhere(joinWithAnd(
      getQuery().getHiveConf().getBoolean
        (CubeQueryConfUtil.REPLACE_TIMEDIM_WITH_PART_COL, CubeQueryConfUtil.DEFAULT_REPLACE_TIMEDIM_WITH_PART_COL)
         ? getPostSelectionWhereClause() : null,
      genWhereClauseWithDimPartitions(where)
    ));
  }

  protected abstract String getPostSelectionWhereClause() throws SemanticException;

  protected String getFromString() throws SemanticException {
    String fromString = getFromTable();
    if (query.isAutoJoinResolved()) {
      fromString =
        query.getAutoJoinCtx().getFromString(fromString, getQueriedFact(), getQueriedDimSet(), getDimsToQuery(), query);
    }
    return fromString;
  }

  protected abstract Set<Dimension> getQueriedDimSet();

  protected abstract CandidateFact getQueriedFact();

  protected abstract String getFromTable() throws SemanticException;

  public Map<Dimension, CandidateDim> getDimsToQuery() {
    return dimsToQuery;
  }

  private String genWhereClauseWithDimPartitions(String originalWhere) {
    StringBuilder whereBuf;
    if (originalWhere != null) {
      whereBuf = new StringBuilder(originalWhere);
    } else {
      whereBuf = new StringBuilder();
    }

    // add where clause for all dimensions
    if (queriedDims != null) {
      boolean added = (originalWhere != null);
      for (Dimension dim : queriedDims) {
        CandidateDim cdim = dimsToQuery.get(dim);
        if (!cdim.isWhereClauseAdded()) {
          appendWhereClause(whereBuf, cdim.whereClause, added);
          added = true;
        }
      }
    }
    if (whereBuf.length() == 0) {
      return null;
    }
    return whereBuf.toString();
  }

  static void appendWhereClause(StringBuilder filterCondition, String whereClause, boolean hasMore) {
    // Make sure we add AND only when there are already some conditions in where
    // clause
    if (hasMore && !filterCondition.toString().isEmpty() && !StringUtils.isBlank(whereClause)) {
      filterCondition.append(" AND ");
    }

    if (!StringUtils.isBlank(whereClause)) {
      filterCondition.append("(");
      filterCondition.append(whereClause);
      filterCondition.append(")");
    }
  }
}


text:
<?php

class Google_Service_ToolResults_ShardSummary extends Google_Collection
{
  protected $collection_key = 'runs';
  protected $runsType = 'Google_Service_ToolResults_StepSummary';
  protected $runsDataType = 'array';
  protected $shardResultType = 'Google_Service_ToolResults_MergedResult';
  protected $shardResultDataType = '';

  /**
   * @param Google_Service_ToolResults_StepSummary
   */
  public function setRuns($runs)
  {
    $this->runs = $runs;
  }
  /**
   * @return Google_Service_ToolResults_StepSummary
   */
  public function getRuns()
  {
    return $this->runs;
  }
  /**
   * @param Google_Service_ToolResults_MergedResult
   */
  public function setShardResult(Google_Service_ToolResults_MergedResult $shardResult)
  {
    $this->shardResult = $shardResult;
  }
  /**
   * @return Google_Service_ToolResults_MergedResult
   */
  public function getShardResult()
  {
    return $this->shardResult;
  }
}