You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2019/09/26 18:30:42 UTC

[GitHub] [trafficcontrol] mhoppa commented on a change in pull request #3758: Rewrote deliveryservice_stats to Go

mhoppa commented on a change in pull request #3758: Rewrote deliveryservice_stats to Go
URL: https://github.com/apache/trafficcontrol/pull/3758#discussion_r328729350
 
 

 ##########
 File path: lib/go-rfc/mimetype.go
 ##########
 @@ -0,0 +1,261 @@
+package rfc
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import "mime"
+import "sort"
+import "strings"
+import "strconv"
+
+/*
+MimeType represents a "Media Type" as defined by RFC6838, along with some ease-of-use functionality.
+
+Note that this structure is in no way guaranteed to represent a *real* MIME Type, only one that
+is *syntactically valid*. The hope is that it will make content negotiation easier for developers,
+and should not be considered a security measure by any standard.
+*/
+type MimeType struct {
+	// Name is the full name of the MIME Type, e.g. 'application/json'.
+	// Usually for printing, it's better to call MimeType.String
+	Name string
+	// Parameters contains a map of provided parameter names to corresponding values. Note that for
+	// MimeTypes constructed with NewMimeType, this will always be initialized, even when empty.
+	Parameters map[string]string
+}
+
+/*
+Quality retrieves and parses the "quality" parameter of a MimeType.
+
+As specified in RFC7231, the quality parameter's name is "q", not actually "quality". To obtain
+a literal "quality" parameter value, access MimeType.Parameters directly.
+MimeTypes with no "q" parameter implicitly have a "quality" of 1.0.
+*/
+func (m MimeType) Quality() float64 {
+	if m.Parameters == nil {
+		return 1
+	}
+
+	fs, ok := m.Parameters["q"]
+	if !ok {
+		return 1
+	}
+
+	ret, err := strconv.ParseFloat(fs, 64)
+	if err != nil {
+		return 1
+	}
+	return ret
+}
+
+/*
+Charset retrieves the "charset" parameter of a MimeType.
+
+Returns an empty string if no charset exists in the parameters, or if the parameters themselves are
+not initialized.
+*/
+func (m MimeType) Charset() string {
+	if m.Parameters == nil {
+		return ""
+	}
+
+	c, ok := m.Parameters["charset"]
+	if !ok {
+		return ""
+	}
+	return c
+}
+
+// Type returns only the "main" type of a MimeType.
+func (m MimeType) Type() string {
+	return strings.SplitN(m.Name, "/", 2)[0]
+}
+
+// SubType returns only the "sub" type of a MimeType.
+func (m MimeType) SubType() string {
+	s := strings.SplitN(m.Name, "/", 2)
+	if len(s) != 2 {
+		return ""
+	}
+	return s[1]
+}
+
+// Facet returns the MimeType's "facet" if one exists, otherwise an empty string.
+func (m MimeType) Facet() string {
+	s := m.SubType()
+	if fx := strings.SplitN(s, ".", 2); len(fx) == 2 {
+		return fx[0]
+	}
+	return ""
+}
+
+// Syntax returns the MimeType's "syntax suffix" if one exists, otherwise an empty string.
+func (m MimeType) Syntax() string {
+	s := m.SubType()
+	if fx := strings.Split(s, "+"); len(fx) > 1 {
+		return fx[len(fx)-1]
+	}
+	return ""
+}
+
+// String implements the Stringer interface using mime.FormatMediaType.
+func (m MimeType) String() string {
+	return mime.FormatMediaType(m.Name, m.Parameters)
+}
+
+/*
+Equal checks whether or not the MimeType is "equal to" some other MimeType, o.
+
+Note that this does not check if the two are literally the *same*. Specifically, if the Type or
+SubType of the given MimeType o is the special '*' name, then this will instead check whether or
+not this MimeType can *satisfy* the other according to RFC7231. This means that this equality
+check is NOT associative - that is a.Equal(b) does not imply b.Equal(a).
 
 Review comment:
   Should we use a different name for the function then? maybe `Satisfy` equal is deceiving based on ^ 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services