Esempio n. 1
0
// Context returns the translation context for the String, which
// might be empty.
func (s String) Context() string {
	fields, _ := stringutil.SplitFields(string(s), "|")
	if len(fields) > 1 {
		return fields[0]
	}
	return ""
}
Esempio n. 2
0
func (s *String) fields() []string {
	fields, err := stringutil.SplitFields(s.Value, "|")
	if err != nil {
		// TODO: Do something better here
		panic(err)
	}
	return fields
}
Esempio n. 3
0
// TranslatedString returns the string translated into
// the language returned by lang.
func (s String) TranslatedString(lang Languager) string {
	if string(s) == "" {
		return ""
	}
	fields, _ := stringutil.SplitFields(string(s), "|")
	if len(fields) > 1 {
		return Tc(lang, fields[0], fields[1])
	}
	if len(fields) > 0 {
		return T(lang, fields[0])
	}
	return T(lang, string(s))
}
Esempio n. 4
0
func (b *Builder) compilerCmd() (*exec.Cmd, error) {
	// Download deps, if any
	b.downloadDeps()
	// -e reports all the errors
	gcflags := []string{"-e"}
	args := []string{"build"}
	if b.GoFlags != "" {
		fields, err := stringutil.SplitFields(b.GoFlags, " ")
		if err != nil {
			return nil, fmt.Errorf("error parsing -goflags: %v", err)
		}
		for ii := 0; ii < len(fields); ii++ {
			field := fields[ii]
			if field == "-gcflags" {
				subFields, err := stringutil.SplitFields(fields[ii+1], " ")
				if err != nil {
					return nil, fmt.Errorf("error parsing -gcflags: %v", err)
				}
				gcflags = append(gcflags, subFields...)
				ii++
				continue
			}
			args = append(args, field)
		}
	}
	args = append(args, "-gcflags")
	args = append(args, strings.Join(gcflags, " "))
	args = append(args, b.buildTags()...)
	lib := filepath.Join(b.Dir, "lib")
	if st, err := os.Stat(lib); err == nil && st.IsDir() {
		// If there's a lib directory, add it to rpath
		args = append(args, []string{"-ldflags", "-r lib"}...)
	}
	cmd := b.goCmd(args...)
	return cmd, nil
}
Esempio n. 5
0
func (c *Client) Parse(s string) error {
	fields, err := stringutil.SplitFields(s, ":")
	if err != nil {
		return err
	}
	switch len(fields) {
	case 1:
		c.Id = fields[0]
	case 2:
		c.Id = fields[0]
		c.Secret = fields[1]
	default:
		return fmt.Errorf("invalid number of fields: %d", len(fields))
	}
	return nil
}
Esempio n. 6
0
func ParseOptions(options string) (Options, error) {
	values, err := stringutil.SplitFields(options, ",")
	if err != nil {
		return nil, fmt.Errorf("error parsing asset options: %s", err)
	}
	opts := make(Options)
	for _, v := range values {
		eq := strings.IndexByte(v, '=')
		if eq < 0 {
			opts[v] = ""
		} else {
			opts[v[:eq]] = v[eq+1:]
		}
	}
	return opts, nil
}
Esempio n. 7
0
func openVFS(appName string, abspath string, baked string) (vfsutil.VFS, error) {
	if appName == "" {
		return nil, errors.New("empty app name")
	}
	if len(baked) > 0 {
		noVFS := false
		fields, _ := stringutil.SplitFields(os.Getenv("GONDOLA_APP_NO_BAKED_DATA"), " ")
		lowerAppName := strings.ToLower(appName)
		for _, field := range fields {
			if strings.ToLower(field) == lowerAppName {
				noVFS = true
			}
		}
		if !noVFS {
			return vfsutil.OpenBaked(baked)
		}
	}
	return vfs.FS(abspath)
}
Esempio n. 8
0
func (t *Template) parseComment(name string, comment string, file string, included bool) error {
	lines := stringutil.SplitLines(comment)
	extended := false
	for _, v := range lines {
		m := keyRe.FindStringSubmatchIndex(v)
		if m != nil && m[0] == 0 && len(m) > 3 {
			start := m[1] - m[3]
			end := start + m[2]
			key := strings.ToLower(strings.TrimSpace(v[start:end]))
			var options assets.Options
			var value string
			if len(v) > end {
				rem := v[end+1:]
				if v[end] == '|' {
					// Has options
					colon := strings.IndexByte(rem, ':')
					opts := rem[:colon]
					var err error
					options, err = assets.ParseOptions(opts)
					if err != nil {
						return fmt.Errorf("error parsing options for asset key %q: %s", key, err)
					}
					value = rem[colon+1:]
				} else {
					// No options
					value = rem
				}
			}
			splitted, err := stringutil.SplitFields(value, ",")
			if err != nil {
				return fmt.Errorf("error parsing value for asset key %q: %s", key, err)
			}
			values, err := t.parseCommentVariables(splitted)
			if err != nil {
				return fmt.Errorf("error parsing values for asset key %q: %s", key, err)
			}

			inc := true
			switch key {
			case "extend", "extends":
				if extended || len(values) > 1 {
					return fmt.Errorf("templates can only extend one template")
				}
				if t.final {
					return fmt.Errorf("template has been declared as final")
				}
				if strings.ToLower(values[0]) == "none" {
					t.final = true
					break
				}
				extended = true
				inc = false
				fallthrough
			case "include", "includes":
				for _, n := range values {
					err := t.load(n, inc, name)
					if err != nil {
						return err
					}
				}
			case "content-type", "mime-type":
				switch len(values) {
				case 1:
					t.contentType = values[0]
				case 2:
					t.contentType = fmt.Sprintf("%s; charset=%s", values[0], values[1])
				default:
					return fmt.Errorf("%q must have one or two values (e.g. \"text/html\" or \"text/html, UTF-8\" - without quotes", key)
				}
			default:
				if t.AssetsManager == nil {
					return ErrNoAssetsManager
				}
				group, err := assets.Parse(t.AssetsManager, key, values, options)
				if err != nil {
					return err
				}
				t.assetGroups = append(t.assetGroups, group)
			}
		}
	}
	if !extended && !included {
		t.root = file
	}
	return nil
}
Esempio n. 9
0
func parseValue(v reflect.Value, raw string) error {
	switch v.Type().Kind() {
	case reflect.Bool:
		value, err := strconv.ParseBool(raw)
		if err != nil {
			return err
		}
		v.SetBool(value)
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		value, err := strconv.ParseInt(raw, 10, 64)
		if err != nil {
			return err
		}
		v.SetInt(int64(value))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		value, err := strconv.ParseUint(raw, 10, 64)
		if err != nil {
			return err
		}
		v.SetUint(uint64(value))
	case reflect.Float32, reflect.Float64:
		value, err := strconv.ParseFloat(raw, 64)
		if err != nil {
			return err
		}
		v.SetFloat(value)
	case reflect.String:
		v.SetString(raw)
	case reflect.Slice:
		fields, err := stringutil.SplitFields(raw, ",")
		if err != nil {
			return fmt.Errorf("error splitting values: %s", err)
		}
		count := len(fields)
		v.Set(reflect.MakeSlice(v.Type(), count, count))
		for ii, f := range fields {
			if err := input.Parse(f, v.Index(ii).Addr().Interface()); err != nil {
				return fmt.Errorf("error parsing field at index %d: %s", ii, err)
			}
		}
	case reflect.Map:
		fields, err := stringutil.SplitFields(raw, ",")
		if err != nil {
			return fmt.Errorf("error splitting values: %s", err)
		}
		v.Set(reflect.MakeMap(v.Type()))
		ktyp := v.Type().Key()
		etyp := v.Type().Elem()
		for ii, field := range fields {
			subFields, err := stringutil.SplitFields(field, "=")
			if err != nil {
				return fmt.Errorf("error splitting key-value %q: %s", field, err)
			}
			k := reflect.New(ktyp)
			if err := input.Parse(subFields[0], k.Interface()); err != nil {
				return fmt.Errorf("error parsing key at index %d: %s", ii, err)
			}
			elem := reflect.New(etyp)
			if len(subFields) < 2 {
				return fmt.Errorf("invalid map field %q", field)
			}
			if err := input.Parse(subFields[1], elem.Interface()); err != nil {
				return fmt.Errorf("error parsing value at index %d: %s", ii, err)
			}
			v.SetMapIndex(k.Elem(), elem.Elem())
		}
	default:
		parser, ok := v.Interface().(input.Parser)
		if !ok {
			return fmt.Errorf("can't parse value of type %s", v.Type())
		}
		if v.Kind() == reflect.Ptr && !v.Elem().IsValid() {
			v.Set(reflect.New(v.Type().Elem()))
			parser = v.Interface().(input.Parser)
		}
		return parser.Parse(raw)
	}
	return nil
}