Example #1
0
func jsonPrettyFilter(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, e *pongo2.Error) {
	body, err := json.MarshalIndent(in.Interface(), "", "  ")
	if err != nil {
		return nil, &pongo2.Error{ErrorMsg: err.Error()}
	}
	return pongo2.AsValue(string(body)), nil
}
Example #2
0
func toSwagger(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	i := in.Interface()
	m := i.(map[string]interface{})

	fixPropertyTree(m)

	data, _ := json.MarshalIndent(i, param.String(), "    ")
	return pongo2.AsValue(string(data)), nil
}
Example #3
0
func filterHumanizeTime(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	t, isTime := in.Interface().(time.Time)
	if !isTime {
		return nil, &pongo2.Error{
			Sender:   "filter:humanize_time",
			ErrorMsg: "Filter input argument must be of type: 'time.Time'.",
		}
	}
	return pongo2.AsValue(humanize.Time(t)), nil
}
Example #4
0
func filterAfterNow(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	t, isTime := in.Interface().(time.Time)
	if !isTime {
		return nil, &pongo2.Error{
			Sender:   "filter:past_now",
			ErrorMsg: "Filter input argument must be of type 'time.Time'.",
		}
	}
	return pongo2.AsValue(time.Now().Before(t)), nil
}
Example #5
0
func filterBool(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	if in.IsNil() {
		return pongo2.AsValue(false), nil
	}
	switch t := in.Interface().(type) {
	case string:
		if t == "" {
			return pongo2.AsValue(false), nil
		}
		v, err := strconv.ParseBool(t)
		if err != nil {
			return nil, &pongo2.Error{
				Sender:   "filter:bool",
				ErrorMsg: "Filter input value invalid.",
			}
		}
		return pongo2.AsValue(v), nil
	case bool:
		return pongo2.AsValue(t), nil
	}
	return nil, &pongo2.Error{
		Sender:   "filter:bool",
		ErrorMsg: "Filter input value must be of type 'bool' or 'string'.",
	}
}
Example #6
0
func timeSince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	errMsg := &pongo2.Error{
		Sender:   "filter:timeuntil/timesince",
		ErrorMsg: "time-value is not a time.Time string.",
	}

	dateStr, ok := in.Interface().(string)
	if !ok {
		return nil, errMsg
	}

	basetime, err := time.Parse(time.RFC3339, dateStr)
	if err != nil {
		return nil, errMsg
	}

	return pongo2.AsValue(util.FormatTime(basetime)), nil
}
Example #7
0
func filterJson(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) {
	var value []byte

	mapValue, ok := in.Interface().(map[string]interface{})
	if ok {
		value, _ = json.Marshal(mapValue)
	} else {
		var arrMapValue []map[string]interface{}
		in.Iterate(func(idx int, count int, key *pongo2.Value, value *pongo2.Value) bool {
			mapItem, ok := key.Interface().(map[string]interface{})
			if ok {
				arrMapValue = append(arrMapValue, mapItem)
			} else {
				panic("[gin_pongo2.filterJson] Unknow format #1")
			}
			return true
		}, func() {})
		value, _ = json.Marshal(arrMapValue)
	}
	return pongo2.AsValue(string(value)), nil
}
Example #8
0
func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	basetime, is_time := in.Interface().(time.Time)
	if !is_time {
		return nil, &pongo2.Error{
			Sender:   "filter:timeuntil/timesince",
			ErrorMsg: "time-value is not a time.Time-instance.",
		}
	}
	var paramtime time.Time
	if !param.IsNil() {
		paramtime, is_time = param.Interface().(time.Time)
		if !is_time {
			return nil, &pongo2.Error{
				Sender:   "filter:timeuntil/timesince",
				ErrorMsg: "time-parameter is not a time.Time-instance.",
			}
		}
	} else {
		paramtime = time.Now()
	}

	return pongo2.AsValue(humanize.TimeDuration(basetime.Sub(paramtime))), nil
}
Example #9
0
func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	basetime, is_time := in.Interface().(time.Time)
	if !is_time {
		return nil, &pongo2.Error{
			Sender:   "filter:naturalday",
			ErrorMsg: "naturalday-value is not a time.Time-instance.",
		}
	}

	var reference_time time.Time
	if !param.IsNil() {
		reference_time, is_time = param.Interface().(time.Time)
		if !is_time {
			return nil, &pongo2.Error{
				Sender:   "filter:naturalday",
				ErrorMsg: "naturalday-parameter is not a time.Time-instance.",
			}
		}
	} else {
		reference_time = time.Now()
	}

	d := reference_time.Sub(basetime) / time.Hour

	switch {
	case d >= 0 && d < 24:
		// Today
		return pongo2.AsValue("today"), nil
	case d >= 24:
		return pongo2.AsValue("yesterday"), nil
	case d < 0 && d >= -24:
		return pongo2.AsValue("tomorrow"), nil
	}

	// Default behaviour
	return pongo2.ApplyFilter("naturaltime", in, param)
}
Example #10
0
func toSwagger(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	i := in.Interface()
	m := i.(map[string]interface{})
	switch properties := m["properties"].(type) {
	case map[string]interface{}:
		for _, value := range properties {
			deleteGohanExtendedProperties(value.(map[string]interface{}))
		}
		delete(properties, "propertiesOrder")
	case map[string]map[string]interface{}:
		for _, value := range properties {
			deleteGohanExtendedProperties(value)
		}
		delete(properties, "propertiesOrder")
	}
	if list, ok := m["required"].([]string); ok {
		if len(list) == 0 {
			delete(m, "required")
		}
	}
	delete(m, "propertiesOrder")
	data, _ := json.MarshalIndent(i, param.String(), "    ")
	return pongo2.AsValue(string(data)), nil
}
Example #11
0
File: gen.go Project: vozhyk-/gohan
func astType(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	i := in.Interface()
	return pongo2.AsValue(exprToString(i)), nil
}
Example #12
0
File: gen.go Project: vozhyk-/gohan
func reflectType(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
	i := in.Interface()
	v := reflect.ValueOf(i)
	t := v.Type()
	return pongo2.AsValue(t), nil
}