func TestExtendNil(t *testing.T) { b := builder.Extend(FooBuilder, "Add", nil) _, ok := builder.Get(b, "X") if ok { t.Fatalf("key %v set unexpectedly", "Add") } }
func TestDelete(t *testing.T) { b := builder.Delete(FooBuilder.X(1), "X") _, ok := builder.Get(b, "X") if ok { t.Fatalf("key %v not deleted", "X") } }
func (b jsonBuilder) AddHrefLink(class string, href string) jsonBuilder { val, _ := builder.Get(b, "Hal") if val == nil { val = Hal{} } hal, _ := val.(Hal) hal.AddHrefLink(class, href) return builder.Set(b, "Hal", hal).(jsonBuilder) }
func TestBuilder(t *testing.T) { b := FooBuilder.X(1).Y(2) assertInt(t, b, "X", 1) assertInt(t, b, "Y", 2) v, _ := builder.Get(b, "Z") if v != nil { t.Errorf("expected nil, got %v", v) } }
func (b jsonBuilder) AddCustomField(name string, value interface{}) jsonBuilder { val, _ := builder.Get(b, "CustomFields") if val == nil { val = make(map[string]interface{}) } fields, _ := val.(map[string]interface{}) fields[name] = value return builder.Set(b, "CustomFields", fields).(jsonBuilder) }
func (b FunctionBuilder) Params(key string, value interface{}) FunctionBuilder { var hash map[string]interface{} params, ok := builder.Get(b, "Params") if ok { hash = params.(map[string]interface{}) } else { hash = make(map[string]interface{}) } hash[key] = value return builder.Set(b, "Params", hash).(FunctionBuilder) }
func assertInt(t *testing.T, b fooBuilder, key string, val int) { v, ok := builder.Get(b, key) if !ok { t.Errorf("key %v not set", key) return } i := v.(int) if i != val { t.Errorf("expected %d, got %d", val, i) } return }
//############################################################################ func (b dirBuilder) Assemble(ctx common.BeamerCtx) (pipe.Pipe, error) { d, ok := builder.Get(b, "Directory") if !ok { return nil, errors.New("mkdir: directory is undefined") } directory := d.(string) if ok, _ := regexp.MatchString(REGEX_VAR_EXPAND, directory); ok { d, err := ctx.Expand(directory) if err != nil { return nil, errors.Errorf("mkdir: expand error: %s", err) } directory = d.(string) } fm, ok := builder.Get(b, "FileMode") if !ok { return nil, errors.New("mkdir: filemode is undefined") } descr, ok := builder.Get(b, "Description") if !ok { descr = fmt.Sprintf("mkdir:: %q", directory) } strict := false if st, ok := builder.Get(b, "Strict"); ok { strict = st.(bool) } return func(s *pipe.State) error { applog.Infof("execute:: %s", descr) err := os.Mkdir(directory, fm.(os.FileMode)) if !strict && strings.Contains(err.Error(), "file exists") { return nil } return err }, nil }
//############################################################################ func (p beamerBuilder) Set(key string, value interface{}) beamerBuilder { m, ok := builder.Get(p, "KVStore") if ok { store := m.(map[string]interface{}) store[key] = value return builder.Set(p, "KVStore", store).(beamerBuilder) } store := make(map[string]interface{}) store[key] = value return builder.Set(p, "KVStore", store).(beamerBuilder) }
func TestAppend(t *testing.T) { b := FooBuilder.Add(1).Add(2) v, ok := builder.Get(b, "Add") if !ok { t.Fatalf("key %v not set", "Add") } a := v.([]int) if len(a) != 2 { t.Fatalf("wrong len %d", len(a)) } i := a[0] j := a[1] if i != 1 || j != 2 { t.Errorf("expected [1, 2], got [%d, %d]", i, j) } }
func TestUnregisteredBuilder(t *testing.T) { b := unregBuilder{}.Add(1) x, _ := builder.Get(b, "X") expected := []interface{}{1} if !reflect.DeepEqual(x.([]interface{}), expected) { t.Errorf("expected %v, got %v", expected, x) } x = builder.GetMap(b)["X"] expected = []interface{}{1} if !reflect.DeepEqual(x.([]interface{}), expected) { t.Errorf("expected %v, got %v", expected, x) } s := builder.GetStruct(b) if s != nil { t.Errorf("expected nil, got %v", s) } }