// Extract returns a SpanContext given the format and the carrier.
// The SpanContext returned represents the parent span (ie, SpanId refers to the parent span's own SpanId).
// TODO support all the BuiltinFormats
194522
func (t Tracer) Extract(format interface{}, carrier interface{}) (ctx opentracing.SpanContext, err error) {
	defer func() {
		if r := recover(); r != nil {
			// TODO annotate this error type
			err = ErrContractViolation{r}
		}
	}()

	if format == opentracing.Binary {
		// carrier is guaranteed to be an io.Reader by contract
		r := carrier.(io.Reader)
		packet, err := ioutil.ReadAll(r)
		if err != nil {
			return nil, err
		}

		sample := ssf.SSFSpan{}
		err = proto.Unmarshal(packet, &sample)
		if err != nil {
			return nil, err
		}

		resource := sample.Tags[ResourceKey]

		trace := &Trace{
			TraceID:  sample.TraceId,
			SpanID:   sample.Id,
			Resource: resource,
		}

		return trace.context(), nil
	}

	if tm, ok := carrier.(opentracing.TextMapReader); ok {
		// carrier is guaranteed to be an opentracing.TextMapReader by contract
		// TODO support other TextMapReader implementations
		var traceID int64
		var spanID int64
		for _, headers := range HeaderFormats {

			base := 10
			if headers.numFormat == hexadecimal {
				base = 16
			}

			traceID, _ = strconv.ParseInt(textMapReaderGet(tm, headers.TraceID), base, 64)
			spanID, _ = strconv.ParseInt(textMapReaderGet(tm, headers.SpanID), base, 64)

			if traceID != 0 && spanID != 0 {
				break
			}
		}
		if traceID == 0 && spanID == 0 {
			return nil, errors.New("error parsing fields from TextMapReader")
		}

		trace := &Trace{
			TraceID:  traceID,
			SpanID:   spanID,
			Resource: textMapReaderGet(tm, ResourceKey),
		}

		return trace.context(), nil
	}

	return nil, opentracing.ErrUnsupportedFormat
}
309348
func extract(dir string) error {

	// create dir if needed
	if _, err := os.Stat(dir); os.IsNotExist(err) {
		if err := os.MkdirAll(dir, 0777); err != nil {
			return err
		}
	}

	return unzip("apidocs_html.zip", dir)
}
1046935
func Parent(sc model.SpanContext) SpanOption {
	return func(t *Tracer, s *spanImpl) {
		if sc.Err != nil {
			// encountered an extraction error
			switch t.extractFailurePolicy {
			case ExtractFailurePolicyRestart:
			case ExtractFailurePolicyError:
				panic(s.SpanContext.Err)
			case ExtractFailurePolicyTagAndRestart:
				s.Tags["error.extract"] = sc.Err.Error()
			default:
				panic(ErrInvalidExtractFailurePolicy)
			}
			/* don't use provided SpanContext, but restart trace */
			return
		}
		s.SpanContext = sc
	}
}
1215601
func (s *Schema) Extract(m map[string]interface{}) error {
	if pdebug.Enabled {
		g := pdebug.IPrintf("START Schema.Extract")
		defer g.IRelease("END Schema.Extract")
	}

	var err error

	if err = extractString(&s.ID, m, "id"); err != nil {
		return errors.Wrapf(err, "failed to extract 'id'")
	}

	if err = extractString(&s.Title, m, "title"); err != nil {
		return errors.Wrap(err, "failed to extract 'title'")
	}

	if err = extractString(&s.Description, m, "description"); err != nil {
		return errors.Wrap(err, "failed to extract 'description'")
	}

	if err = extractStringList(&s.Required, m, "required"); err != nil {
		return errors.Wrap(err, "failed to extract 'required'")
	}

	if err = extractJSPointer(&s.SchemaRef, m, "$schema"); err != nil {
		return errors.Wrap(err, "failed to extract '$schema'")
	}

	if err = extractJSPointer(&s.Reference, m, "$ref"); err != nil {
		return errors.Wrap(err, "failed to extract '$ref'")
	}

	if err = extractFormat(&s.Format, m, "format"); err != nil {
		return errors.Wrap(err, "failed to extract 'format'")
	}

	if err = extractInterfaceList(&s.Enum, m, "enum"); err != nil {
		return errors.Wrap(err, "failed to extract 'enum'")
	}

	if err = extractInterface(&s.Default, m, "default"); err != nil {
		return errors.Wrap(err, "failed to extract 'default'")
	}

	if err = extractType(&s.Type, m, "type"); err != nil {
		return errors.Wrap(err, "failed to extract 'type'")
	}

	if s.Definitions, err = extractSchemaMap(m, "definitions"); err != nil {
		return errors.Wrap(err, "failed to extract 'definitions'")
	}

	if err = extractItems(&s.Items, m, "items"); err != nil {
		return errors.Wrap(err, "failed to extract 'items'")
	}

	if err = extractRegexp(&s.Pattern, m, "pattern"); err != nil {
		return errors.Wrap(err, "failed to extract 'patterns'")
	}

	if extractInt(&s.MinLength, m, "minLength"); err != nil {
		return errors.Wrap(err, "failed to extract 'minLength'")
	}

	if extractInt(&s.MaxLength, m, "maxLength"); err != nil {
		return errors.Wrap(err, "failed to extract 'maxLength'")
	}

	if extractInt(&s.MinItems, m, "minItems"); err != nil {
		return errors.Wrap(err, "failed to extract 'minItems'")
	}

	if extractInt(&s.MaxItems, m, "maxItems"); err != nil {
		return errors.Wrap(err, "failed to extract 'maxItems'")
	}

	if err = extractBool(&s.UniqueItems, m, "uniqueItems", false); err != nil {
		return errors.Wrap(err, "failed to extract 'uniqueItems'")
	}

	if err = extractInt(&s.MaxProperties, m, "maxProperties"); err != nil {
		return errors.Wrap(err, "failed to extract 'maxProperties'")
	}

	if err = extractInt(&s.MinProperties, m, "minProperties"); err != nil {
		return errors.Wrap(err, "failed to extract 'minProperties'")
	}

	if err = extractNumber(&s.Minimum, m, "minimum"); err != nil {
		return errors.Wrap(err, "failed to extract 'minimum'")
	}

	if err = extractBool(&s.ExclusiveMinimum, m, "exclusiveMinimum", false); err != nil {
		return errors.Wrap(err, "failed to extract 'exclusiveMinimum'")
	}

	if err = extractNumber(&s.Maximum, m, "maximum"); err != nil {
		return errors.Wrap(err, "failed to extract 'maximum'")
	}

	if err = extractBool(&s.ExclusiveMaximum, m, "exclusiveMaximum", false); err != nil {
		return errors.Wrap(err, "failed to extract 'exclusiveMaximum'")
	}

	if err = extractNumber(&s.MultipleOf, m, "multipleOf"); err != nil {
		return errors.Wrap(err, "failed to extract 'multipleOf'")
	}

	if s.Properties, err = extractSchemaMap(m, "properties"); err != nil {
		return errors.Wrap(err, "failed to extract 'properties'")
	}

	if err = extractDependecies(&s.Dependencies, m, "dependencies"); err != nil {
		return errors.Wrap(err, "failed to extract 'dependencies'")
	}

	if _, ok := m["additionalItems"]; !ok {
		// doesn't exist. it's an empty schema
		s.AdditionalItems = &AdditionalItems{}
	} else {
		var b Bool
		if err = extractBool(&b, m, "additionalItems", true); err == nil {
			if b.Bool() {
				s.AdditionalItems = &AdditionalItems{}
			}
		} else {
			// Oh, it's not a boolean?
			var apSchema *Schema
			if err = extractSchema(&apSchema, m, "additionalItems"); err != nil {
				return errors.Wrap(err, "failed to extract 'additionalItems'")
			}
			s.AdditionalItems = &AdditionalItems{apSchema}
		}
	}

	if _, ok := m["additionalProperties"]; !ok {
		// doesn't exist. it's an empty schema
		s.AdditionalProperties = &AdditionalProperties{}
	} else {
		var b Bool
		if err = extractBool(&b, m, "additionalProperties", true); err == nil {
			if b.Bool() {
				s.AdditionalProperties = &AdditionalProperties{}
			}
		} else {
			// Oh, it's not a boolean?
			var apSchema *Schema
			if err = extractSchema(&apSchema, m, "additionalProperties"); err != nil {
				return errors.Wrap(err, "failed to extract 'additionalProperties'")
			}
			s.AdditionalProperties = &AdditionalProperties{apSchema}
		}
	}

	if s.PatternProperties, err = extractRegexpToSchemaMap(m, "patternProperties"); err != nil {
		return errors.Wrap(err, "failed to extract 'patternProperties'")
	}

	if err = s.AllOf.extractIfPresent(m, "allOf"); err != nil {
		return errors.Wrap(err, "failed to extract 'allOf'")
	}

	if err = s.AnyOf.extractIfPresent(m, "anyOf"); err != nil {
		return errors.Wrap(err, "failed to extract 'anyOf'")
	}

	if err = s.OneOf.extractIfPresent(m, "oneOf"); err != nil {
		return errors.Wrap(err, "failed to extract 'oneOf'")
	}

	if err = extractSchema(&s.Not, m, "not"); err != nil {
		return errors.Wrap(err, "failed to extract 'not'")
	}

	s.applyParentSchema()

	s.Extras = make(map[string]interface{})
	for k, v := range m {
		switch k {
		case "id", "title", "description", "required", "$schema", "$ref", "format", "enum", "default", "type", "definitions", "items", "pattern", "minLength", "maxLength", "minItems", "maxItems", "uniqueItems", "maxProperties", "minProperties", "minimum", "exclusiveMinimum", "maximum", "exclusiveMaximum", "multipleOf", "properties", "dependencies", "additionalItems", "additionalProperties", "patternProperties", "allOf", "anyOf", "oneOf", "not":
			continue
		}
		if pdebug.Enabled {
			pdebug.Printf("Extracting extra field '%s'", k)
		}
		s.Extras[k] = v
	}

	if pdebug.Enabled {
		pdebug.Printf("Successfully extracted schema")
	}

	return nil
}
880688
func (p *testDataParser) ExtractDuration(line string) (string, bool) {
	if matches := p.testConclusionPattern.FindStringSubmatch(line); len(matches) > 2 && len(matches[2]) > 0 {
		return matches[2], true
	}

	return "", false
}
839175
func (m ConfigMap) extract(key string, defval ConfigValue) (ConfigValue, error) {

	v, err := m.get(key, defval)
	if err != nil {
		return nil, err
	}

	delete(m, key)

	return v, nil
}
227716
func (e *extractor) Extract(tarballPath string) (ExtractedStemcell, error) {
	tmpDir, err := e.fs.TempDir("stemcell-manager")
	if err != nil {
		return nil, bosherr.WrapError(err, "creating temp dir for stemcell extraction")
	}

	stemcell, err := e.reader.Read(tarballPath, tmpDir)
	if err != nil {
		_ = e.fs.RemoveAll(tmpDir)
		return nil, bosherr.WrapErrorf(err, "reading extracted stemcell manifest in '%s'", tmpDir)
	}

	return stemcell, nil
}
1126405
func ExtractFromHTTPRequest(req *http.Request, handlerName string) (opentracing.Span, *http.Request) {
	spanContext, err := opentracing.GlobalTracer().Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
	if err != nil {
		span, ctx := opentracing.StartSpanFromContext(req.Context(), handlerName+":"+req.URL.Path)
		LogError(span, err)
		req = req.WithContext(ctx)
		return span, req
	}

	span := opentracing.StartSpan(handlerName+":"+req.URL.Path, opentracing.ChildOf(spanContext), ext.RPCServerOption(spanContext))
	req = req.WithContext(opentracing.ContextWithSpan(req.Context(), span))
	return span, req
}
730033
func Extract(ctx context.Context) *logrus.Entry {
	l, ok := ctx.Value(ctxLoggerKey).(*ctxLogger)
	if !ok || l == nil {
		return logrus.NewEntry(nullLogger)
	}

	fields := logrus.Fields{}

	// Add grpc_ctxtags tags metadata until now.
	tags := grpc_ctxtags.Extract(ctx)
	for k, v := range tags.Values() {
		fields[k] = v
	}

	// Add logrus fields added until now.
	for k, v := range l.fields {
		fields[k] = v
	}

	return l.logger.WithFields(fields)
}
3330
func Extract(reader io.Reader, dest string) error {
	tarReader := tar.NewReader(reader)

	for {
		header, err := tarReader.Next()

		if err == io.EOF {
			break
		}

		if err != nil {
			return err
		}

		if header.Name == "." {
			continue
		}

		err = extract(tarReader, header, dest)
		if err != nil {
			return err
		}
	}

	return nil
}
235548
func ExtractSpan(ctx context.Context) (opentracing.SpanContext, error) {
	return tracing.ExtractSpan(ctx)
}
236562
func (s *Span) Attach(ctx context.Context) context.Context {
	return opentracing.ContextWithSpan(ctx, s)
}
245737
func (ctx *Context) ToContext(parentCtx ...context.Context) context.Context {
	if len(parentCtx) > 0 {
		return context.WithValue(parentCtx[0], ContextKey, ctx)
	}

	return context.WithValue(context.Background(), ContextKey, ctx)
}
469748
func (e *Extractor) Extract(token *jwt.Token) (interface{}, error) {
	e.Token = token

	if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
		return nil, errors.New("invalid signing method")
	}

	claims, ok := token.Claims.(*KiteClaims)
	if !ok {
		return nil, fmt.Errorf("no kontrol key found")
	}

	e.Claims = claims

	return jwt.ParseRSAPublicKeyFromPEM([]byte(claims.KontrolKey))
}
1315384
func extract(filePath string, directory string) error {
	file, err := os.Open(filePath)
	if err != nil {
		return err
	}
	defer file.Close()

	gzipReader, err := gzip.NewReader(bufio.NewReader(file))
	if err != nil {
		return err
	}
	defer gzipReader.Close()

	tarReader := tar.NewReader(gzipReader)

	for {
		header, err := tarReader.Next()
		if err == io.EOF {
			break
		}
		if err != nil {
			return err
		}

		fileInfo := header.FileInfo()
		dir := filepath.Join(directory, filepath.Dir(header.Name))
		filename := filepath.Join(dir, fileInfo.Name())

		err = os.MkdirAll(dir, 0755)
		if err != nil {
			return err
		}

		file, err := os.Create(filename)
		if err != nil {
			return err
		}

		writer := bufio.NewWriter(file)

		buffer := make([]byte, 4096)
		for {
			n, err := tarReader.Read(buffer)
			if err != nil && err != io.EOF {
				panic(err)
			}
			if n == 0 {
				break
			}

			_, err = writer.Write(buffer[:n])
			if err != nil {
				return err
			}
		}

		err = writer.Flush()
		if err != nil {
			return err
		}

		err = file.Close()
		if err != nil {
			return err
		}
	}

	return nil
}
73825
func (txz *TarXz) Extract(source, target, destination string) error {
	txz.wrapReader()
	return txz.Tar.Extract(source, target, destination)
}
1266914
func (t *Tracer) ExtractHTTPHeaders(r *http.Request) (tracing.Span, context.Context) {
	// Deserialize tracing context into request.
	wireContext, _ := t.tracer.Extract(
		opentracing.HTTPHeaders,
		opentracing.HTTPHeadersCarrier(r.Header),
	)

	span := t.tracer.StartSpan("HTTP", ext.RPCServerOption(wireContext))
	ctx := opentracing.ContextWithSpan(r.Context(), span)
	return span, ctx
}