Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.zeppelin.notebook;

import org.apache.zeppelin.display.AngularObject;
import org.apache.zeppelin.display.AngularObjectRegistry;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.apache.zeppelin.display.GUI;
Expand Down Expand Up @@ -232,6 +233,12 @@ protected Object jobRun() throws Throwable {
String scriptBody = getScriptBody();
Map<String, Input> inputs = Input.extractSimpleQueryParam(scriptBody); // inputs will be built
// from script body

final AngularObjectRegistry angularRegistry = repl.getInterpreterGroup()
.getAngularObjectRegistry();

scriptBody = extractVariablesFromAngularRegistry(scriptBody, inputs, angularRegistry);

settings.setForms(inputs);
script = Input.getSimpleQuery(settings.getParams(), scriptBody);
}
Expand Down Expand Up @@ -390,4 +397,25 @@ public Object clone() throws CloneNotSupportedException {
Paragraph paraClone = (Paragraph) this.clone();
return paraClone;
}

String extractVariablesFromAngularRegistry(String scriptBody, Map<String, Input> inputs,
AngularObjectRegistry angularRegistry) {

final String noteId = this.getNote().getId();
final String paragraphId = this.getId();

final Set<String> keys = new HashSet<>(inputs.keySet());

for (String varName : keys) {
final AngularObject paragraphScoped = angularRegistry.get(varName, noteId, paragraphId);
final AngularObject noteScoped = angularRegistry.get(varName, noteId, null);
final AngularObject angularObject = paragraphScoped != null ? paragraphScoped : noteScoped;
if (angularObject != null) {
inputs.remove(varName);
final String pattern = "[$][{]\\s*" + varName + "\\s*(?:=[^}]+)?[}]";
scriptBody = scriptBody.replaceAll(pattern, angularObject.get().toString());
}
}
return scriptBody;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.zeppelin.display;

public class AngularObjectBuilder {

public static <T> AngularObject<T> build(String varName, T value, String noteId,
String paragraphId) {
return new AngularObject<>(varName, value, noteId, paragraphId, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@

package org.apache.zeppelin.notebook;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.apache.zeppelin.display.AngularObject;
import org.apache.zeppelin.display.AngularObjectBuilder;
import org.apache.zeppelin.display.AngularObjectRegistry;
import org.apache.zeppelin.display.Input;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class ParagraphTest {
@Test
Expand All @@ -35,4 +46,43 @@ public void scriptBodyWithoutReplName() {
String text = "12345678";
assertEquals(text, Paragraph.getScriptBody(text));
}

@Test
public void should_extract_variable_from_angular_object_registry() throws Exception {
//Given
final String noteId = "noteId";

final AngularObjectRegistry registry = mock(AngularObjectRegistry.class);
final Note note = mock(Note.class);
final Map<String, Input> inputs = new HashMap<>();
inputs.put("name", null);
inputs.put("age", null);
inputs.put("job", null);

final String scriptBody = "My name is ${name} and I am ${age=20} years old. " +
"My occupation is ${ job = engineer | developer | artists}";

final Paragraph paragraph = new Paragraph(note, null, null);
final String paragraphId = paragraph.getId();

final AngularObject nameAO = AngularObjectBuilder.build("name", "DuyHai DOAN", noteId,
paragraphId);

final AngularObject ageAO = AngularObjectBuilder.build("age", 34, noteId, null);

when(note.getId()).thenReturn(noteId);
when(registry.get("name", noteId, paragraphId)).thenReturn(nameAO);
when(registry.get("age", noteId, null)).thenReturn(ageAO);

final String expected = "My name is DuyHai DOAN and I am 34 years old. " +
"My occupation is ${ job = engineer | developer | artists}";
//When
final String actual = paragraph.extractVariablesFromAngularRegistry(scriptBody, inputs,
registry);

//Then
verify(registry).get("name", noteId, paragraphId);
verify(registry).get("age", noteId, null);
assertEquals(actual, expected);
}
}