Skip to content

Commit

Permalink
isSourceDef accepts NamedModelObjects (#1965)
Browse files Browse the repository at this point in the history
removing superflous modelObjIsSource
  • Loading branch information
mtoy-googly-moogly authored Oct 13, 2024
1 parent fb143fd commit efa007b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {QuerySource} from '../source-elements/query-source';
import {NamedSource} from '../source-elements/named-source';
import {QueryReference} from '../query-elements/query-reference';
import {Argument} from '../parameters/argument';
import {modelObjIsSource} from '../../../model';
import {isSourceDef} from '../../../model';

/**
* A reference to either a source or a query.
Expand Down Expand Up @@ -75,7 +75,7 @@ export class SQReference extends SourceQueryElement {

isSource() {
const refTo = this.ref.getNamed();
return refTo !== undefined && modelObjIsSource(refTo);
return refTo !== undefined && isSourceDef(refTo);
}

getSource(): Source | undefined {
Expand All @@ -100,7 +100,7 @@ export class SQReference extends SourceQueryElement {
}
const existingQuery = new QueryReference(this.ref);
this.asSource = new QuerySource(existingQuery);
} else if (modelObjIsSource(entry)) {
} else if (isSourceDef(entry)) {
this.asSource = new NamedSource(this.ref, undefined, this.args);
} else {
this.sqLog(
Expand Down
10 changes: 5 additions & 5 deletions packages/malloy/src/lang/ast/types/malloy-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
Query,
SQLSourceDef,
StructDef,
modelObjIsSource,
isSourceDef,
} from '../../../model/malloy_types';
import {Tag} from '../../../tags';
import {
Expand Down Expand Up @@ -146,7 +146,7 @@ export abstract class MalloyElement {
definition: result.entry,
location: reference.location,
});
} else if (result && modelObjIsSource(result.entry)) {
} else if (result && isSourceDef(result.entry)) {
this.addReference({
type: 'exploreReference',
text: key,
Expand Down Expand Up @@ -526,7 +526,7 @@ export class Document extends MalloyElement implements NameSpace {
for (const [nm, orig] of Object.entries(extendingModelDef.contents)) {
const entry = structuredClone(orig);
if (
modelObjIsSource(entry) ||
isSourceDef(entry) ||
entry.type === 'query' ||
entry.type === 'function'
) {
Expand Down Expand Up @@ -594,7 +594,7 @@ export class Document extends MalloyElement implements NameSpace {
}
for (const entry in this.documentModel) {
const entryDef = this.documentModel[entry].entry;
if (modelObjIsSource(entryDef) || entryDef.type === 'query') {
if (isSourceDef(entryDef) || entryDef.type === 'query') {
if (this.documentModel[entry].exported) {
def.exports.push(entry);
}
Expand Down Expand Up @@ -636,7 +636,7 @@ export class Document extends MalloyElement implements NameSpace {
`Cannot redefine '${str}', which is in global namespace`
);
}
if (modelObjIsSource(ent.entry)) {
if (isSourceDef(ent.entry)) {
this.checkExperimentalDialect(this, ent.entry.dialect);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/malloy/src/lang/parse-malloy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
DocumentReference,
ImportLocation,
ModelDef,
modelObjIsSource,
isSourceDef,
NamedModelObject,
Query,
SourceDef,
Expand Down Expand Up @@ -859,7 +859,7 @@ export abstract class MalloyTranslation {
if (did.translated) {
for (const fromChild of child.modelDef.exports) {
const modelEntry = child.modelDef.contents[fromChild];
if (modelObjIsSource(modelEntry) || modelEntry.type === 'query') {
if (isSourceDef(modelEntry) || modelEntry.type === 'query') {
exports[fromChild] = modelEntry;
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/malloy/src/lang/test/test-translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
TurtleDef,
isQuerySegment,
isSegmentSQL,
modelObjIsSource,
isSourceDef,
SourceDef,
JoinBase,
TableSourceDef,
Expand Down Expand Up @@ -221,7 +221,7 @@ class TestRoot extends MalloyElement implements NameSpace {
const global = this.globalNameSpace.getEntry(name);
if (global) return global;
const struct = this.modelDef.contents[name];
if (modelObjIsSource(struct)) {
if (isSourceDef(struct)) {
const exported = this.modelDef.exports.includes(name);
return {entry: struct, exported};
}
Expand Down Expand Up @@ -390,7 +390,7 @@ export class TestTranslator extends MalloyTranslator {

exploreFor(exploreName: string): StructDef {
const explore = this.nameSpace[exploreName];
if (explore && modelObjIsSource(explore)) {
if (explore && isSourceDef(explore)) {
return explore;
}
throw new Error(`Expected model to contain source '${exploreName}'`);
Expand All @@ -413,7 +413,7 @@ export class TestTranslator extends MalloyTranslator {
getSourceDef(srcName: string): SourceDef | undefined {
const t = this.translate().translated;
const s = t?.modelDef?.contents[srcName];
if (s && modelObjIsSource(s)) {
if (s && isSourceDef(s)) {
return s;
}
return undefined;
Expand Down Expand Up @@ -442,7 +442,7 @@ export class BetaExpression extends TestTranslator {

private testFS() {
const aStruct = this.internalModel.contents['ab'];
if (modelObjIsSource(aStruct)) {
if (isSourceDef(aStruct)) {
const tstFS = new StaticSourceSpace(aStruct);
return tstFS;
} else {
Expand Down
9 changes: 4 additions & 5 deletions packages/malloy/src/malloy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ import {
QueryValue,
SQLSentence,
SQLSourceDef,
modelObjIsSource,
AtomicFieldDef,
DateFieldDef,
TimestampFieldDef,
Expand Down Expand Up @@ -817,7 +816,7 @@ export class Model implements Taggable {
*/
public getExploreByName(name: string): Explore {
const struct = this.modelDef.contents[name];
if (modelObjIsSource(struct)) {
if (isSourceDef(struct)) {
return new Explore(struct);
}
throw new Error("'name' is not an explore");
Expand All @@ -830,7 +829,7 @@ export class Model implements Taggable {
*/
public get explores(): Explore[] {
return Object.values(this.modelDef.contents)
.filter(modelObjIsSource)
.filter(isSourceDef)
.map(structDef => new Explore(structDef));
}

Expand Down Expand Up @@ -917,7 +916,7 @@ export class PreparedQuery implements Taggable {
typeof sourceRef === 'string'
? this._modelDef.contents[sourceRef]
: sourceRef;
if (!modelObjIsSource(source)) {
if (!isSourceDef(source)) {
throw new Error('Invalid source for query');
}
return source.dialect;
Expand Down Expand Up @@ -1295,7 +1294,7 @@ export class PreparedResult implements Taggable {
if (explore === undefined) {
throw new Error('Malformed query result.');
}
if (modelObjIsSource(explore)) {
if (isSourceDef(explore)) {
return new Explore(explore);
}
throw new Error(`'${name} is not an explore`);
Expand Down
3 changes: 1 addition & 2 deletions packages/malloy/src/model/malloy_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ import {
SpreadExpr,
FilteredExpr,
SourceDef,
modelObjIsSource,
isSourceDef,
fieldIsIntrinsic,
AtomicFieldDef,
Expand Down Expand Up @@ -4692,7 +4691,7 @@ export class QueryModel {
this.modelDef = modelDef;
for (const s of Object.values(this.modelDef.contents)) {
let qs;
if (modelObjIsSource(s)) {
if (isSourceDef(s)) {
qs = new QueryStruct(s, undefined, {model: this}, {});
this.structs.set(getIdentifier(s), qs);
qs.resolveQueryFields();
Expand Down
6 changes: 1 addition & 5 deletions packages/malloy/src/model/malloy_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ export function sourceBase(sd: SourceDefBase): SourceDefBase {
return {...sd};
}

export function isSourceDef(sd: StructDef | FieldDef): sd is SourceDef {
export function isSourceDef(sd: NamedModelObject | FieldDef): sd is SourceDef {
return (
sd.type === 'table' ||
sd.type === 'sql_select' ||
Expand Down Expand Up @@ -1227,10 +1227,6 @@ export type NamedModelObject =
| FunctionDef
| ConnectionDef;

export function modelObjIsSource(nmo: NamedModelObject): nmo is SourceDef {
return isSourceDef(nmo as StructDef);
}

/** Result of parsing a model file */
export interface ModelDef {
name: string;
Expand Down

0 comments on commit efa007b

Please sign in to comment.