Skip to content

Commit

Permalink
Add structure_hash and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman committed Nov 28, 2023
1 parent e60296f commit 79cd3ac
Show file tree
Hide file tree
Showing 5 changed files with 471 additions and 5 deletions.
6 changes: 6 additions & 0 deletions components/support/nimbus-fml/src/client/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ impl FmlFeatureInspector {
pub fn get_errors(&self, string: String) -> Option<Vec<FmlEditorError>> {
self.get_first_error(string).map(|e| vec![e])
}

pub fn get_structure_hash(&self) -> String {
self.manifest
.get_structure_hash(&self.feature_id)
.unwrap_or_default()
}
}

impl FmlFeatureInspector {
Expand Down
18 changes: 18 additions & 0 deletions components/support/nimbus-fml/src/fml.udl
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ interface FmlFeatureInspector {
// Returns the default JSON for the feature for this channel.
[Throws=FMLError]
JsonObject get_default_json();

// Returns a 8 digit hex hash of the structure of the feature.
//
// This is suitable for tracking over time.
//
// This is a truncated SHA256 hash which takes in to consideration
// the name and types of all the variables in this feature, and nested
// objects, as well as the variants of enums.
//
// It does not take into consideration the default values for this
// feature, ordering of fields or variables or enum variants, or
// documentation, so as to remain stable despite superficial changes
// to the feature's configuration.
//
// If this hash changes for a given feature then it is almost
// certain that the code which uses this configuration will also have
// changed.
string get_structure_hash();
};

dictionary FmlEditorError {
Expand Down
17 changes: 13 additions & 4 deletions components/support/nimbus-fml/src/intermediate_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::defaults::{DefaultsMerger, DefaultsValidator};
use crate::error::FMLError::InvalidFeatureError;
use crate::error::{FMLError, Result};
use crate::frontend::{AboutBlock, FeatureMetadata};
use crate::structure::{StructureValidator, TypeQuery};
use crate::structure::{StructureHasher, StructureValidator};
use crate::util::loaders::FilePath;
use anyhow::{bail, Error, Result as AnyhowResult};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -456,12 +456,21 @@ impl FeatureManifest {

Ok(feature_def)
}

pub fn get_structure_hash(&self, feature_name: &str) -> Result<String> {
let (manifest, feature_def) = self
.find_feature(feature_name)
.ok_or_else(|| InvalidFeatureError(feature_name.to_string()))?;

Ok(manifest.feature_structure_hash(feature_def))
}
}

impl FeatureManifest {
pub(crate) fn feature_types(&self, feature_def: &FeatureDef) -> HashSet<TypeRef> {
let all_types = TypeQuery::new(&self.obj_defs);
all_types.all_types(feature_def)
pub(crate) fn feature_structure_hash(&self, feature_def: &FeatureDef) -> String {
let hasher = StructureHasher::new(&self.enum_defs, &self.obj_defs);
let hash = hasher.hash(feature_def) & 0xffffffff;
format!("{hash:x}")
}
}

Expand Down

0 comments on commit 79cd3ac

Please sign in to comment.