<commit_before>package fhirutil

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"reflect"

	"github.com/intervention-engine/fhir/models"
	"gopkg.in/mgo.v2/bson"
)

// GetResourceID returns the string equivalent of a FHIR resource ID.
func GetResourceID(resource interface{}) string {
	return reflect.ValueOf(resource).Elem().FieldByName("Id").String()
}

// SetResourceID sets or updates the Id for a FHIR resource.
func SetResourceID(resource interface{}, newID string) {
	reflect.ValueOf(resource).Elem().FieldByName("Id").SetString(newID)
}

// GetResourceType returns the string equivalent of a FHIR resource type.
func GetResourceType(resource interface{}) string {
	val := reflect.ValueOf(resource)
	if val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
		return val.Elem().FieldByName("ResourceType").String()
	}
	return ""
}

// JSONGetResourceType gets the string equivalent of a FHIR resource type
// from the map[string]interface{} equivalent of FHIR JSON for that resource.
func JSONGetResourceType(obj []byte) string {
	var resource map[string]interface{}
	err := json.Unmarshal(obj, &resource)
	if err != nil {
		return ""
	}

	s, ok := resource["resourceType"].(string)
	if !ok {
		return ""
	}
	return s
}

// GetResourceByURL GETs a FHIR resource from it's specified URL.
func GetResourceByURL(resourceType, resourceURL string) (resource interface{}, err error) {
	// Make the request.
	res, err := http.Get(resourceURL)
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()

	if res.StatusCode == 404 {
		return nil, fmt.Errorf("Resource %s not found", resourceURL)
	}

	if res.StatusCode != 200 {
		return nil, fmt.Errorf("An unexpected error occured while requesting resource %s", resourceURL)<commit_msg>Update resource_utilities.go<commit_after>package fhirutil

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"reflect"

	"github.com/intervention-engine/fhir/models"
	"gopkg.in/mgo.v2/bson"
)

// GetResourceID returns the string equivalent of a FHIR resource ID.
func GetResourceID(resource interface{}) string {
	return reflect.ValueOf(resource).Elem().FieldByName("Id").String()
}

// SetResourceID sets or updates the Id for a FHIR resource.
func SetResourceID(resource interface{}, newID string) {
	reflect.ValueOf(resource).Elem().FieldByName("Id").SetString(newID)
}

// GetResourceType returns the string equivalent of a FHIR resource type.
func GetResourceType(resource interface{}) string {
	val := reflect.ValueOf(resource)
	if val.Kind() == reflect.Ptr || val.Kind() == reflect.Interface {
		return val.Elem().FieldByName("ResourceType").String()
	}
	return ""
}

// JSONGetResourceType gets the string equivalent of a FHIR resource type
// from the JSON byte string of that resource.
func JSONGetResourceType(obj []byte) string {
	var resource map[string]interface{}
	err := json.Unmarshal(obj, &resource)
	if err != nil {
		return ""
	}

	s, ok := resource["resourceType"].(string)
	if !ok {
		return ""
	}
	return s
}

// GetResourceByURL GETs a FHIR resource from it's specified URL.
func GetResourceByURL(resourceType, resourceURL string) (resource interface{}, err error) {
	// Make the request.
	res, err := http.Get(resourceURL)
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()

	if res.StatusCode == 404 {
		return nil, fmt.Errorf("Resource %s not found", resourceURL)
	}

	if res.StatusCode != 200 {
		return nil, fmt.Errorf("An unexpected error occured while requesting resource %s", resourceURL)
{'fraction_non_alphanumeric': 0.1277019340159272, 'fraction_numerical': 0.003981797497155859, 'mean_word_length': 4.573692551505546, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6934927', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1097, 'n_words': 326}