Example #1
0
func writeRowsFunc(w io.Writer, tree *parse.Node) {
	var buf1, buf2, buf3 bytes.Buffer

	var i int
	var parent = tree
	for _, node := range tree.Edges() {
		if node.Tags.Skip {
			continue
		}

		// temporary variable declaration
		switch node.Kind {
		case parse.Map, parse.Slice:
			fmt.Fprintf(&buf1, "var v%d %s\n", i, "[]byte")
		default:
			fmt.Fprintf(&buf1, "var v%d %s\n", i, node.Type)
		}

		// variable scanning
		fmt.Fprintf(&buf2, "&v%d,\n", i)

		// variable setting
		path := node.Path()[2:]

		// if the parent is a ptr struct we
		// need to create a new
		if parent != node.Parent && node.Parent.Kind == parse.Ptr {
			fmt.Fprintf(&buf3, "v.%s=&%s{}\n", join(path[:len(path)-1], "."), node.Parent.Type)
		}

		switch node.Kind {
		case parse.Map, parse.Slice, parse.Struct, parse.Ptr:
			fmt.Fprintf(&buf3, "json.Unmarshal(v%d, &v.%s)\n", i, join(path, "."))
		default:
			fmt.Fprintf(&buf3, "v.%s=v%d\n", join(path, "."), i)
		}

		parent = node.Parent
		i++
	}

	fmt.Fprintf(w,
		sScanRows,
		inflections.Pluralize(tree.Type),
		tree.Type,
		tree.Type,
		buf1.String(),
		buf2.String(),
		tree.Type,
		buf3.String(),
	)
}
Example #2
0
func InvText(i item.Itemer) string {
	invStr := ""
	if item.IsStackable(i) {
		name := i.Name()
		if i.Count() > 1 {
			name = inflections.Pluralize(name)
		}
		invStr = fmt.Sprintf("%c - %d %s", i.Hotkey(), i.Count(), name)
	} else {
		invStr = fmt.Sprintf("%c - %s", i.Hotkey(), i.Name())
	}

	if item.IsEquipable(i) {
		if creature.Hero.IsEquipped(i) {
			invStr += " (wielding)"
		}
	}
	return invStr
}
Example #3
0
func NarrativeEquip(pos rune) {
	i := creature.Hero.Equip(pos)
	if i == nil {
		status.Println(unableToEquip, termbox.ColorRed+termbox.AttrBold)
		return
	}

	var equipStr string
	if item.IsStackable(i) {
		equipStr += fmt.Sprintf("%d ", i.Count())
		if i.Count() > 1 {
			inflections.Pluralize(i.Name())
		} else {
			equipStr += i.Name()
		}
	} else {
		equipStr += i.Name()
	}
	status.Println(fmt.Sprintf("You equipped %s.", equipStr), termbox.ColorWhite)
}
Example #4
0
func (c *Creature) DropItem(pos rune, a *area.Area) {
	i := c.Inventory[pos]
	c.UnEquip(i)
	delete(c.Inventory, pos)

	cor := c.Coord()
	if a.Items[cor] == nil {
		a.Items[cor] = new(area.Stack)
	}
	a.Items[cor].Push(i)

	// If the item couldn't be dropped (cursed for example), print unable to
	// drop message.
	if i == nil {
		status.Println(UnableToDrop, termbox.ColorRed+termbox.AttrBold)
		return
	}

	fmtStr := "%s dropped %s."
	cName := strings.Title(c.Name())
	if c.IsHero() {
		cName = "You"
	}
	iName := i.Name()
	if item.IsStackable(i) {
		name := i.Name()
		if i.Count() > 1 {
			name = inflections.Pluralize(name)
		}
		iName = strconv.Itoa(i.Count()) + " " + name
	}

	if c.dist() <= Hero.Sight {
		status.Println(fmt.Sprintf(fmtStr, cName, iName), termbox.ColorWhite)
	}
}
Example #5
0
func Load(tree *parse.Node) *Table {
	table := new(Table)

	// local map of indexes, used for quick
	// lookups and de-duping.
	indexs := map[string]*Index{}

	// pluralizes the table name and then
	// formats in snake case.
	table.Name = inflections.Underscore(tree.Type)
	table.Name = inflections.Pluralize(table.Name)

	// each edge node in the tree is a column
	// in the table. Convert each edge node to
	// a Field structure.
	for _, node := range tree.Edges() {

		field := new(Field)

		// Lookup the SQL column type
		// TODO: move this to a function
		t, ok := parse.Types[node.Type]
		if ok {
			tt, ok := types[t]
			if !ok {
				tt = BLOB
			}
			field.Type = tt
		} else {
			field.Type = BLOB
		}

		// substitute tag variables
		if node.Tags != nil {

			if node.Tags.Skip {
				continue
			}

			// default ID and int64 to primary key
			// with auto-increment
			if node.Name == "ID" && node.Kind == parse.Int64 {
				node.Tags.Primary = true
				node.Tags.Auto = true
			}

			field.Auto = node.Tags.Auto
			field.Primary = node.Tags.Primary
			field.Size = node.Tags.Size

			if node.Tags.Primary {
				table.Primary = append(table.Primary, field)
			}

			if node.Tags.Index != "" {
				index, ok := indexs[node.Tags.Index]
				if !ok {
					index = new(Index)
					index.Name = node.Tags.Index
					indexs[index.Name] = index
					table.Index = append(table.Index, index)
				}
				index.Fields = append(index.Fields, field)
			}

			if node.Tags.Unique != "" {
				index, ok := indexs[node.Tags.Index]
				if !ok {
					index = new(Index)
					index.Name = node.Tags.Unique
					index.Unique = true
					indexs[index.Name] = index
					table.Index = append(table.Index, index)
				}
				index.Fields = append(index.Fields, field)
			}

			if node.Tags.Type != "" {
				t, ok := sqlTypes[node.Tags.Type]
				if ok {
					field.Type = t
				}
			}
		}

		// get the full path name
		path := node.Path()
		var parts []string
		for _, part := range path {
			if part.Tags != nil && part.Tags.Name != "" {
				parts = append(parts, part.Tags.Name)
				continue
			}

			parts = append(parts, part.Name)
		}
		field.Name = strings.Join(parts, "_")
		field.Name = inflections.Underscore(field.Name)

		table.Fields = append(table.Fields, field)
	}

	return table
}
Example #6
0
func writeSelectRows(w io.Writer, tree *parse.Node) {
	plural := inflections.Pluralize(tree.Type)
	fmt.Fprintf(w, sSelectRows, plural, tree.Type, plural)
}
Example #7
0
func pluralizeStruct(s string) string {
	return inflections.Pluralize(strings.ToLower(s))
}