func readDimension()

in pkg/data-query/parser.go [152:208]


func readDimension(v *fastjson.Value) (*QueryDimension, error) {
	var t QueryDimension
	if v.Type() == fastjson.TypeString {
		t = QueryDimension{
			Name: string(v.GetStringBytes()),
		}
	} else {
		subNameValue := v.Get("subName")
		if subNameValue == nil {
			t = QueryDimension{
				Name: string(v.GetStringBytes("n")),
				Sql:  string(v.GetStringBytes("sql")),
			}
		} else {
			arrayJoin := string(v.GetStringBytes("n"))
			t = QueryDimension{
				Name:               arrayJoin + "." + string(subNameValue.GetStringBytes()),
				arrayJoin:          arrayJoin,
				Sql:                string(v.GetStringBytes("sql")),
				resultPropertyName: string(v.GetStringBytes("resultKey")),
			}

			if !reNestedFieldName.MatchString(t.Name) {
				return nil, http_error.NewHttpError(400, fmt.Sprintf("Name %s is not a valid field name", t.Name))
			}
			if !isValidFieldName(t.arrayJoin) {
				return nil, http_error.NewHttpError(400, fmt.Sprintf("subName %s is not a valid field name", t.Name))
			}

			return &t, nil
		}
	}

	t.resultPropertyName = string(v.GetStringBytes("resultKey"))

	qualifierDotIndex := strings.IndexRune(t.Name, '.')
	if qualifierDotIndex != -1 {
		t.metricPath = t.Name[0:qualifierDotIndex]
		t.metricName = t.Name[qualifierDotIndex+1:]
		t.metricValueName = 'd'

		metricNameLength := len(t.metricName)
		if metricNameLength > 2 && t.metricName[metricNameLength-2] == '.' {
			t.metricValueName = rune(t.metricName[metricNameLength-1])
			t.metricName = t.metricName[:metricNameLength-2]
		}

		if t.resultPropertyName == "" {
			t.resultPropertyName = strings.ReplaceAll(t.metricName, " ", "_")
		}

		if !isValidFieldName(t.metricPath) || !reMetricName.MatchString(t.metricName) {
			return nil, http_error.NewHttpError(400, fmt.Sprintf("Name %s is not a valid field name", t.Name))
		}
	}
	return &t, nil
}