factor out common code in the metadata builders
fixup for 862ffa365b7448cf871303efb7c272fbd86bd62a, will be squashed