// UnmarshalDeltaSpec marshals a map containing route variables // generated by (*DeltaSpec).RouteVars() and returns the // equivalent DeltaSpec struct. func UnmarshalDeltaSpec(routeVars map[string]string) (DeltaSpec, error) { s := DeltaSpec{} rr, err := UnmarshalRepoRevSpec(routeVars) if err != nil { return DeltaSpec{}, err } s.Base = rr dhr := routeVars["DeltaHeadResolvedRev"] if i := strings.Index(dhr, ":"); i != -1 { // base repo != head repo repoPCB64, revPC := dhr[:i], dhr[i+1:] repoPC, err := base64.URLEncoding.DecodeString(repoPCB64) if err != nil { return DeltaSpec{}, err } rev, commitID, err := spec.ParseResolvedRev(revPC) if err != nil { return DeltaSpec{}, err } s.Head = RepoRevSpec{RepoSpec: RepoSpec{URI: string(repoPC)}, Rev: rev, CommitID: commitID} } else { rev, commitID, err := spec.ParseResolvedRev(dhr) if err != nil { return DeltaSpec{}, err } s.Head = RepoRevSpec{RepoSpec: rr.RepoSpec, Rev: rev, CommitID: commitID} } return s, nil }
// FixResolvedRevVars is a mux.PostMatchFunc that cleans and // normalizes the route vars pertaining to a ResolvedRev (Rev and CommitID). func FixResolvedRevVars(req *http.Request, match *mux.RouteMatch, r *mux.Route) { if rrev, present := match.Vars["ResolvedRev"]; present { rev, commitID, err := spec.ParseResolvedRev(rrev) if err == nil || rrev == "" { // Propagate ResolvedRev if it was set and if parsing // failed; otherwise remove it. delete(match.Vars, "ResolvedRev") } if err == nil { if rev != "" { match.Vars["Rev"] = rev } if commitID != "" { match.Vars["CommitID"] = commitID } } } }